public final class Objects extends Object
static utility methods for operating on
objects, or checking certain conditions before operation. These utilities
include null-safe or null-tolerant methods and methods
for checking if indexes or sub-range values are out-of-bounds.
API Note:
Static methods such as checkIndex(int, int),
checkFromToIndex(int, int, int), and checkFromIndexSize(int, int, int) are
provided for the convenience of checking if values corresponding to indexes
and sub-ranges are out-of-bounds.
| Modifier and Type | Method and Description |
|---|---|
static int |
checkFromIndexSize(int fromIndex,
int size,
int length)
Checks if the sub-range from
fromIndex (inclusive) to
fromIndex + size (exclusive) is within the bounds of range from
0 (inclusive) to length (exclusive). |
static long |
checkFromIndexSize(long fromIndex,
long size,
long length)
Checks if the sub-range from
fromIndex (inclusive) to
fromIndex + size (exclusive) is within the bounds of range from
0 (inclusive) to length (exclusive). |
static int |
checkFromToIndex(int fromIndex,
int toIndex,
int length)
Checks if the sub-range from
fromIndex (inclusive) to
toIndex (exclusive) is within the bounds of range from 0
(inclusive) to length (exclusive). |
static long |
checkFromToIndex(long fromIndex,
long toIndex,
long length)
Checks if the sub-range from
fromIndex (inclusive) to
toIndex (exclusive) is within the bounds of range from 0
(inclusive) to length (exclusive). |
static int |
checkIndex(int index,
int length)
Checks if the
index is within the bounds of the range from
0 (inclusive) to length (exclusive). |
static long |
checkIndex(long index,
long length)
Checks if the
index is within the bounds of the range from
0 (inclusive) to length (exclusive). |
static <T> T |
requireNonNullElse(T obj,
T defaultObj)
Returns the first argument if it is non-
null and otherwise
returns the non-null second argument. |
static <T> T |
requireNonNullElseGet(T obj,
Supplier<? extends T> supplier)
Returns the first argument if it is non-
null and otherwise
returns the non-null value of supplier.get(). |
public static <T> T requireNonNullElse(T obj,
T defaultObj)
null and otherwise
returns the non-null second argument.T - the type of the referenceobj - an objectdefaultObj - a non-null object to return if the first argument is
nullnull and otherwise the
second argument if it is non-nullNullPointerException - if both obj is null and defaultObj is
nullpublic static <T> T requireNonNullElseGet(T obj,
Supplier<? extends T> supplier)
null and otherwise
returns the non-null value of supplier.get().T - the type of the first argument and return typeobj - an objectsupplier - of a non-null object to return if the first argument
is nullnull and otherwise the
value from supplier.get() if it is non-nullNullPointerException - if both obj is null and either the supplier
is null or the supplier.get() value is
nullpublic static int checkIndex(int index,
int length)
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
inequalitiesindex - the indexlength - the upper-bound (exclusive) of the rangeindex if it is within bounds of the rangeIndexOutOfBoundsException - if the index is out-of-boundspublic static int checkFromToIndex(int fromIndex,
int toIndex,
int length)
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
inequalitiesfromIndex - the lower-bound (inclusive) of the sub-rangetoIndex - the upper-bound (exclusive) of the sub-rangelength - the upper-bound (exclusive) the rangefromIndex if the sub-range is within bounds of the rangeIndexOutOfBoundsException - if the sub-range is out-of-boundspublic static int checkFromIndexSize(int fromIndex,
int size,
int length)
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
inequalitiesfromIndex - the lower-bound (inclusive) of the sub-intervalsize - the size of the sub-rangelength - the upper-bound (exclusive) of the rangefromIndex if the sub-range is within bounds of the rangeIndexOutOfBoundsException - if the sub-range is out-of-boundspublic static long checkIndex(long index,
long length)
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 inequalitiesindex - the indexlength - the upper-bound (exclusive) of the rangeindex if it is within bounds of the rangeIndexOutOfBoundsException - if the index is out of boundspublic static long checkFromToIndex(long fromIndex,
long toIndex,
long length)
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 inequalitiesfromIndex - the lower-bound (inclusive) of the sub-rangetoIndex - the upper-bound (exclusive) of the sub-rangelength - the upper-bound (exclusive) the rangefromIndex if the sub-range within bounds of the rangeIndexOutOfBoundsException - if the sub-range is out of boundspublic static long checkFromIndexSize(long fromIndex,
long size,
long length)
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 inequalitiesfromIndex - the lower-bound (inclusive) of the sub-intervalsize - the size of the sub-rangelength - the upper-bound (exclusive) of the rangefromIndex if the sub-range within bounds of the rangeIndexOutOfBoundsException - if the sub-range is out of boundsCopyright © 2021. All rights reserved.