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>){@return 0 if the arguments are identical and {@code * c. |
| static Boolean |
deepEquals(a: Any?, b: Any?){@return {@code true} if the arguments are deeply equal to each other * and {@code false} otherwise} Two |
| static Boolean |
{@return {@code true} if the arguments are equal to each other * and {@code false} otherwise} Consequently, if both arguments are |
| static Int |
{@return 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 |
| static Int |
{@return the hash code of a non-{@code null} argument and 0 for * a {@code null} argument} |
| static Boolean |
{@return {@code true} if the provided reference is {@code * null}; {@code false} otherwise} |
| static Boolean |
{@return {@code true} if the provided reference is non-{@code null}; * {@code false} otherwise} |
| 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){@return the first argument if it is non-{@code null} and * otherwise the second argument if it is non-{@code null}} |
| static T |
requireNonNullElseGet(obj: T?, supplier: Supplier<out T>){@return the first argument if it is non-{@code null} and * otherwise the value from {@code supplier. |
| static String |
toIdentityString(o: Any){@return a string equivalent to the string returned by {@code * Object. |
| static String |
{@return the result of calling {@code toString} for a * non-{@code null} argument and {@code "null"} for a * {@code null} argument} |
| static String |
{@return the result of calling {@code toString} on the first * argument if the first argument is not {@code null} and the * second argument otherwise} |
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 < 0size < 0fromIndex + 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 < 0size < 0fromIndex + 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 < 0fromIndex > toIndextoIndex > lengthlength < 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 < 0fromIndex > toIndextoIndex > lengthlength < 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 < 0index >= lengthlength < 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 < 0index >= lengthlength < 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
{@return 0 if the arguments are identical and {@code * 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 |
See Also
deepEquals
static fun deepEquals(
a: Any?,
b: Any?
): Boolean
{@return {@code true} if the arguments are deeply equal to each other * and {@code 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 |
equals
static fun equals(
a: Any?,
b: Any?
): Boolean
{@return {@code true} if the arguments are equal to each other * and {@code 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 |
hash
static fun hash(vararg values: Any!): Int
{@return 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 Arrays.hashCode(Object[]).
This method is useful for implementing 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); }
hashCode(Object).
| Parameters | |
|---|---|
values |
Any!: the values to be hashed |
hashCode
static fun hashCode(o: Any?): Int
{@return the hash code of a non-{@code null} argument and 0 for * a {@code null} argument}
| Parameters | |
|---|---|
o |
Any?: an object |
See Also
isNull
static fun isNull(obj: Any?): Boolean
{@return {@code true} if the provided reference is {@code * null}; {@code false} otherwise}
| Parameters | |
|---|---|
obj |
Any?: a reference to be checked against null |
See Also
nonNull
static fun nonNull(obj: Any?): Boolean
{@return {@code true} if the provided reference is non-{@code null}; * {@code false} otherwise}
| Parameters | |
|---|---|
obj |
Any?: a reference to be checked against null |
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 | |
|---|---|
<T> |
the type of the reference |
obj |
T?: the object reference to check for nullity |
| 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 | |
|---|---|
<T> |
the type of the reference |
obj |
T?: the object reference to check for nullity |
message |
String: detail message to be used in the event that a NullPointerException is thrown |
| 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(Object,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 | |
|---|---|
<T> |
the type of the reference |
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 |
| 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
{@return the first argument if it is non-{@code null} and * otherwise the second argument if it is non-{@code null}}
| Parameters | |
|---|---|
<T> |
the type of the reference |
obj |
T?: an object |
defaultObj |
T: a non-null object to return if the first argument is 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
{@return the first argument if it is non-{@code null} and * otherwise the value from {@code supplier.get()} if it is * non-{@code null}}
| Parameters | |
|---|---|
<T> |
the type of the first argument and return type |
obj |
T?: an object |
supplier |
Supplier<out T>: of a non-null object to return if the first argument is null |
| Exceptions | |
|---|---|
java.lang.NullPointerException |
if both obj is null and either the supplier is null or the supplier.get() value is null |
toIdentityString
static fun toIdentityString(o: Any): String
{@return a string equivalent to the string returned by {@code * Object.toString} if that method and {@code hashCode} are not * overridden}
| Parameters | |
|---|---|
o |
Any: an object |
| Exceptions | |
|---|---|
java.lang.NullPointerException |
if the argument is null |
toString
static fun toString(o: Any?): String
{@return the result of calling {@code toString} for a * non-{@code null} argument and {@code "null"} for a * {@code null} argument}
| Parameters | |
|---|---|
o |
Any?: an object |
toString
static fun toString(
o: Any?,
nullDefault: String
): String
{@return the result of calling {@code toString} on the first * argument if the first argument is not {@code null} and the * second argument otherwise}
| Parameters | |
|---|---|
o |
Any?: an object |
nullDefault |
String: string to return if the first argument is null |
See Also