@ParametersAreNonnullByDefault public final class Preconditions extends Object
boolean expression which is expected to
be true (or in the case of checkNotNull, an object reference which is expected to be non-null). When
false (or null) is passed instead, the Preconditions method throws an unchecked exception,
which helps the calling method communicate to its caller that that caller has made a mistake.
Warning: The goal of this class is to improve readability of code, but in some circumstances this may come at
a significant performance cost. Remember that parameter values for message construction must all be computed eagerly,
and autoboxing and varargs array creation may happen as well, even when the precondition check then succeeds (as it
should almost always do in production). In some circumstances these wasted CPU cycles and allocations can add up to a
real problem.| 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,
String message)
Ensures the truth of an expression involving one or more parameters to the calling method.
|
static void |
checkArgument(boolean expression,
String pattern,
Object... args)
Ensures the truth of an expression involving one or more parameters to the calling method.
|
static int |
checkElementIndex(int index,
int size)
Ensures that
index specifies a valid element in an array, list or string of size. |
static <T> T |
checkEqualTo(T value,
T other)
Ensures that a
value passed as a parameter to the calling method is equal to other. |
static <T> T |
checkEqualTo(T value,
T other,
String message)
Ensures that a
value passed as a parameter to the calling method is equal to other. |
static <T> T |
checkEqualTo(T value,
T other,
String pattern,
Object... args)
Ensures that a
value passed as a parameter to the calling method is equal to other. |
static <C extends Comparable<C>> |
checkGreaterThan(C value,
C lowerBoundExclusive)
Ensures that a
value passed as a parameter to the calling method is strictly greater than lowerBoundExclusive. |
static <C extends Comparable<C>> |
checkGreaterThan(C value,
C lowerBoundExclusive,
String message)
Ensures that a
value passed as a parameter to the calling method is strictly greater than lowerBoundExclusive. |
static <C extends Comparable<C>> |
checkGreaterThan(C value,
C lowerBoundExclusive,
String pattern,
Object... args)
Ensures that a
value passed as a parameter to the calling method is strictly greater than lowerBoundExclusive. |
static <C extends Comparable<C>> |
checkGreaterThanOrEqualTo(C value,
C lowerBoundInclusive)
Ensures that a
value passed as a parameter to the calling method is greater than or equal to lowerBoundInclusive. |
static <C extends Comparable<C>> |
checkGreaterThanOrEqualTo(C value,
C lowerBoundInclusive,
String message)
Ensures that a
value passed as a parameter to the calling method is greater than or equal to lowerBoundInclusive. |
static <C extends Comparable<C>> |
checkGreaterThanOrEqualTo(C value,
C lowerBoundInclusive,
String pattern,
Object... args)
Ensures that a
value passed as a parameter to the calling method is greater than or equal to lowerBoundInclusive. |
static <T> T |
checkInstanceOf(T reference,
Class<?> type)
Ensures that an object
reference passed as a parameter to the calling method is instance of type. |
static <T> T |
checkInstanceOf(T reference,
Class<?> type,
String message)
Ensures that an object
reference passed as a parameter to the calling method is instance of type. |
static <T> T |
checkInstanceOf(T reference,
Class<?> type,
String pattern,
Object... args)
Ensures that an object
reference passed as a parameter to the calling method is instance of type. |
static <C extends Comparable<C>> |
checkLessThan(C value,
C upperBoundExclusive)
Ensures that a
value passed as a parameter to the calling method is strictly less than upperBoundExclusive. |
static <C extends Comparable<C>> |
checkLessThan(C value,
C upperBoundExclusive,
String message)
Ensures that a
value passed as a parameter to the calling method is strictly less than upperBoundExclusive. |
static <C extends Comparable<C>> |
checkLessThan(C value,
C upperBoundExclusive,
String pattern,
Object... args)
Ensures that a
value passed as a parameter to the calling method is strictly less than upperBoundExclusive. |
static <C extends Comparable<C>> |
checkLessThanOrEqualTo(C value,
C upperBoundInclusive)
Ensures that a
value passed as a parameter to the calling method is less than or equal to upperBoundInclusive. |
static <C extends Comparable<C>> |
checkLessThanOrEqualTo(C value,
C upperBoundInclusive,
String message)
Ensures that a
value passed as a parameter to the calling method is less than or equal to upperBoundInclusive. |
static <C extends Comparable<C>> |
checkLessThanOrEqualTo(C value,
C upperBoundInclusive,
String pattern,
Object... args)
Ensures that a
value passed as a parameter to the calling method is less than or equal to upperBoundInclusive. |
static <C extends Collection<? extends T>,T> |
checkNotContainsNull(C collection)
Ensures that a
collection passed as a parameter to the calling method is not null and does not
contains any null element. |
static <C extends Collection<? extends T>,T> |
checkNotContainsNull(C collection,
String message)
Ensures that a
collection passed as a parameter to the calling method is not null and does not
contains any null element. |
static <C extends Collection<? extends T>,T> |
checkNotContainsNull(C collection,
String pattern,
Object... args)
Ensures that a
collection passed as a parameter to the calling method is not null and does not
contains any null element. |
static <T> T |
checkNotNull(T reference)
Ensures that an object
reference passed as a parameter to the calling method is not null. |
static <T> T |
checkNotNull(T reference,
String message)
Ensures that an object
reference passed as a parameter to the calling method is not null. |
static <T> T |
checkNotNull(T reference,
String pattern,
Object... args)
Ensures that an object
reference passed as a parameter to the calling method is not null. |
static int |
checkPositionIndex(int index,
int size)
Ensures that
index specifies a valid position in an array, list or string of size. |
static void |
checkState(boolean expression)
Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters
to the calling method.
|
static void |
checkState(boolean expression,
String message)
Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters
to the calling method.
|
static void |
checkState(boolean expression,
String pattern,
Object... args)
Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters
to the calling method.
|
@Contract(value="false -> fail",
pure=true)
public static void checkArgument(boolean expression)
expression - a boolean expressionIllegalArgumentException - if expression is false@Contract(value="false, _ -> fail",
pure=true)
public static void checkArgument(boolean expression,
String message)
expression - a boolean expressionmessage - the exception message to use if the check failsIllegalArgumentException - if expression is false@Contract(value="false, _, _ -> fail",
pure=true)
public static void checkArgument(boolean expression,
String pattern,
Object... args)
expression - a boolean expressionpattern - a template for the exception message should the check failargs - the arguments to be substituted into the message templateIllegalArgumentException - if expression is falseNullPointerException - if the check fails and either pattern or args is null@Contract(value="false -> fail",
pure=true)
public static void checkState(boolean expression)
expression - a boolean expressionIllegalStateException - if expression is false@Contract(value="false, _ -> fail",
pure=true)
public static void checkState(boolean expression,
String message)
expression - a boolean expressionmessage - the exception message to use if the check failsIllegalStateException - if expression is false@Contract(value="false, _, _ -> fail",
pure=true)
public static void checkState(boolean expression,
String pattern,
Object... args)
expression - a boolean expressionpattern - a template for the exception message should the check failargs - the arguments to be substituted into the message templateIllegalStateException - if expression is falseNullPointerException - if the check fails and either pattern or args is null@Nonnull @Contract(value="null -> fail", pure=true) public static <T> T checkNotNull(@Nullable T reference)
reference passed as a parameter to the calling method is not null.reference - an object referenceNullPointerException - if reference is null@Nonnull @Contract(value="null, _ -> fail", pure=true) public static <T> T checkNotNull(@Nullable T reference, String message)
reference passed as a parameter to the calling method is not null.reference - an object referencemessage - the exception message to use if the check failsNullPointerException - if reference is null@Nonnull @Contract(value="null, _, _ -> fail", pure=true) public static <T> T checkNotNull(@Nullable T reference, String pattern, Object... args)
reference passed as a parameter to the calling method is not null.reference - an object referencepattern - a template for the exception message should the check failargs - the arguments to be substituted into the message template. Arguments are converted to strings
using String.valueOf(Object).NullPointerException - if reference is null@Nonnull public static <C extends Collection<? extends T>,T> C checkNotContainsNull(C collection)
collection passed as a parameter to the calling method is not null and does not
contains any null element.collection - a collectionNullPointerException - if collection is null or contains at least one null element@Nonnull public static <C extends Collection<? extends T>,T> C checkNotContainsNull(C collection, String message)
collection passed as a parameter to the calling method is not null and does not
contains any null element.collection - a collectionmessage - the exception message to use if the check failsNullPointerException - if collection is null or contains at least one null element@Nonnull public static <C extends Collection<? extends T>,T> C checkNotContainsNull(C collection, String pattern, Object... args)
collection passed as a parameter to the calling method is not null and does not
contains any null element.collection - a collectionpattern - a template for the exception message should the check failargs - the arguments to be substituted into the message template. Arguments are converted to strings
using String.valueOf(Object).NullPointerException - if collection is null or contains at least one null element@Nonnull public static <T> T checkInstanceOf(T reference, Class<?> type)
reference passed as a parameter to the calling method is instance of type.reference - an object referencetype - the expected type of referenceIllegalArgumentException - if reference is not instance of type@Nonnull public static <T> T checkInstanceOf(T reference, Class<?> type, String message)
reference passed as a parameter to the calling method is instance of type.reference - an object referencetype - the expected type of referencemessage - the exception message to use if the check failsIllegalArgumentException - if reference is not instance of type@Nonnull public static <T> T checkInstanceOf(T reference, Class<?> type, String pattern, Object... args)
reference passed as a parameter to the calling method is instance of type.reference - an object referencetype - the expected type of referencepattern - a template for the exception message should the check failargs - the arguments to be substituted into the message template. Arguments are converted to strings
using String.valueOf(Object).IllegalArgumentException - if reference is not instance of typepublic static <T> T checkEqualTo(@Nullable T value, @Nullable T other)
value passed as a parameter to the calling method is equal to other.value - an object referenceother - another object referenceIllegalArgumentException - if value is not equal to otherpublic static <T> T checkEqualTo(@Nullable T value, @Nullable T other, String message)
value passed as a parameter to the calling method is equal to other.value - an object referenceother - another object referencemessage - the exception message to use if the check failsIllegalArgumentException - if value is not equal to otherpublic static <T> T checkEqualTo(@Nullable T value, @Nullable T other, String pattern, Object... args)
value passed as a parameter to the calling method is equal to other.value - an object referenceother - another object referencepattern - a template for the exception message should the check failargs - the arguments to be substituted into the message template. Arguments are converted to strings
using String.valueOf(Object).IllegalArgumentException - if value is not equal to other@Nonnull public static <C extends Comparable<C>> C checkGreaterThan(C value, C lowerBoundExclusive)
value passed as a parameter to the calling method is strictly greater than lowerBoundExclusive.value - a comparablelowerBoundExclusive - the lower bound (exclusive)IllegalArgumentException - if value is less than or equal to lowerBoundExclusive@Nonnull public static <C extends Comparable<C>> C checkGreaterThan(C value, C lowerBoundExclusive, String message)
value passed as a parameter to the calling method is strictly greater than lowerBoundExclusive.value - a comparablelowerBoundExclusive - the lower bound (exclusive)message - the exception message to use if the check failsIllegalArgumentException - if value is less than or equal to lowerBoundExclusive@Nonnull public static <C extends Comparable<C>> C checkGreaterThan(C value, C lowerBoundExclusive, String pattern, Object... args)
value passed as a parameter to the calling method is strictly greater than lowerBoundExclusive.value - a comparablelowerBoundExclusive - the lower bound (exclusive)pattern - a template for the exception message should the check failargs - the arguments to be substituted into the message template. Arguments are converted to
strings using String.valueOf(Object).IllegalArgumentException - if value is less than or equal to lowerBoundExclusive@Nonnull public static <C extends Comparable<C>> C checkGreaterThanOrEqualTo(C value, C lowerBoundInclusive)
value passed as a parameter to the calling method is greater than or equal to lowerBoundInclusive.value - a comparablelowerBoundInclusive - the lower bound (inclusive)IllegalArgumentException - if value is less than lowerBoundInclusive@Nonnull public static <C extends Comparable<C>> C checkGreaterThanOrEqualTo(C value, C lowerBoundInclusive, String message)
value passed as a parameter to the calling method is greater than or equal to lowerBoundInclusive.value - a comparablelowerBoundInclusive - the lower bound (inclusive)message - the exception message to use if the check failsIllegalArgumentException - if value is less than lowerBoundInclusive@Nonnull public static <C extends Comparable<C>> C checkGreaterThanOrEqualTo(C value, C lowerBoundInclusive, String pattern, Object... args)
value passed as a parameter to the calling method is greater than or equal to lowerBoundInclusive.value - a comparablelowerBoundInclusive - the lower bound (inclusive)pattern - a template for the exception message should the check failargs - the arguments to be substituted into the message template. Arguments are converted to
strings using String.valueOf(Object).IllegalArgumentException - if value is less than lowerBoundInclusive@Nonnull public static <C extends Comparable<C>> C checkLessThan(C value, C upperBoundExclusive)
value passed as a parameter to the calling method is strictly less than upperBoundExclusive.value - a comparableupperBoundExclusive - the upper bound (exclusive)IllegalArgumentException - if value is greater than or equal to upperBoundExclusive@Nonnull public static <C extends Comparable<C>> C checkLessThan(C value, C upperBoundExclusive, String message)
value passed as a parameter to the calling method is strictly less than upperBoundExclusive.value - a comparableupperBoundExclusive - the upper bound (exclusive)message - the exception message to use if the check failsIllegalArgumentException - if value is greater than or equal to upperBoundExclusive@Nonnull public static <C extends Comparable<C>> C checkLessThan(C value, C upperBoundExclusive, String pattern, Object... args)
value passed as a parameter to the calling method is strictly less than upperBoundExclusive.value - a comparableupperBoundExclusive - the upper bound (exclusive)pattern - a template for the exception message should the check failargs - the arguments to be substituted into the message template. Arguments are converted to
strings using String.valueOf(Object).IllegalArgumentException - if value is greater than or equal to upperBoundExclusive@Nonnull public static <C extends Comparable<C>> C checkLessThanOrEqualTo(C value, C upperBoundInclusive)
value passed as a parameter to the calling method is less than or equal to upperBoundInclusive.value - a comparableupperBoundInclusive - the upper bound (inclusive)IllegalArgumentException - if value is greater than upperBoundInclusive@Nonnull public static <C extends Comparable<C>> C checkLessThanOrEqualTo(C value, C upperBoundInclusive, String message)
value passed as a parameter to the calling method is less than or equal to upperBoundInclusive.value - a comparableupperBoundInclusive - the upper bound (inclusive)message - the exception message to use if the check failsIllegalArgumentException - if value is greater than upperBoundInclusive@Nonnull public static <C extends Comparable<C>> C checkLessThanOrEqualTo(C value, C upperBoundInclusive, String pattern, Object... args)
value passed as a parameter to the calling method is less than or equal to upperBoundInclusive.value - a comparableupperBoundInclusive - the upper bound (inclusive)pattern - a template for the exception message should the check failargs - the arguments to be substituted into the message template. Arguments are converted to
strings using String.valueOf(Object).IllegalArgumentException - if value is greater than upperBoundInclusive@Nonnegative public static int checkElementIndex(@Nonnegative int index, @Nonnegative int size)
index specifies a valid element in an array, list or string of size. An
element index may range from zero, inclusive, to size, exclusive.index - a user-supplied index identifying an element of an array, list or stringsize - the size of that array, list or stringIndexOutOfBoundsException - if index is negative or is not less than sizeIllegalArgumentException - if size is negative@Nonnegative public static int checkPositionIndex(@Nonnegative int index, @Nonnegative int size)
index specifies a valid position in an array, list or string of size. A
position index may range from zero to size, inclusive.index - a user-supplied index identifying a position in an array, list or stringsize - the size of that array, list or stringIndexOutOfBoundsException - if index is negative or is greater than sizeIllegalArgumentException - if size is negativeCopyright © 2017–2019 Atlanmod. All rights reserved.