Objects
class Objects
kotlin.Any | |
↳ | java.util.Objects |
This class consists of static
utility methods for operating on objects, or checking certain conditions before operation. These utilities include null
-safe or null
-tolerant methods for computing the hash code of an object, returning a string for an object, comparing two objects, and checking if indexes or sub-range values are out of bounds.
Summary
Public methods | |
---|---|
static Int |
checkFromIndexSize(fromIndex: Int, size: Int, length: Int) Checks if the sub-range from |
static Long |
checkFromIndexSize(fromIndex: Long, size: Long, length: Long) Checks if the sub-range from |
static Int |
checkFromToIndex(fromIndex: Int, toIndex: Int, length: Int) Checks if the sub-range from |
static Long |
checkFromToIndex(fromIndex: Long, toIndex: Long, length: Long) Checks if the sub-range from |
static Int |
checkIndex(index: Int, length: Int) Checks if the |
static Long |
checkIndex(index: Long, length: Long) Checks if the |
static Int |
compare(a: T, b: T, c: Comparator<in T>) Returns 0 if the arguments are identical and |
static Boolean |
deepEquals(a: Any?, b: Any?) Returns |
static Boolean |
Returns |
static Int |
Generates a hash code for a sequence of input values. |
static Int |
Returns the hash code of a non- |
static Boolean |
Returns |
static Boolean |
Returns |
static T |
requireNonNull(obj: T?) Checks that the specified object reference is not |
static T |
requireNonNull(obj: T?, message: String) Checks that the specified object reference is not |
static T |
requireNonNull(obj: T?, messageSupplier: Supplier<String!>) Checks that the specified object reference is not |
static T |
requireNonNullElse(obj: T?, defaultObj: T) Returns the first argument if it is non- |
static T |
requireNonNullElseGet(obj: T?, supplier: Supplier<out T>) Returns the first argument if it is non- |
static String |
Returns the result of calling |
static String |
Returns the result of calling |
Public methods
checkFromIndexSize
static fun checkFromIndexSize(
fromIndex: Int,
size: Int,
length: Int
): Int
Checks if the sub-range from fromIndex
(inclusive) to fromIndex + size
(exclusive) is within the bounds of range from 0
(inclusive) to length
(exclusive).
The sub-range is defined to be out of bounds if any of the following inequalities is true:
fromIndex < 0
size < 0
fromIndex + size > length
, taking into account integer overflowlength < 0
, which is implied from the former inequalities
Parameters | |
---|---|
fromIndex |
Int: the lower-bound (inclusive) of the sub-interval |
size |
Int: the size of the sub-range |
length |
Int: the upper-bound (exclusive) of the range |
Return | |
---|---|
Int |
fromIndex if the sub-range within bounds of the range |
Exceptions | |
---|---|
java.lang.IndexOutOfBoundsException |
if the sub-range is out of bounds |
checkFromIndexSize
static fun checkFromIndexSize(
fromIndex: Long,
size: Long,
length: Long
): Long
Checks if the sub-range from fromIndex
(inclusive) to fromIndex + size
(exclusive) is within the bounds of range from 0
(inclusive) to length
(exclusive).
The sub-range is defined to be out of bounds if any of the following inequalities is true:
fromIndex < 0
size < 0
fromIndex + size > length
, taking into account integer overflowlength < 0
, which is implied from the former inequalities
Parameters | |
---|---|
fromIndex |
Long: the lower-bound (inclusive) of the sub-interval |
size |
Long: the size of the sub-range |
length |
Long: the upper-bound (exclusive) of the range |
Return | |
---|---|
Long |
fromIndex if the sub-range within bounds of the range |
Exceptions | |
---|---|
java.lang.IndexOutOfBoundsException |
if the sub-range is out of bounds |
checkFromToIndex
static fun checkFromToIndex(
fromIndex: Int,
toIndex: Int,
length: Int
): Int
Checks if the sub-range from fromIndex
(inclusive) to toIndex
(exclusive) is within the bounds of range from 0
(inclusive) to length
(exclusive).
The sub-range is defined to be out of bounds if any of the following inequalities is true:
fromIndex < 0
fromIndex > toIndex
toIndex > length
length < 0
, which is implied from the former inequalities
Parameters | |
---|---|
fromIndex |
Int: the lower-bound (inclusive) of the sub-range |
toIndex |
Int: the upper-bound (exclusive) of the sub-range |
length |
Int: the upper-bound (exclusive) the range |
Return | |
---|---|
Int |
fromIndex if the sub-range within bounds of the range |
Exceptions | |
---|---|
java.lang.IndexOutOfBoundsException |
if the sub-range is out of bounds |
checkFromToIndex
static fun checkFromToIndex(
fromIndex: Long,
toIndex: Long,
length: Long
): Long
Checks if the sub-range from fromIndex
(inclusive) to toIndex
(exclusive) is within the bounds of range from 0
(inclusive) to length
(exclusive).
The sub-range is defined to be out of bounds if any of the following inequalities is true:
fromIndex < 0
fromIndex > toIndex
toIndex > length
length < 0
, which is implied from the former inequalities
Parameters | |
---|---|
fromIndex |
Long: the lower-bound (inclusive) of the sub-range |
toIndex |
Long: the upper-bound (exclusive) of the sub-range |
length |
Long: the upper-bound (exclusive) the range |
Return | |
---|---|
Long |
fromIndex if the sub-range within bounds of the range |
Exceptions | |
---|---|
java.lang.IndexOutOfBoundsException |
if the sub-range is out of bounds |
checkIndex
static fun checkIndex(
index: Int,
length: Int
): Int
Checks if the index
is within the bounds of the range from 0
(inclusive) to length
(exclusive).
The index
is defined to be out of bounds if any of the following inequalities is true:
index < 0
index >= length
length < 0
, which is implied from the former inequalities
Parameters | |
---|---|
index |
Int: the index |
length |
Int: the upper-bound (exclusive) of the range |
Return | |
---|---|
Int |
index if it is within bounds of the range |
Exceptions | |
---|---|
java.lang.IndexOutOfBoundsException |
if the index is out of bounds |
checkIndex
static fun checkIndex(
index: Long,
length: Long
): Long
Checks if the index
is within the bounds of the range from 0
(inclusive) to length
(exclusive).
The index
is defined to be out of bounds if any of the following inequalities is true:
index < 0
index >= length
length < 0
, which is implied from the former inequalities
Parameters | |
---|---|
index |
Long: the index |
length |
Long: the upper-bound (exclusive) of the range |
Return | |
---|---|
Long |
index if it is within bounds of the range |
Exceptions | |
---|---|
java.lang.IndexOutOfBoundsException |
if the index is out of bounds |
compare
static fun <T : Any!> compare(
a: T,
b: T,
c: Comparator<in T>
): Int
Returns 0 if the arguments are identical and c.compare(a, b)
otherwise. Consequently, if both arguments are null
0 is returned.
Note that if one of the arguments is null
, a NullPointerException
may or may not be thrown depending on what ordering policy, if any, the Comparator
chooses to have for null
values.
Parameters | |
---|---|
<T> |
the type of the objects being compared |
a |
T: an object |
b |
T: an object to be compared with a |
c |
Comparator<in T>: the Comparator to compare the first two arguments |
Return | |
---|---|
Int |
0 if the arguments are identical and c.compare(a, b) otherwise. |
See Also
deepEquals
static fun deepEquals(
a: Any?,
b: Any?
): Boolean
Returns true
if the arguments are deeply equal to each other and false
otherwise. Two null
values are deeply equal. If both arguments are arrays, the algorithm in Arrays.deepEquals
is used to determine equality. Otherwise, equality is determined by using the equals
method of the first argument.
Parameters | |
---|---|
a |
Any?: an object |
b |
Any?: an object to be compared with a for deep equality |
Return | |
---|---|
Boolean |
true if the arguments are deeply equal to each other and false otherwise |
equals
static fun equals(
a: Any?,
b: Any?
): Boolean
Returns true
if the arguments are equal to each other and false
otherwise. Consequently, if both arguments are null
, true
is returned. Otherwise, if the first argument is not null
, equality is determined by calling the equals
method of the first argument with the second argument of this method. Otherwise, false
is returned.
Parameters | |
---|---|
a |
Any?: an object |
b |
Any?: an object to be compared with a for equality |
Return | |
---|---|
Boolean |
true if the arguments are equal to each other and false otherwise |
See Also
hash
static fun hash(vararg values: Any!): Int
Generates a hash code for a sequence of input values. The hash code is generated as if all the input values were placed into an array, and that array were hashed by calling java.util.Arrays#hashCode(java.lang.Object[])
.
This method is useful for implementing java.lang.Object#hashCode()
on objects containing multiple fields. For example, if an object that has three fields, x
, y
, and z
, one could write:
@Override public int hashCode() { return Objects.hash(x, y, z); }Warning: When a single object reference is supplied, the returned value does not equal the hash code of that object reference. This value can be computed by calling
hashCode(java.lang.Object)
.
Parameters | |
---|---|
values |
Any!: the values to be hashed |
Return | |
---|---|
Int |
a hash value of the sequence of input values |
See Also
hashCode
static fun hashCode(o: Any?): Int
Returns the hash code of a non-null
argument and 0 for a null
argument.
Parameters | |
---|---|
o |
Any?: an object |
Return | |
---|---|
Int |
the hash code of a non-null argument and 0 for a null argument |
See Also
isNull
static fun isNull(obj: Any?): Boolean
Returns true
if the provided reference is null
otherwise returns false
.
Parameters | |
---|---|
obj |
Any?: a reference to be checked against null |
Return | |
---|---|
Boolean |
true if the provided reference is null otherwise false |
See Also
nonNull
static fun nonNull(obj: Any?): Boolean
Returns true
if the provided reference is non-null
otherwise returns false
.
Parameters | |
---|---|
obj |
Any?: a reference to be checked against null |
Return | |
---|---|
Boolean |
true if the provided reference is non-null otherwise false |
See Also
requireNonNull
static fun <T : Any!> requireNonNull(obj: T?): T
Checks that the specified object reference is not null
. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
public Foo(Bar bar) { this.bar = Objects.requireNonNull(bar); }
Parameters | |
---|---|
obj |
T?: the object reference to check for nullity |
<T> |
the type of the reference |
Return | |
---|---|
T |
obj if not null |
Exceptions | |
---|---|
java.lang.NullPointerException |
if obj is null |
requireNonNull
static fun <T : Any!> requireNonNull(
obj: T?,
message: String
): T
Checks that the specified object reference is not null
and throws a customized NullPointerException
if it is. This method is designed primarily for doing parameter validation in methods and constructors with multiple parameters, as demonstrated below:
public Foo(Bar bar, Baz baz) { this.bar = Objects.requireNonNull(bar, "bar must not be null"); this.baz = Objects.requireNonNull(baz, "baz must not be null"); }
Parameters | |
---|---|
obj |
T?: the object reference to check for nullity |
message |
String: detail message to be used in the event that a NullPointerException is thrown |
<T> |
the type of the reference |
Return | |
---|---|
T |
obj if not null |
Exceptions | |
---|---|
java.lang.NullPointerException |
if obj is null |
requireNonNull
static fun <T : Any!> requireNonNull(
obj: T?,
messageSupplier: Supplier<String!>
): T
Checks that the specified object reference is not null
and throws a customized NullPointerException
if it is.
Unlike the method requireNonNull(java.lang.Object,java.lang.String)
, this method allows creation of the message to be deferred until after the null check is made. While this may confer a performance advantage in the non-null case, when deciding to call this method care should be taken that the costs of creating the message supplier are less than the cost of just creating the string message directly.
Parameters | |
---|---|
obj |
T?: the object reference to check for nullity |
messageSupplier |
Supplier<String!>: supplier of the detail message to be used in the event that a NullPointerException is thrown |
<T> |
the type of the reference |
Return | |
---|---|
T |
obj if not null |
Exceptions | |
---|---|
java.lang.NullPointerException |
if obj is null |
requireNonNullElse
static fun <T : Any!> requireNonNullElse(
obj: T?,
defaultObj: T
): T
Returns the first argument if it is non-null
and otherwise returns the non-null
second argument.
Parameters | |
---|---|
obj |
T?: an object |
defaultObj |
T: a non-null object to return if the first argument is null |
<T> |
the type of the reference |
Return | |
---|---|
T |
the first argument if it is non-null and otherwise the second argument if it is non-null |
Exceptions | |
---|---|
java.lang.NullPointerException |
if both obj is null and defaultObj is null |
requireNonNullElseGet
static fun <T : Any!> requireNonNullElseGet(
obj: T?,
supplier: Supplier<out T>
): T
Returns the first argument if it is non-null
and otherwise returns the non-null
value of supplier.get()
.
Parameters | |
---|---|
obj |
T?: an object |
supplier |
Supplier<out T>: of a non-null object to return if the first argument is null |
<T> |
the type of the first argument and return type |
Return | |
---|---|
T |
the first argument if it is non-null and otherwise the value from supplier.get() if it is non-null |
Exceptions | |
---|---|
java.lang.NullPointerException |
if both obj is null and either the supplier is null or the supplier.get() value is null |
toString
static fun toString(o: Any?): String
Returns the result of calling toString
for a non-null
argument and "null"
for a null
argument.
Parameters | |
---|---|
o |
Any?: an object |
Return | |
---|---|
String |
the result of calling toString for a non-null argument and "null" for a null argument |
toString
static fun toString(
o: Any?,
nullDefault: String
): String
Returns the result of calling toString
on the first argument if the first argument is not null
and returns the second argument otherwise.
Parameters | |
---|---|
o |
Any?: an object |
nullDefault |
String: string to return if the first argument is null |
Return | |
---|---|
String |
the result of calling toString on the first argument if it is not null and the second argument otherwise. |
See Also