public final class Preconditions
extends java.lang.Object
In general, these utilities allow code of the form
if (!expression) {
throw new SomeException();
}
to be replaced with
Preconditions.check*(expression);for compactness, behavioral clarity, and readability
It is highly recommended to constrain use of these utilities to verifying possible failure conditions which are the caller's fault, as otherwise behavior may be confusing in stack traces, and to future readers of caller implementations
For failure messages which may have non-trivial performance costs to create, it is highly recommended to use method
forms which accept a Supplier
| Modifier and Type | Method and Description |
|---|---|
static void |
checkArgument(boolean expression)
Ensures the truth of an expression involving one or more parameters to the calling method.
|
static void |
checkArgument(boolean expression,
java.lang.Object errorMessage)
Ensures the truth of an expression involving one or more parameters to the calling method.
|
static void |
checkArgument(boolean expression,
java.util.function.Supplier<java.lang.String> errorMessageSupplier)
Ensures the truth of an expression involving one or more parameters to the calling method.
|
static void |
checkState(boolean expression)
Ensures the truth of an expression involving one or more parameters to the calling method.
|
static void |
checkState(boolean expression,
java.lang.Object errorMessage)
Ensures the truth of an expression involving one or more parameters to the calling method.
|
static void |
checkState(boolean expression,
java.util.function.Supplier<java.lang.String> errorMessageSupplier)
Ensures the truth of an expression involving one or more parameters to the calling method.
|
public static void checkArgument(boolean expression)
expression - A boolean expressionjava.lang.IllegalArgumentException - If expression is falsepublic static void checkArgument(boolean expression,
@Nullable
java.lang.Object errorMessage)
expression - A boolean expressionerrorMessage - The message to provide in the exception, if the check fails; will be converted via
String.valueOf(java.lang.Object)java.lang.IllegalArgumentException - If expression is falsepublic static void checkArgument(boolean expression,
java.util.function.Supplier<java.lang.String> errorMessageSupplier)
Preferred to checkArgument(boolean, Object) when elements needs to be substituted or otherwise processed
for performance reasons
expression - A boolean expressionerrorMessageSupplier - A supplier for the message to provide in the exception, if the check fails; Will not be invoked if
expression is truejava.lang.IllegalArgumentException - If expression is falsepublic static void checkState(boolean expression)
expression - A boolean expressionjava.lang.IllegalStateException - If expression is falsepublic static void checkState(boolean expression,
@Nullable
java.lang.Object errorMessage)
expression - A boolean expressionerrorMessage - The message to provide in the exception, if the check fails; will be converted via
String.valueOf(java.lang.Object)java.lang.IllegalStateException - If expression is falsepublic static void checkState(boolean expression,
@Nullable
java.util.function.Supplier<java.lang.String> errorMessageSupplier)
Preferred to checkState(boolean, Object) when elements needs to be substituted or otherwise processed
for performance reasons
expression - A boolean expressionerrorMessageSupplier - A supplier for the message to provide in the exception, if the check fails; Will not be invoked if
expression is truejava.lang.IllegalStateException - If expression is false