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.lang.String template,
java.lang.Object... args)
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 <T> T |
checkArgument(T value,
java.util.function.Predicate<T> predicate)
Evaluates a value against a provided
Predicate expression |
static <T> T |
checkArgument(T value,
java.util.function.Predicate<T> predicate,
java.lang.Object errorMessage)
Evaluates a value against a provided
Predicate expression |
static <T> T |
checkArgument(T value,
java.util.function.Predicate<T> predicate,
java.lang.String template,
java.lang.Object... args)
Evaluates a value against a provided
Predicate expression |
static <T> T |
checkArgument(T value,
java.util.function.Predicate<T> predicate,
java.util.function.Supplier<java.lang.String> errorMessageSupplier)
Evaluates a value against a provided
Predicate expression |
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.lang.String template,
java.lang.Object... args)
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.
|
static <T> T |
checkState(T value,
java.util.function.Predicate<T> predicate)
Evaluates a value against a provided
Predicate expression |
static <T> T |
checkState(T value,
java.util.function.Predicate<T> predicate,
java.lang.Object errorMessage)
Evaluates a value against a provided
Predicate expression |
static <T> T |
checkState(T value,
java.util.function.Predicate<T> predicate,
java.lang.String template,
java.lang.Object... args)
Evaluates a value against a provided
Predicate expression |
static <T> T |
checkState(T value,
java.util.function.Predicate<T> predicate,
java.util.function.Supplier<java.lang.String> errorMessageSupplier)
Evaluates a value against a provided
Predicate expression |
public static void checkArgument(boolean expression)
expression - A boolean expressionjava.lang.IllegalArgumentException - If expression is falsepublic static <T> T checkArgument(@Nullable
T value,
java.util.function.Predicate<T> predicate)
Predicate expressionT - The type of the value being evaluatedvalue - An assignable value to evaluatepredicate - An operation to evaluate against valuevalue if the provided predicate evaluates to truejava.lang.IllegalArgumentException - If predicate evaluates to 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 <T> T checkArgument(@Nullable
T value,
java.util.function.Predicate<T> predicate,
@Nullable
java.lang.Object errorMessage)
Predicate expressionT - The type of the value being evaluatedvalue - An assignable value to evaluatepredicate - An operation to evaluate against valueerrorMessage - The message to provide in the exception, if the check fails; will be converted via
String.valueOf(java.lang.Object)value if the provided predicate evaluates to truejava.lang.IllegalArgumentException - If predicate evaluates to 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 <T> T checkArgument(@Nullable
T value,
java.util.function.Predicate<T> predicate,
java.util.function.Supplier<java.lang.String> errorMessageSupplier)
Predicate expression
Preferred to checkArgument(Object, Predicate, Object) when elements needs to be substituted or otherwise
processed for performance reasons
T - The type of the value being evaluatedvalue - An assignable value to evaluatepredicate - An operation to evaluate against valueerrorMessageSupplier - A supplier for the message to provide in the exception, if the check fails; Will not be invoked if
predicate is truevalue if the provided predicate evaluates to truejava.lang.IllegalArgumentException - If predicate evaluates to falsepublic static void checkArgument(boolean expression,
@Nullable
java.lang.String template,
@Nullable
java.lang.Object... args)
Preferred to checkArgument(boolean, Object) when elements needs to be substituted or otherwise processed
for performance reasons
expression - A boolean expressiontemplate - A template for a formatted message. The message is formed by replacing each %s placeholder in
the template with an argument. These are matched by position - the first %s gets
args[0], etc. Unmatched arguments will be appended to the formatted message in square braces.
Unmatched placeholders will be left as-is.args - The arguments to be substituted into the message template. Arguments are converted to strings using
String.valueOf(Object).java.lang.IllegalArgumentException - If expression is falsepublic static <T> T checkArgument(@Nullable
T value,
java.util.function.Predicate<T> predicate,
@Nullable
java.lang.String template,
@Nullable
java.lang.Object... args)
Predicate expression
Preferred to checkArgument(Object, Predicate, Object) when elements needs to be substituted or otherwise
processed for performance reasons
T - The type of the value being evaluatedvalue - An assignable value to evaluatepredicate - An operation to evaluate against valuetemplate - A template for a formatted message. The message is formed by replacing each %s placeholder in
the template with an argument. These are matched by position - the first %s gets
args[0], etc. Unmatched arguments will be appended to the formatted message in square braces.
Unmatched placeholders will be left as-is.args - The arguments to be substituted into the message template. Arguments are converted to strings using
String.valueOf(Object).value if the provided predicate evaluates to truejava.lang.IllegalArgumentException - If predicate evaluates to falsepublic static void checkState(boolean expression)
expression - A boolean expressionjava.lang.IllegalStateException - If expression is falsepublic static <T> T checkState(@Nullable
T value,
java.util.function.Predicate<T> predicate)
Predicate expressionT - The type of the value being evaluatedvalue - An assignable value to evaluatepredicate - An operation to evaluate against valuevalue if the provided predicate evaluates to truejava.lang.IllegalStateException - If predicate evaluates to 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 <T> T checkState(@Nullable
T value,
java.util.function.Predicate<T> predicate,
@Nullable
java.lang.Object errorMessage)
Predicate expressionT - The type of the value being evaluatedvalue - An assignable value to evaluatepredicate - An operation to evaluate against valueerrorMessage - The message to provide in the exception, if the check fails; will be converted via
String.valueOf(java.lang.Object)value if the provided predicate evaluates to truejava.lang.IllegalStateException - If predicate evaluates to falsepublic static void checkState(boolean expression,
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 falsepublic static <T> T checkState(@Nullable
T value,
java.util.function.Predicate<T> predicate,
java.util.function.Supplier<java.lang.String> errorMessageSupplier)
Predicate expression
Preferred to checkArgument(Object, Predicate, Object) when elements needs to be substituted or otherwise
processed for performance reasons
T - The type of the value being evaluatedvalue - An assignable value to evaluatepredicate - An operation to evaluate against valueerrorMessageSupplier - A supplier for the message to provide in the exception, if the check fails; Will not be invoked if
predicate is truevalue if the provided predicate evaluates to truejava.lang.IllegalStateException - If predicate evaluates to falsepublic static void checkState(boolean expression,
@Nullable
java.lang.String template,
@Nullable
java.lang.Object... args)
Preferred to checkState(boolean, Object) when elements needs to be substituted or otherwise processed
for performance reasons
expression - A boolean expressiontemplate - A template for a formatted message. The message is formed by replacing each %s placeholder in
the template with an argument. These are matched by position - the first %s gets
args[0], etc. Unmatched arguments will be appended to the formatted message in square braces.
Unmatched placeholders will be left as-is.args - The arguments to be substituted into the message template. Arguments are converted to strings using
String.valueOf(Object).java.lang.IllegalStateException - If expression is falsepublic static <T> T checkState(@Nullable
T value,
java.util.function.Predicate<T> predicate,
@Nullable
java.lang.String template,
@Nullable
java.lang.Object... args)
Predicate expression
Preferred to checkArgument(Object, Predicate, Object) when elements needs to be substituted or otherwise
processed for performance reasons
T - The type of the value being evaluatedvalue - An assignable value to evaluatepredicate - An operation to evaluate against valuetemplate - A template for a formatted message. The message is formed by replacing each %s placeholder in
the template with an argument. These are matched by position - the first %s gets
args[0], etc. Unmatched arguments will be appended to the formatted message in square braces.
Unmatched placeholders will be left as-is.args - The arguments to be substituted into the message template. Arguments are converted to strings using
String.valueOf(Object).value if the provided predicate evaluates to truejava.lang.IllegalStateException - If predicate evaluates to false