public class Preconditions extends Object
| 限定符和类型 | 方法和说明 |
|---|---|
static void |
checkArgument(boolean expression) |
static void |
checkArgument(boolean expression,
String errorMessage) |
static void |
checkArgument(boolean expression,
Supplier<Object[],String> errorMessageSupplier,
Object... params) |
static <X extends RuntimeException> |
checkFromIndexSize(int fromIndex,
int size,
int length,
Function2<String,List<Integer>,X> oobef)
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 <X extends RuntimeException> |
checkFromToIndex(int fromIndex,
int toIndex,
int length,
Function2<String,List<Integer>,X> oobef)
Checks if the sub-range from
fromIndex (inclusive) to
toIndex (exclusive) is within the bounds of range from 0
(inclusive) to length (exclusive). |
static <X extends RuntimeException> |
checkIndex(int index,
int length) |
static <X extends RuntimeException> |
checkIndex(int index,
int length,
Function2<String,List<Integer>,X> oobef)
Checks if the
index is within the bounds of the range from
0 (inclusive) to length (exclusive). |
static <T> T |
checkNotEmpty(T obj) |
static <T> T |
checkNotEmpty(T obj,
String errorMessage) |
static <T> T |
checkNotEmpty(T obj,
Supplier<Object[],String> errorMessageSupplier,
Object... params) |
static <T> T |
checkNotNull(T obj) |
static <T> T |
checkNotNull(T obj,
String errorMessage) |
static <T> T |
checkNotNull(T obj,
Supplier<Object[],String> errorMessageSupplier,
Object... params) |
static void |
checkState(boolean expression)
Ensures that the state expression is true.
|
static void |
checkTrue(boolean expression) |
static void |
checkTrue(boolean expression,
String errorMessage) |
static void |
checkTrue(boolean expression,
Supplier<Object[],String> errorMessageSupplier,
Object... params) |
static <X extends RuntimeException> |
outOfBoundsExceptionFormatter(Function<String,X> f)
Returns an out-of-bounds exception formatter from an given exception
factory.
|
static <T> T |
test(Predicate<T> predicate,
T argument) |
static <T> T |
test(Predicate<T> predicate,
T argument,
String message) |
static <T> T |
test(Predicate<T> predicate,
T argument,
Supplier<Object[],String> messageSupplier,
Object... params) |
public static <T> T test(@NonNull Predicate<T> predicate, @Nullable T argument, String message)
public static <T> T test(@NonNull Predicate<T> predicate, @Nullable T argument, Supplier<Object[],String> messageSupplier, Object... params)
public static <T> T checkNotNull(@NonNull T obj)
public static <T> T checkNotNull(@NonNull T obj, @Nullable Supplier<Object[],String> errorMessageSupplier, Object... params)
public static <T> T checkNotEmpty(@Nullable T obj)
public static <T> T checkNotEmpty(@NonNull T obj, @Nullable Supplier<Object[],String> errorMessageSupplier, Object... params)
public static void checkState(boolean expression)
public static void checkArgument(boolean expression)
public static void checkArgument(boolean expression,
String errorMessage)
public static void checkArgument(boolean expression,
Supplier<Object[],String> errorMessageSupplier,
Object... params)
public static void checkTrue(boolean expression)
public static void checkTrue(boolean expression,
String errorMessage)
public static void checkTrue(boolean expression,
Supplier<Object[],String> errorMessageSupplier,
Object... params)
public static <X extends RuntimeException> Function2<String,List<Integer>,X> outOfBoundsExceptionFormatter(Function<String,X> f)
The exception formatter accepts two arguments: a String
describing the out-of-bounds range check that failed, referred to as the
check kind; and a List<Integer> containing the
out-of-bound integer values that failed the check. The list of
out-of-bound values is not modified.
Three check kinds are supported checkIndex,
checkFromToIndex and checkFromIndexSize corresponding
respectively to the specified application of an exception formatter as an
argument to the out-of-bounds range check methods
checkIndex(int, int, Function2) checkIndex},
checkFromToIndex, and
checkFromIndexSize.
Thus a supported check kind corresponds to a method name and the
out-of-bound integer values correspond to method argument values, in
order, preceding the exception formatter argument (similar in many
respects to the form of arguments required for a reflective invocation of
such a range check method).
Formatter arguments conforming to such supported check kinds will
produce specific exception messages describing failed out-of-bounds
checks. Otherwise, more generic exception messages will be produced in
any of the following cases: the check kind is supported but fewer
or more out-of-bounds values are supplied, the check kind is not
supported, the check kind is null, or the list of out-of-bound
values is null.
This method produces an out-of-bounds exception formatter that can be
passed as an argument to any of the supported out-of-bounds range check
methods declared by Objects. For example, a formatter producing
an ArrayIndexOutOfBoundsException may be produced and stored on a
static final field as follows:
static final
BiFunction<String, List<Integer>, ArrayIndexOutOfBoundsException> AIOOBEF =
outOfBoundsExceptionFormatter(ArrayIndexOutOfBoundsException::new);
The formatter instance AIOOBEF may be passed as an argument to an
out-of-bounds range check method, such as checking if an index
is within the bounds of a limit:
checkIndex(index, limit, AIOOBEF);
If the bounds check fails then the range check method will throw an
ArrayIndexOutOfBoundsException with an appropriate exception
message that is a produced from AIOOBEF as follows:
AIOOBEF.apply("checkIndex", List.of(index, limit));
X - the type of runtime exception to be returned by the given
exception factory and relayed by the exception formatterf - the exception factory, that produces an exception from a message
where the message is produced and formatted by the returned
exception formatter. If this factory is stateless and side-effect
free then so is the returned formatter.
Exceptions thrown by the factory are relayed to the caller
of the returned formatter.public static <X extends RuntimeException> int checkIndex(int index, int length)
public static <X extends RuntimeException> int checkIndex(int index, int length, Function2<String,List<Integer>,X> oobef)
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 inequalitiesIf the index is out of bounds, then a runtime exception is
thrown that is the result of applying the following arguments to the
exception formatter: the name of this method, checkIndex;
and an unmodifiable list integers whose values are, in order, the
out-of-bounds arguments index and length.
X - the type of runtime exception to throw if the arguments are
out of boundsindex - the indexlength - the upper-bound (exclusive) of the rangeoobef - the exception formatter that when applied with this
method name and out-of-bounds arguments returns a runtime
exception. If null or returns null then, it is as
if an exception formatter produced from an invocation of
outOfBoundsExceptionFormatter(IndexOutOfBounds::new) is used
instead (though it may be more efficient).
Exceptions thrown by the formatter are relayed to the caller.index if it is within bounds of the rangeX - if the index is out of bounds and the exception
formatter is non-nullIndexOutOfBoundsException - if the index is out of bounds
and the exception formatter is null
Note: This method is made intrinsic in optimizing compilers to guide them to
perform unsigned comparisons of the index and length when it is known the
length is a non-negative value (such as that of an array length or from
the upper bound of a loop)public static <X extends RuntimeException> int checkFromToIndex(int fromIndex, int toIndex, int length, Function2<String,List<Integer>,X> oobef)
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 inequalitiesIf the sub-range is out of bounds, then a runtime exception is
thrown that is the result of applying the following arguments to the
exception formatter: the name of this method, checkFromToIndex;
and an unmodifiable list integers whose values are, in order, the
out-of-bounds arguments fromIndex, toIndex, and length.
X - the type of runtime exception to throw if the arguments are
out of boundsfromIndex - the lower-bound (inclusive) of the sub-rangetoIndex - the upper-bound (exclusive) of the sub-rangelength - the upper-bound (exclusive) the rangeoobef - the exception formatter that when applied with this
method name and out-of-bounds arguments returns a runtime
exception. If null or returns null then, it is as
if an exception formatter produced from an invocation of
outOfBoundsExceptionFormatter(IndexOutOfBounds::new) is used
instead (though it may be more efficient).
Exceptions thrown by the formatter are relayed to the caller.fromIndex if the sub-range within bounds of the rangeX - if the sub-range is out of bounds and the exception factory
function is non-nullIndexOutOfBoundsException - if the sub-range is out of bounds and
the exception factory function is nullpublic static <X extends RuntimeException> int checkFromIndexSize(int fromIndex, int size, int length, Function2<String,List<Integer>,X> oobef)
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 inequalitiesIf the sub-range is out of bounds, then a runtime exception is
thrown that is the result of applying the following arguments to the
exception formatter: the name of this method, checkFromIndexSize;
and an unmodifiable list integers whose values are, in order, the
out-of-bounds arguments fromIndex, size, and
length.
X - the type of runtime exception to throw if the arguments are
out of boundsfromIndex - the lower-bound (inclusive) of the sub-intervalsize - the size of the sub-rangelength - the upper-bound (exclusive) of the rangeoobef - the exception formatter that when applied with this
method name and out-of-bounds arguments returns a runtime
exception. If null or returns null then, it is as
if an exception formatter produced from an invocation of
outOfBoundsExceptionFormatter(IndexOutOfBounds::new) is used
instead (though it may be more efficient).
Exceptions thrown by the formatter are relayed to the caller.fromIndex if the sub-range within bounds of the rangeX - if the sub-range is out of bounds and the exception factory
function is non-nullIndexOutOfBoundsException - if the sub-range is out of bounds and
the exception factory function is nullCopyright © 2020. All rights reserved.