Package org.atlanmod.commons
Class Guards
- java.lang.Object
-
- org.atlanmod.commons.Guards
-
@ParametersAreNonnullByDefault public class Guards extends Object
Static convenience methods that help a method or constructor check whether it was invoked correctly (whether its preconditions have been met). These methods generally accept abooleanexpression which is expected to betrue(or in the case ofcheckNotNull, an object reference which is expected to be non-null). Whenfalse(ornull) is passed instead, thePreconditionsmethod 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.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidcheckArgument(boolean expression)Ensures the truth of an expression involving one or more parameters to the calling method.static voidcheckArgument(boolean expression, String message)Ensures the truth of an expression involving one or more parameters to the calling method.static voidcheckArgument(boolean expression, String pattern, Object... args)Ensures the truth of an expression involving one or more parameters to the calling method.static intcheckElementIndex(int index, int size)Ensures thatindexspecifies a valid element in an array, list or string ofsize.static <T> TcheckEqualTo(T value, T other)Ensures that avaluepassed as a parameter to the calling method is equal toother.static <T> TcheckEqualTo(T value, T other, String message)Ensures that avaluepassed as a parameter to the calling method is equal toother.static <T> TcheckEqualTo(T value, T other, String pattern, Object... args)Ensures that avaluepassed as a parameter to the calling method is equal toother.static <C extends Comparable<C>>
CcheckGreaterThan(C value, C lowerBoundExclusive)Ensures that avaluepassed as a parameter to the calling method is strictly greater thanlowerBoundExclusive.static <C extends Comparable<C>>
CcheckGreaterThan(C value, C lowerBoundExclusive, String message)Ensures that avaluepassed as a parameter to the calling method is strictly greater thanlowerBoundExclusive.static <C extends Comparable<C>>
CcheckGreaterThan(C value, C lowerBoundExclusive, String pattern, Object... args)Ensures that avaluepassed as a parameter to the calling method is strictly greater thanlowerBoundExclusive.static <C extends Comparable<C>>
CcheckGreaterThanOrEqualTo(C value, C lowerBoundInclusive)Ensures that avaluepassed as a parameter to the calling method is greater than or equal tolowerBoundInclusive.static <C extends Comparable<C>>
CcheckGreaterThanOrEqualTo(C value, C lowerBoundInclusive, String message)Ensures that avaluepassed as a parameter to the calling method is greater than or equal tolowerBoundInclusive.static <C extends Comparable<C>>
CcheckGreaterThanOrEqualTo(C value, C lowerBoundInclusive, String pattern, Object... args)Ensures that avaluepassed as a parameter to the calling method is greater than or equal tolowerBoundInclusive.static <T> TcheckInstanceOf(T reference, Class<?> type)Ensures that an objectreferencepassed as a parameter to the calling method is instance oftype.static <T> TcheckInstanceOf(T reference, Class<?> type, String message)Ensures that an objectreferencepassed as a parameter to the calling method is instance oftype.static <T> TcheckInstanceOf(T reference, Class<?> type, String pattern, Object... args)Ensures that an objectreferencepassed as a parameter to the calling method is instance oftype.static <C extends Comparable<C>>
CcheckLessThan(C value, C upperBoundExclusive)Ensures that avaluepassed as a parameter to the calling method is strictly less thanupperBoundExclusive.static <C extends Comparable<C>>
CcheckLessThan(C value, C upperBoundExclusive, String message)Ensures that avaluepassed as a parameter to the calling method is strictly less thanupperBoundExclusive.static <C extends Comparable<C>>
CcheckLessThan(C value, C upperBoundExclusive, String pattern, Object... args)Ensures that avaluepassed as a parameter to the calling method is strictly less thanupperBoundExclusive.static <C extends Comparable<C>>
CcheckLessThanOrEqualTo(C value, C upperBoundInclusive)Ensures that avaluepassed as a parameter to the calling method is less than or equal toupperBoundInclusive.static <C extends Comparable<C>>
CcheckLessThanOrEqualTo(C value, C upperBoundInclusive, String message)Ensures that avaluepassed as a parameter to the calling method is less than or equal toupperBoundInclusive.static <C extends Comparable<C>>
CcheckLessThanOrEqualTo(C value, C upperBoundInclusive, String pattern, Object... args)Ensures that avaluepassed as a parameter to the calling method is less than or equal toupperBoundInclusive.static <C extends Collection<? extends T>,T>
CcheckNotContainsNull(C collection)Ensures that acollectionpassed as a parameter to the calling method is notnulland does not contains anynullelement.static <C extends Collection<? extends T>,T>
CcheckNotContainsNull(C collection, String message)Ensures that acollectionpassed as a parameter to the calling method is notnulland does not contains anynullelement.static <C extends Collection<? extends T>,T>
CcheckNotContainsNull(C collection, String pattern, Object... args)Ensures that acollectionpassed as a parameter to the calling method is notnulland does not contains anynullelement.static <T> TcheckNotNull(T reference)Ensures that an objectreferencepassed as a parameter to the calling method is notnull.static <T> TcheckNotNull(T reference, String message)Ensures that an objectreferencepassed as a parameter to the calling method is notnull.static <T> TcheckNotNull(T reference, String pattern, Object... args)Ensures that an objectreferencepassed as a parameter to the calling method is notnull.static intcheckPositionIndex(int index, int size)Ensures thatindexspecifies a valid position in an array, list or string ofsize.static voidcheckState(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 voidcheckState(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 voidcheckState(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.
-
-
-
Method Detail
-
checkArgument
@Contract(value="false -> fail", pure=true) 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
@Contract(value="false, _ -> fail", pure=true) public static void checkArgument(boolean expression, String message)Ensures the truth of an expression involving one or more parameters to the calling method.- Parameters:
expression- a boolean expressionmessage- the exception message to use if the check fails- Throws:
IllegalArgumentException- ifexpressionis false
-
checkArgument
@Contract(value="false, _, _ -> fail", pure=true) public static void checkArgument(boolean expression, String pattern, Object... args)Ensures the truth of an expression involving one or more parameters to the calling method.- Parameters:
expression- a boolean expressionpattern- a template for the exception message should the check failargs- the arguments to be substituted into the message template- Throws:
IllegalArgumentException- ifexpressionis falseNullPointerException- if the check fails and eitherpatternorargsis null
-
checkState
@Contract(value="false -> fail", pure=true) public 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.- Parameters:
expression- a boolean expression- Throws:
IllegalStateException- ifexpressionis false
-
checkState
@Contract(value="false, _ -> fail", pure=true) public 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.- Parameters:
expression- a boolean expressionmessage- the exception message to use if the check fails- Throws:
IllegalStateException- ifexpressionis false
-
checkState
@Contract(value="false, _, _ -> fail", pure=true) public 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.- Parameters:
expression- a boolean expressionpattern- a template for the exception message should the check failargs- the arguments to be substituted into the message template- Throws:
IllegalStateException- ifexpressionis falseNullPointerException- if the check fails and eitherpatternorargsis null
-
checkNotNull
@Nonnull @Contract(value="null -> fail", pure=true) public static <T> T checkNotNull(@Nullable T reference)Ensures that an objectreferencepassed as a parameter to the calling method is notnull.- Parameters:
reference- an object reference- Returns:
- the non-null reference that was validated
- Throws:
NullPointerException- ifreferenceis null
-
checkNotNull
@Nonnull @Contract(value="null, _ -> fail", pure=true) public static <T> T checkNotNull(@Nullable T reference, String message)Ensures that an objectreferencepassed as a parameter to the calling method is notnull.- Parameters:
reference- an object referencemessage- the exception message to use if the check fails- Returns:
- the non-null reference that was validated
- Throws:
NullPointerException- ifreferenceis null
-
checkNotNull
@Nonnull @Contract(value="null, _, _ -> fail", pure=true) public static <T> T checkNotNull(@Nullable T reference, String pattern, Object... args)Ensures that an objectreferencepassed as a parameter to the calling method is notnull.- Parameters:
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 usingString.valueOf(Object).- Returns:
- the non-null reference that was validated
- Throws:
NullPointerException- ifreferenceis null
-
checkNotContainsNull
@Nonnull public static <C extends Collection<? extends T>,T> C checkNotContainsNull(C collection)
Ensures that acollectionpassed as a parameter to the calling method is notnulland does not contains anynullelement.- Parameters:
collection- a collection- Returns:
- the non-null collection that was validated
- Throws:
NullPointerException- ifcollectionis null or contains at least onenullelement
-
checkNotContainsNull
@Nonnull public static <C extends Collection<? extends T>,T> C checkNotContainsNull(C collection, String message)
Ensures that acollectionpassed as a parameter to the calling method is notnulland does not contains anynullelement.- Parameters:
collection- a collectionmessage- the exception message to use if the check fails- Returns:
- the non-null collection that was validated
- Throws:
NullPointerException- ifcollectionis null or contains at least onenullelement
-
checkNotContainsNull
@Nonnull public static <C extends Collection<? extends T>,T> C checkNotContainsNull(C collection, String pattern, Object... args)
Ensures that acollectionpassed as a parameter to the calling method is notnulland does not contains anynullelement.- Parameters:
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 usingString.valueOf(Object).- Returns:
- the non-null collection that was validated
- Throws:
NullPointerException- ifcollectionis null or contains at least onenullelement
-
checkInstanceOf
@Nonnull public static <T> T checkInstanceOf(T reference, Class<?> type)Ensures that an objectreferencepassed as a parameter to the calling method is instance oftype.- Parameters:
reference- an object referencetype- the expected type ofreference- Returns:
- the reference that was validated
- Throws:
IllegalArgumentException- ifreferenceis not instance oftype
-
checkInstanceOf
@Nonnull public static <T> T checkInstanceOf(T reference, Class<?> type, String message)Ensures that an objectreferencepassed as a parameter to the calling method is instance oftype.- Parameters:
reference- an object referencetype- the expected type ofreferencemessage- the exception message to use if the check fails- Returns:
- the reference that was validated
- Throws:
IllegalArgumentException- ifreferenceis not instance oftype
-
checkInstanceOf
@Nonnull public static <T> T checkInstanceOf(T reference, Class<?> type, String pattern, Object... args)Ensures that an objectreferencepassed as a parameter to the calling method is instance oftype.- Parameters:
reference- an object referencetype- the expected type ofreferencepattern- a template for the exception message should the check failargs- the arguments to be substituted into the message template. Arguments are converted to strings usingString.valueOf(Object).- Returns:
- the reference that was validated
- Throws:
IllegalArgumentException- ifreferenceis not instance oftype
-
checkEqualTo
public static <T> T checkEqualTo(@Nullable T value, @Nullable T other)Ensures that avaluepassed as a parameter to the calling method is equal toother.- Parameters:
value- an object referenceother- another object reference- Returns:
- the value that was validated
- Throws:
IllegalArgumentException- ifvalueis not equal toother
-
checkEqualTo
public static <T> T checkEqualTo(@Nullable T value, @Nullable T other, String message)Ensures that avaluepassed as a parameter to the calling method is equal toother.- Parameters:
value- an object referenceother- another object referencemessage- the exception message to use if the check fails- Returns:
- the value that was validated
- Throws:
IllegalArgumentException- ifvalueis not equal toother
-
checkEqualTo
public static <T> T checkEqualTo(@Nullable T value, @Nullable T other, String pattern, Object... args)Ensures that avaluepassed as a parameter to the calling method is equal toother.- Parameters:
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 usingString.valueOf(Object).- Returns:
- the value that was validated
- Throws:
IllegalArgumentException- ifvalueis not equal toother
-
checkGreaterThan
@Nonnull public static <C extends Comparable<C>> C checkGreaterThan(C value, C lowerBoundExclusive)
Ensures that avaluepassed as a parameter to the calling method is strictly greater thanlowerBoundExclusive.- Parameters:
value- a comparablelowerBoundExclusive- the lower bound (exclusive)- Returns:
- the value that was validated
- Throws:
IllegalArgumentException- ifvalueis less than or equal tolowerBoundExclusive
-
checkGreaterThan
@Nonnull public static <C extends Comparable<C>> C checkGreaterThan(C value, C lowerBoundExclusive, String message)
Ensures that avaluepassed as a parameter to the calling method is strictly greater thanlowerBoundExclusive.- Parameters:
value- a comparablelowerBoundExclusive- the lower bound (exclusive)message- the exception message to use if the check fails- Returns:
- the value that was validated
- Throws:
IllegalArgumentException- ifvalueis less than or equal tolowerBoundExclusive
-
checkGreaterThan
@Nonnull public static <C extends Comparable<C>> C checkGreaterThan(C value, C lowerBoundExclusive, String pattern, Object... args)
Ensures that avaluepassed as a parameter to the calling method is strictly greater thanlowerBoundExclusive.- Parameters:
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 usingString.valueOf(Object).- Returns:
- the value that was validated
- Throws:
IllegalArgumentException- ifvalueis less than or equal tolowerBoundExclusive
-
checkGreaterThanOrEqualTo
@Nonnull public static <C extends Comparable<C>> C checkGreaterThanOrEqualTo(C value, C lowerBoundInclusive)
Ensures that avaluepassed as a parameter to the calling method is greater than or equal tolowerBoundInclusive.- Parameters:
value- a comparablelowerBoundInclusive- the lower bound (inclusive)- Returns:
- the value that was validated
- Throws:
IllegalArgumentException- ifvalueis less thanlowerBoundInclusive
-
checkGreaterThanOrEqualTo
@Nonnull public static <C extends Comparable<C>> C checkGreaterThanOrEqualTo(C value, C lowerBoundInclusive, String message)
Ensures that avaluepassed as a parameter to the calling method is greater than or equal tolowerBoundInclusive.- Parameters:
value- a comparablelowerBoundInclusive- the lower bound (inclusive)message- the exception message to use if the check fails- Returns:
- the value that was validated
- Throws:
IllegalArgumentException- ifvalueis less thanlowerBoundInclusive
-
checkGreaterThanOrEqualTo
@Nonnull public static <C extends Comparable<C>> C checkGreaterThanOrEqualTo(C value, C lowerBoundInclusive, String pattern, Object... args)
Ensures that avaluepassed as a parameter to the calling method is greater than or equal tolowerBoundInclusive.- Parameters:
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 usingString.valueOf(Object).- Returns:
- the value that was validated
- Throws:
IllegalArgumentException- ifvalueis less thanlowerBoundInclusive
-
checkLessThan
@Nonnull public static <C extends Comparable<C>> C checkLessThan(C value, C upperBoundExclusive)
Ensures that avaluepassed as a parameter to the calling method is strictly less thanupperBoundExclusive.- Parameters:
value- a comparableupperBoundExclusive- the upper bound (exclusive)- Returns:
- the value that was validated
- Throws:
IllegalArgumentException- ifvalueis greater than or equal toupperBoundExclusive
-
checkLessThan
@Nonnull public static <C extends Comparable<C>> C checkLessThan(C value, C upperBoundExclusive, String message)
Ensures that avaluepassed as a parameter to the calling method is strictly less thanupperBoundExclusive.- Parameters:
value- a comparableupperBoundExclusive- the upper bound (exclusive)message- the exception message to use if the check fails- Returns:
- the value that was validated
- Throws:
IllegalArgumentException- ifvalueis greater than or equal toupperBoundExclusive
-
checkLessThan
@Nonnull public static <C extends Comparable<C>> C checkLessThan(C value, C upperBoundExclusive, String pattern, Object... args)
Ensures that avaluepassed as a parameter to the calling method is strictly less thanupperBoundExclusive.- Parameters:
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 usingString.valueOf(Object).- Returns:
- the value that was validated
- Throws:
IllegalArgumentException- ifvalueis greater than or equal toupperBoundExclusive
-
checkLessThanOrEqualTo
@Nonnull public static <C extends Comparable<C>> C checkLessThanOrEqualTo(C value, C upperBoundInclusive)
Ensures that avaluepassed as a parameter to the calling method is less than or equal toupperBoundInclusive.- Parameters:
value- a comparableupperBoundInclusive- the upper bound (inclusive)- Returns:
- the value that was validated
- Throws:
IllegalArgumentException- ifvalueis greater thanupperBoundInclusive
-
checkLessThanOrEqualTo
@Nonnull public static <C extends Comparable<C>> C checkLessThanOrEqualTo(C value, C upperBoundInclusive, String message)
Ensures that avaluepassed as a parameter to the calling method is less than or equal toupperBoundInclusive.- Parameters:
value- a comparableupperBoundInclusive- the upper bound (inclusive)message- the exception message to use if the check fails- Returns:
- the value that was validated
- Throws:
IllegalArgumentException- ifvalueis greater thanupperBoundInclusive
-
checkLessThanOrEqualTo
@Nonnull public static <C extends Comparable<C>> C checkLessThanOrEqualTo(C value, C upperBoundInclusive, String pattern, Object... args)
Ensures that avaluepassed as a parameter to the calling method is less than or equal toupperBoundInclusive.- Parameters:
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 usingString.valueOf(Object).- Returns:
- the value that was validated
- Throws:
IllegalArgumentException- ifvalueis greater thanupperBoundInclusive
-
checkElementIndex
@Nonnegative public static int checkElementIndex(@Nonnegative int index, @Nonnegative int size)Ensures thatindexspecifies a valid element in an array, list or string ofsize. An element index may range from zero, inclusive, tosize, exclusive.- Parameters:
index- a user-supplied index identifying an element of an array, list or stringsize- the size of that array, list or string- Returns:
- the index that was validated
- Throws:
IndexOutOfBoundsException- ifindexis negative or is not less thansizeIllegalArgumentException- ifsizeis negative
-
checkPositionIndex
@Nonnegative public static int checkPositionIndex(@Nonnegative int index, @Nonnegative int size)Ensures thatindexspecifies a valid position in an array, list or string ofsize. A position index may range from zero tosize, inclusive.- Parameters:
index- a user-supplied index identifying a position in an array, list or stringsize- the size of that array, list or string- Returns:
- the index that was validated
- Throws:
IndexOutOfBoundsException- ifindexis negative or is greater thansizeIllegalArgumentException- ifsizeis negative
-
-