-
public final class Preconditions
Simple static methods to be called at the start of your own methods to verify correct arguments and state.
-
-
Method Summary
Modifier and Type Method Description static void
checkArgument(boolean expression)
static void
checkArgument(boolean expression, @NonNull() Object errorMessage)
Ensures that an expression checking an argument is true. static <T extends CharSequence> T
checkStringNotEmpty(@Nullable() T string)
Ensures that an string reference passed as a parameter to the callingmethod is not empty. static <T extends CharSequence> T
checkStringNotEmpty(@Nullable() T string, @NonNull() Object errorMessage)
Ensures that an string reference passed as a parameter to the callingmethod is not empty. static <T extends CharSequence> T
checkStringNotEmpty(@Nullable() T string, @NonNull() String messageTemplate, @NonNull() Array<Object> messageArgs)
Ensures that an string reference passed as a parameter to the calling method is not empty. static <T> T
checkNotNull(@Nullable() T reference)
Ensures that an object reference passed as a parameter to the callingmethod is not null. static <T> T
checkNotNull(@Nullable() T reference, @NonNull() Object errorMessage)
Ensures that an object reference passed as a parameter to the callingmethod is not null. static void
checkState(boolean expression, @Nullable() String message)
Ensures the truth of an expression involving the state of the callinginstance, but not involving any parameters to the calling method. static void
checkState(boolean expression)
Ensures the truth of an expression involving the state of the callinginstance, but not involving any parameters to the calling method. static int
checkArgumentNonnegative(int value, @Nullable() String errorMessage)
Ensures that that the argument numeric value is non-negative. static int
checkArgumentNonnegative(int value)
Ensures that that the argument numeric value is non-negative. static int
checkArgumentInRange(int value, int lower, int upper, @NonNull() String valueName)
Ensures that the argument int value is within the inclusive range. -
-
Method Detail
-
checkArgument
static void checkArgument(boolean expression)
-
checkArgument
static void checkArgument(boolean expression, @NonNull() Object errorMessage)
Ensures that an expression checking an argument is true.
- Parameters:
expression
- the expression to checkerrorMessage
- the exception message to use if the check fails; willbe converted to a string using valueOf
-
checkStringNotEmpty
@NonNull() static <T extends CharSequence> T checkStringNotEmpty(@Nullable() T string)
Ensures that an string reference passed as a parameter to the callingmethod is not empty.
- Parameters:
string
- an string reference
-
checkStringNotEmpty
@NonNull() static <T extends CharSequence> T checkStringNotEmpty(@Nullable() T string, @NonNull() Object errorMessage)
Ensures that an string reference passed as a parameter to the callingmethod is not empty.
- Parameters:
string
- an string referenceerrorMessage
- the exception message to use if the check fails; willbe converted to a string using valueOf
-
checkStringNotEmpty
@NonNull() static <T extends CharSequence> T checkStringNotEmpty(@Nullable() T string, @NonNull() String messageTemplate, @NonNull() Array<Object> messageArgs)
Ensures that an string reference passed as a parameter to the calling method is not empty.
- Parameters:
string
- an string referencemessageTemplate
- a printf-style message template to use if the check fails; will beconverted to a string using formatmessageArgs
- arguments for{@code messageTemplate}
-
checkNotNull
@NonNull() static <T> T checkNotNull(@Nullable() T reference)
Ensures that an object reference passed as a parameter to the callingmethod is not null.
- Parameters:
reference
- an object reference
-
checkNotNull
@NonNull() static <T> T checkNotNull(@Nullable() T reference, @NonNull() Object errorMessage)
Ensures that an object reference passed as a parameter to the callingmethod is not null.
- Parameters:
reference
- an object referenceerrorMessage
- the exception message to use if the check fails; willbe converted to a string using valueOf
-
checkState
static void checkState(boolean expression, @Nullable() String message)
Ensures the truth of an expression involving the state of the callinginstance, but not involving any parameters to the calling method.
- Parameters:
expression
- a boolean expressionmessage
- exception message
-
checkState
static void checkState(boolean expression)
Ensures the truth of an expression involving the state of the callinginstance, but not involving any parameters to the calling method.
- Parameters:
expression
- a boolean expression
-
checkArgumentNonnegative
static int checkArgumentNonnegative(int value, @Nullable() String errorMessage)
Ensures that that the argument numeric value is non-negative.
- Parameters:
value
- a numeric int valueerrorMessage
- the exception message to use if the check fails
-
checkArgumentNonnegative
static int checkArgumentNonnegative(int value)
Ensures that that the argument numeric value is non-negative.
- Parameters:
value
- a numeric int value
-
checkArgumentInRange
static int checkArgumentInRange(int value, int lower, int upper, @NonNull() String valueName)
Ensures that the argument int value is within the inclusive range.
- Parameters:
value
- a int valuelower
- the lower endpoint of the inclusive rangeupper
- the upper endpoint of the inclusive rangevalueName
- the name of the argument to use if the check fails
-
-
-
-