Class Preconditions
This class replaces guava.Preconditions which provides helpers
to validate the following conditions:
- An invalid
nullobj causes aNullPointerException. - An invalid argument causes an
IllegalArgumentException. - An invalid state causes an
IllegalStateException. - An invalid index causes an
IndexOutOfBoundsException.
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidcheckArgument(boolean expression) Ensures the truth of an expression involving one or more parameters to the calling method.static voidcheckArgument(boolean expression, Object errorMessage) Ensures the truth of an expression involving one or more parameters to the calling method.static voidcheckArgument(boolean expression, String errorMsg, Object... errorMsgArgs) Ensures the truth of an expression involving one or more parameters to the calling method.static voidcheckArgument(boolean expression, Supplier<String> msgSupplier) Preconditions that the expression involving one or more parameters to the calling method.static <T> TcheckNotNull(T obj) Preconditions that the specified argument is notnull, throwing a NPE exception otherwise.static <T> TcheckNotNull(T obj, Object errorMessage) Preconditions that the specified argument is notnull, throwing a NPE exception otherwise.static <T> TcheckNotNull(T obj, String message, Object... values) Preconditions that the specified argument is notnull, throwing a NPE exception otherwise.static <T> TcheckNotNull(T obj, Supplier<String> msgSupplier) Preconditions that the specified argument is notnull, throwing a NPE exception otherwise.static voidcheckState(boolean expression) Ensures the truth of an expression involving the state of the calling instance without involving any parameters to the calling method.static voidcheckState(boolean expression, Object errorMessage) Ensures the truth of an expression involving the state of the calling instance without involving any parameters to the calling method.static voidcheckState(boolean expression, String errorMsg, Object... errorMsgArgs) Ensures the truth of an expression involving the state of the calling instance without involving any parameters to the calling method.static voidcheckState(boolean expression, Supplier<String> msgSupplier) Preconditions that the expression involving one or more parameters to the calling method.
-
Method Details
-
checkNotNull
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".
- Type Parameters:
T- the object type- Parameters:
obj- the object to check- Returns:
- the validated object
- Throws:
NullPointerException- if the object isnull- See Also:
-
checkNotNull
Preconditions that the specified argument is not
null, throwing a NPE exception otherwise.The message of the exception is
errorMessage.- Type Parameters:
T- the object type- Parameters:
obj- the object to checkerrorMessage- the message associated with the NPE- Returns:
- the validated object
- Throws:
NullPointerException- if the object isnull- See Also:
-
checkNotNull
Preconditions that the specified argument is not
null, throwing a NPE exception otherwise.The message of the exception is
String.format(f, m).- Type Parameters:
T- the object type- Parameters:
obj- the object to checkmessage- theString.format(String, Object...)exception message if valid. Otherwise, the message isVALIDATE_IS_NOT_NULL_EX_MESSAGEvalues- the optional values for the formatted exception message- Returns:
- the validated object
- Throws:
NullPointerException- if the object isnull- See Also:
-
checkNotNull
Preconditions that the specified argument is notnull, throwing a NPE exception otherwise.The message of the exception is
msgSupplier.get().- Type Parameters:
T- the object type- Parameters:
obj- the object to checkmsgSupplier- theSupplier.get()set the exception message if valid. Otherwise, the message isVALIDATE_IS_NOT_NULL_EX_MESSAGE- Returns:
- the validated object (never
nullfor method chaining) - Throws:
NullPointerException- if the object isnull
-
checkArgument
public static void checkArgument(boolean expression) Ensures the truth of an expression involving one or more parameters to the calling method.- Parameters:
expression- a boolean expression- Throws:
IllegalArgumentException- ifexpressionis false
-
checkArgument
Ensures the truth of an expression involving one or more parameters to the calling method.- Parameters:
expression- a boolean expressionerrorMessage- the exception message to use if the check fails; will be converted to a string usingString.valueOf(Object)- Throws:
IllegalArgumentException- ifexpressionis false
-
checkArgument
Ensures the truth of an expression involving one or more parameters to the calling method.The message of the exception is
String.format(f, m).- Parameters:
expression- a boolean expressionerrorMsg- theString.format(String, Object...)exception message if valid. Otherwise, the message isCHECK_ARGUMENT_EX_MESSAGEerrorMsgArgs- the optional values for the formatted exception message.- Throws:
IllegalArgumentException- ifexpressionis false
-
checkArgument
Preconditions that the expression involving one or more parameters to the calling method.The message of the exception is
msgSupplier.get().- Parameters:
expression- a boolean expressionmsgSupplier- theSupplier.get()set the exception message if valid. Otherwise, the message isCHECK_ARGUMENT_EX_MESSAGE- Throws:
IllegalArgumentException- ifexpressionis false
-
checkState
public 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.- Parameters:
expression- a boolean expression- Throws:
IllegalStateException- ifexpressionis false
-
checkState
Ensures the truth of an expression involving the state of the calling instance without involving any parameters to the calling method.- Parameters:
expression- a boolean expressionerrorMessage- the exception message to use if the check fails; will be converted to a string usingString.valueOf(Object)- Throws:
IllegalStateException- ifexpressionis false
-
checkState
Ensures the truth of an expression involving the state of the calling instance without involving any parameters to the calling method.The message of the exception is
String.format(f, m).- Parameters:
expression- a boolean expressionerrorMsg- theString.format(String, Object...)exception message if valid. Otherwise, the message isCHECK_STATE_EX_MESSAGEerrorMsgArgs- the optional values for the formatted exception message.- Throws:
IllegalStateException- ifexpressionis false
-
checkState
Preconditions that the expression involving one or more parameters to the calling method.The message of the exception is
msgSupplier.get().- Parameters:
expression- a boolean expressionmsgSupplier- theSupplier.get()set the exception message if valid. Otherwise, the message isCHECK_STATE_EX_MESSAGE- Throws:
IllegalStateException- ifexpressionis false
-