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