@Private
@Unstable
public final class Preconditions
extends java.lang.Object
This class replaces guava.Preconditions which provides helpers
to validate the following conditions:
null obj causes a NullPointerException.IllegalArgumentException.IllegalStateException.IndexOutOfBoundsException.| Modifier and Type | Method | 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 errorMsg,
java.lang.Object... errorMsgArgs) |
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> msgSupplier) |
Preconditions that the expression involving one or more parameters to the calling method.
|
static <T> T |
checkNotNull(T obj) |
Preconditions that the specified argument is not
null,
throwing a NPE exception otherwise. |
static <T> T |
checkNotNull(T obj,
java.lang.Object errorMessage) |
Preconditions that the specified argument is not
null,
throwing a NPE exception otherwise. |
static <T> T |
checkNotNull(T obj,
java.lang.String message,
java.lang.Object... values) |
Preconditions that the specified argument is not
null,
throwing a NPE exception otherwise. |
static <T> T |
checkNotNull(T obj,
java.util.function.Supplier<java.lang.String> msgSupplier) |
Preconditions that the specified argument is not
null,
throwing a NPE exception otherwise. |
static void |
checkState(boolean expression) |
Ensures the truth of an expression involving the state of the calling instance
without involving any parameters to the calling method.
|
static void |
checkState(boolean expression,
java.lang.Object errorMessage) |
Ensures the truth of an expression involving the state of the calling instance
without involving any parameters to the calling method.
|
static void |
checkState(boolean expression,
java.lang.String errorMsg,
java.lang.Object... errorMsgArgs) |
Ensures the truth of an expression involving the state of the calling instance
without involving any parameters to the calling method.
|
static void |
checkState(boolean expression,
java.util.function.Supplier<java.lang.String> msgSupplier) |
Preconditions that the expression involving one or more parameters to the calling method.
|
public static <T> T checkNotNull(T obj)
Preconditions that the specified argument is not null,
throwing a NPE exception otherwise.
The message of the exception is "The validated object is null".
T - the object typeobj - the object to checkjava.lang.NullPointerException - if the object is nullcheckNotNull(Object, Object)public static <T> T checkNotNull(T obj,
java.lang.Object errorMessage)
Preconditions that the specified argument is not null,
throwing a NPE exception otherwise.
The message of the exception is errorMessage.
T - the object typeobj - the object to checkerrorMessage - the message associated with the NPEjava.lang.NullPointerException - if the object is nullcheckNotNull(Object, String, Object...)public static <T> T checkNotNull(T obj,
java.lang.String message,
java.lang.Object... values)
Preconditions that the specified argument is not null,
throwing a NPE exception otherwise.
The message of the exception is String.format(f, m).
T - the object typeobj - the object to checkmessage - the String.format(String, Object...)
exception message if valid. Otherwise,
the message is VALIDATE_IS_NOT_NULL_EX_MESSAGEvalues - the optional values for the formatted exception messagejava.lang.NullPointerException - if the object is nullcheckNotNull(Object, Supplier)public static <T> T checkNotNull(T obj,
java.util.function.Supplier<java.lang.String> msgSupplier)
null,
throwing a NPE exception otherwise.
The message of the exception is msgSupplier.get().
T - the object typeobj - the object to checkmsgSupplier - the Supplier.get() set the
exception message if valid. Otherwise,
the message is VALIDATE_IS_NOT_NULL_EX_MESSAGEnull for method chaining)java.lang.NullPointerException - if the object is nullpublic static void checkArgument(boolean expression)
expression - a boolean expressionjava.lang.IllegalArgumentException - if expression is falsepublic static void checkArgument(boolean expression,
java.lang.Object errorMessage)
expression - a boolean expressionerrorMessage - the exception message to use if the check fails; will be converted to a
string using String.valueOf(Object)java.lang.IllegalArgumentException - if expression is falsepublic static void checkArgument(boolean expression,
java.lang.String errorMsg,
java.lang.Object... errorMsgArgs)
The message of the exception is String.format(f, m).
expression - a boolean expressionerrorMsg - the String.format(String, Object...)
exception message if valid. Otherwise,
the message is CHECK_ARGUMENT_EX_MESSAGEerrorMsgArgs - the optional values for the formatted exception message.java.lang.IllegalArgumentException - if expression is falsepublic static void checkArgument(boolean expression,
java.util.function.Supplier<java.lang.String> msgSupplier)
The message of the exception is msgSupplier.get().
expression - a boolean expressionmsgSupplier - the Supplier.get() set the
exception message if valid. Otherwise,
the message is CHECK_ARGUMENT_EX_MESSAGEjava.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,
java.lang.Object errorMessage)
expression - a boolean expressionerrorMessage - the exception message to use if the check fails; will be converted to a
string using String.valueOf(Object)java.lang.IllegalStateException - if expression is falsepublic static void checkState(boolean expression,
java.lang.String errorMsg,
java.lang.Object... errorMsgArgs)
The message of the exception is String.format(f, m).
expression - a boolean expressionerrorMsg - the String.format(String, Object...)
exception message if valid. Otherwise,
the message is CHECK_STATE_EX_MESSAGEerrorMsgArgs - the optional values for the formatted exception message.java.lang.IllegalStateException - if expression is falsepublic static void checkState(boolean expression,
java.util.function.Supplier<java.lang.String> msgSupplier)
The message of the exception is msgSupplier.get().
expression - a boolean expressionmsgSupplier - the Supplier.get() set the
exception message if valid. Otherwise,
the message is CHECK_STATE_EX_MESSAGEjava.lang.IllegalStateException - if expression is falseCopyright © 2008–2025 Apache Software Foundation. All rights reserved.