Class RequirementUtil
java.lang.Object
de.tomatengames.util.RequirementUtil
Provides convenient methods to check requirements.
For example,
requireNotNull(Object, String) can be used to ensure
that parameters are not null.- Since:
- 1.3
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> voidrequireAllNotNull(@Nullable Collection<T> collection, @NotNull String msg) Ensures that the specified collection and all of its elements are not null.static <T> voidrequireAllNotNull(T[] array, @NotNull String msg) Ensures that the specified array and all of its elements are not null.static <T> voidrequireExactLength(T[] array, int length, @NotNull String msg) Ensures that the specified array is not null and has exactly the specified length.static voidrequireExactSize(@Nullable Collection<?> collection, int size, @NotNull String msg) Ensures that the specified collection is not null and has exactly the specified number of elements.static <T> voidrequireForAll(@Nullable Collection<T> collection, @NotNull Predicate<T> predicate, @NotNull String msg, @Nullable String elementErrorMsgSuffix) Ensures that the specifiedCollectionis not null and all elements satisfy the specified predicate.static <T> voidrequireForAll(T[] array, @NotNull Predicate<T> predicate, @NotNull String msg, @Nullable String elementErrorMsgSuffix) Ensures that the specified array is not null and all elements satisfy the specified predicate.static voidrequireGreaterOrEqual(double num, double min, @NotNull String msg) Ensures that the specified number is greater than or equal to the minimum value.static voidrequireGreaterOrEqual(int num, int min, @NotNull String msg) Ensures that the specified number is greater than or equal to the minimum value.static voidrequireGreaterOrEqual(long num, long min, @NotNull String msg) Ensures that the specified number is greater than or equal to the minimum value.static voidrequireInRange(double num, double min, double max, @NotNull String msg) Ensures that the specified number is in the range [min, max].static voidrequireInRange(int num, int min, int max, @NotNull String msg) Ensures that the specified number is in the range [min, max].static voidrequireInRange(long num, long min, long max, @NotNull String msg) Ensures that the specified number is in the range [min, max].static <T> voidrequireLengthInRange(T[] array, int minLength, int maxLength, @NotNull String msg) Ensures that the specified array is not null and has a length within the specified range.static voidrequireLessOrEqual(double num, double max, @NotNull String msg) Ensures that the specified number is less than or equal to the maximum value.static voidrequireLessOrEqual(int num, int max, @NotNull String msg) Ensures that the specified number is less than or equal to the maximum value.static voidrequireLessOrEqual(long num, long max, @NotNull String msg) Ensures that the specified number is less than or equal to the maximum value.static <T> voidrequireMinLength(T[] array, int minLength, @NotNull String msg) Ensures that the specified array is not null and has a length of at least the specified minimum length.static voidrequireMinSize(@Nullable Collection<?> collection, int minSize, @NotNull String msg) Ensures that the specifiedCollectionis not null and has a size of at least the specified minimum size.static voidEnsures that the specified string is notnulland does not only contain whitespace characters.static voidrequireNotBlank(@Nullable String str, @NotNull String msg) Ensures that the specified string is notnulland does not only contain whitespace characters.static voidrequireNotEmpty(@Nullable Collection<?> collection, @NotNull String msg) Ensures that the specifiedCollectionis not null and contains at least 1 element.static <T> voidrequireNotEmpty(T[] array, @NotNull String msg) Ensures that the specified array is not null and has a length of at least 1.static voidrequireNotNegative(double num, @NotNull String msg) Ensures that the specified number is not negative.static voidrequireNotNegative(int num, @NotNull String msg) Ensures that the specified number is not negative.static voidrequireNotNegative(long num, @NotNull String msg) Ensures that the specified number is not negative.static voidEnsures that the specified object is notnull.static voidrequireNotNull(@Nullable Object obj, @NotNull String msg) Ensures that the specified object is notnull.static voidrequirePositive(double num, @NotNull String msg) Ensures that the specified number is positive.static voidrequirePositive(int num, @NotNull String msg) Ensures that the specified number is positive.static voidrequirePositive(long num, @NotNull String msg) Ensures that the specified number is positive.static voidrequireSizeInRange(@Nullable Collection<?> collection, int minSize, int maxSize, @NotNull String msg) Ensures that the specified collection is not null and has a size within the specified range.
-
Method Details
-
requireNotNull
public static void requireNotNull(@Nullable Object obj, @NotNull String msg) throws IllegalArgumentException Ensures that the specified object is notnull.- Parameters:
obj- The object that should be checked.msg- The message that should be passed to the exception. If the message ends with"...","must not be null"is appended.- Throws:
IllegalArgumentException- If the object isnull.
-
requireNotNull
Ensures that the specified object is notnull.- Parameters:
obj- The object that should be checked.- Throws:
IllegalArgumentException- If the object isnull.- See Also:
-
requireNotBlank
Ensures that the specified string is notnulland does not only contain whitespace characters.- Parameters:
str- The string that should be checked.msg- The message that should be passed to the exception. If the message ends with"...","must not be null"or"must not be blank"is appended.- Throws:
IllegalArgumentException- If the string isnullor blank.
-
requireNotBlank
Ensures that the specified string is notnulland does not only contain whitespace characters.- Parameters:
str- The string that should be checked.- Throws:
IllegalArgumentException- If the string isnullor blank.- See Also:
-
requireNotNegative
Ensures that the specified number is not negative. 0 is allowed.- Parameters:
num- The number that should be checked.msg- The message that should be passed to the exception. If the message ends with"...","must not be negative (was num)"is appended.- Throws:
IllegalArgumentException- If the number is negative.- Since:
- 1.8
-
requireNotNegative
Ensures that the specified number is not negative. 0 is allowed.- Parameters:
num- The number that should be checked.msg- The message that should be passed to the exception. If the message ends with"...","must not be negative (was num)"is appended.- Throws:
IllegalArgumentException- If the number is negative.- Since:
- 1.8
-
requireNotNegative
Ensures that the specified number is not negative. 0 is allowed.- Parameters:
num- The number that should be checked.msg- The message that should be passed to the exception. If the message ends with"...","must not be negative (was num)"is appended.- Throws:
IllegalArgumentException- If the number is negative.- Since:
- 1.8
-
requirePositive
Ensures that the specified number is positive. 0 is not allowed.- Parameters:
num- The number that should be checked.msg- The message that should be passed to the exception. If the message ends with"...","must be positive (was num)"is appended.- Throws:
IllegalArgumentException- If the number is not positive.- Since:
- 1.8
-
requirePositive
Ensures that the specified number is positive. 0 is not allowed.- Parameters:
num- The number that should be checked.msg- The message that should be passed to the exception. If the message ends with"...","must be positive (was num)"is appended.- Throws:
IllegalArgumentException- If the number is not positive.- Since:
- 1.8
-
requirePositive
Ensures that the specified number is positive. 0 is not allowed.- Parameters:
num- The number that should be checked.msg- The message that should be passed to the exception. If the message ends with"...","must be positive (was num)"is appended.- Throws:
IllegalArgumentException- If the number is not positive.- Since:
- 1.8
-
requireLessOrEqual
Ensures that the specified number is less than or equal to the maximum value.- Parameters:
num- The number that should be checked.max- The maximum value.msg- The message that should be passed to the exception. If the message ends with"...","must be less than or equal to max (was num)"is appended.- Throws:
IllegalArgumentException- If the number is greater than the maximum value.- Since:
- 1.8
-
requireLessOrEqual
Ensures that the specified number is less than or equal to the maximum value.- Parameters:
num- The number that should be checked.max- The maximum value.msg- The message that should be passed to the exception. If the message ends with"...","must be less than or equal to max (was num)"is appended.- Throws:
IllegalArgumentException- If the number is greater than the maximum value.- Since:
- 1.8
-
requireLessOrEqual
Ensures that the specified number is less than or equal to the maximum value.- Parameters:
num- The number that should be checked.max- The maximum value.msg- The message that should be passed to the exception. If the message ends with"...","must be less than or equal to max (was num)"is appended.- Throws:
IllegalArgumentException- If the number is greater than the maximum value.- Since:
- 1.8
-
requireGreaterOrEqual
Ensures that the specified number is greater than or equal to the minimum value.- Parameters:
num- The number that should be checked.min- The minimum value.msg- The message that should be passed to the exception. If the message ends with"...","must be greater than or equal to min (was num)"is appended.- Throws:
IllegalArgumentException- If the number is less than the minimum value.- Since:
- 1.8
-
requireGreaterOrEqual
Ensures that the specified number is greater than or equal to the minimum value.- Parameters:
num- The number that should be checked.min- The minimum value.msg- The message that should be passed to the exception. If the message ends with"...","must be greater than or equal to min (was num)"is appended.- Throws:
IllegalArgumentException- If the number is less than the minimum value.- Since:
- 1.8
-
requireGreaterOrEqual
Ensures that the specified number is greater than or equal to the minimum value.- Parameters:
num- The number that should be checked.min- The minimum value.msg- The message that should be passed to the exception. If the message ends with"...","must be greater than or equal to min (was num)"is appended.- Throws:
IllegalArgumentException- If the number is less than the minimum value.- Since:
- 1.8
-
requireInRange
Ensures that the specified number is in the range [min, max].- Parameters:
num- The number that should be checked.min- The minimum value (inclusive).max- The maximum value (inclusive).msg- The message that should be passed to the exception. If the message ends with"...","must be between min and max (was num)"is appended.- Throws:
IllegalArgumentException- If the number is not within the range.- Since:
- 1.8
-
requireInRange
Ensures that the specified number is in the range [min, max].- Parameters:
num- The number that should be checked.min- The minimum value (inclusive).max- The maximum value (inclusive).msg- The message that should be passed to the exception. If the message ends with"...","must be between min and max (was num)"is appended.- Throws:
IllegalArgumentException- If the number is not within the range.- Since:
- 1.8
-
requireInRange
Ensures that the specified number is in the range [min, max].- Parameters:
num- The number that should be checked.min- The minimum value (inclusive).max- The maximum value (inclusive).msg- The message that should be passed to the exception. If the message ends with"...","must be between min and max (was num)"is appended.- Throws:
IllegalArgumentException- If the number is not within the range.- Since:
- 1.8
-
requireNotEmpty
Ensures that the specifiedCollectionis not null and contains at least 1 element.- Parameters:
collection- The collection that should be checked.msg- The message that should be passed to the exception. If the message ends with"...","must not be null"or"must not be empty"is appended.- Throws:
IllegalArgumentException- If the collection is null or empty.- Since:
- 1.8
-
requireNotEmpty
Ensures that the specified array is not null and has a length of at least 1.- Type Parameters:
T- The type of the array elements.- Parameters:
array- The array that should be checked.msg- The message that should be passed to the exception. If the message ends with"...","must not be null"or"must not be empty"is appended.- Throws:
IllegalArgumentException- If the array is null or has a length of 0.- Since:
- 1.8
-
requireMinSize
public static void requireMinSize(@Nullable Collection<?> collection, int minSize, @NotNull String msg) Ensures that the specifiedCollectionis not null and has a size of at least the specified minimum size.- Parameters:
collection- The collection that should be checked.minSize- The minimum size that the collection should have.msg- The message that should be passed to the exception. If the message ends with"...", a suffix like"must not be null"with a meaningful error description is appended.- Throws:
IllegalArgumentException- If the collection is null or has a size less than the specified minimum size.- Since:
- 1.8
-
requireMinLength
Ensures that the specified array is not null and has a length of at least the specified minimum length.- Type Parameters:
T- The type of the array elements.- Parameters:
array- The array that should be checked.minLength- The minimum length that the array should have.msg- The message that should be passed to the exception. If the message ends with"...", a suffix like"must not be null"with a meaningful error description is appended.- Throws:
IllegalArgumentException- If the array is null or has a length less than the specified minimum length.- Since:
- 1.8
-
requireExactSize
public static void requireExactSize(@Nullable Collection<?> collection, int size, @NotNull String msg) Ensures that the specified collection is not null and has exactly the specified number of elements.- Parameters:
collection- The collection that should be checked.size- The exact number of elements that the collection should have.msg- The message that should be passed to the exception. If the message ends with"...", a suffix like"must not be null"with a meaningful error description is appended.- Throws:
IllegalArgumentException- If the collection is null or has a different number of elements than the specified size.- Since:
- 1.8
-
requireExactLength
Ensures that the specified array is not null and has exactly the specified length.- Type Parameters:
T- The type of the array elements.- Parameters:
array- The array that should be checked.length- The exact length that the array should have.msg- The message that should be passed to the exception. If the message ends with"...", a suffix like"must not be null"with a meaningful error description is appended.- Throws:
IllegalArgumentException- If the array is null or has a different length than the specified length.- Since:
- 1.8
-
requireSizeInRange
public static void requireSizeInRange(@Nullable Collection<?> collection, int minSize, int maxSize, @NotNull String msg) Ensures that the specified collection is not null and has a size within the specified range.- Parameters:
collection- The collection that should be checked.minSize- The minimum allowed size of the collection.maxSize- The maximum allowed size of the collection.msg- The message that should be passed to the exception. If the message ends with"...", a suffix like"must not be null"with a meaningful error description is appended.- Throws:
IllegalArgumentException- If the collection is null or has a size outside the specified range.- Since:
- 1.8
-
requireLengthInRange
public static <T> void requireLengthInRange(T[] array, int minLength, int maxLength, @NotNull String msg) Ensures that the specified array is not null and has a length within the specified range.- Type Parameters:
T- The type of elements in the array.- Parameters:
array- The array that should be checked.minLength- The minimum allowed length of the array.maxLength- The maximum allowed length of the array.msg- The message that should be passed to the exception. If the message ends with"...", a suffix like"must not be null"with a meaningful error description is appended.- Throws:
IllegalArgumentException- If the array is null or has a length outside the specified range.- Since:
- 1.8
-
requireForAll
public static <T> void requireForAll(@Nullable Collection<T> collection, @NotNull Predicate<T> predicate, @NotNull String msg, @Nullable String elementErrorMsgSuffix) Ensures that the specifiedCollectionis not null and all elements satisfy the specified predicate.- Type Parameters:
T- The type of elements in the collection.- Parameters:
collection- The collection that should be checked.predicate- The predicate that should be checked for each element in the collection. Not null.msg- The message that should be passed to the exception. If the message ends with"...", a suffix like"must not be null"with a meaningful error description is appended.elementErrorMsgSuffix- The suffix to be appended to the error message if the predicate fails for an element. If null, a generic error message is used. Example:"must not contain a null value".- Throws:
IllegalArgumentException- If the collection is null or any of its elements do not satisfy the predicate.- Since:
- 1.8
-
requireForAll
public static <T> void requireForAll(T[] array, @NotNull Predicate<T> predicate, @NotNull String msg, @Nullable String elementErrorMsgSuffix) Ensures that the specified array is not null and all elements satisfy the specified predicate.- Type Parameters:
T- The type of elements in the array.- Parameters:
array- The array that should be checked.predicate- The predicate that should be checked for each element in the array. Not null.msg- The message that should be passed to the exception. If the message ends with"...", a suffix like"must not be null"with a meaningful error description is appended.elementErrorMsgSuffix- The suffix to be appended to the error message if the predicate fails for an element. If null, a generic error message is used. Example:"must not contain a null value".- Throws:
IllegalArgumentException- If the array is null or any of its elements do not satisfy the predicate.- Since:
- 1.8
-
requireAllNotNull
Ensures that the specified collection and all of its elements are not null.- Type Parameters:
T- The type of elements in the collection.- Parameters:
collection- The collection that should be checked.msg- The message that should be passed to the exception. If the message ends with"...", a suffix like"must not be null"with a meaningful error description is appended.- Throws:
IllegalArgumentException- If the collection is null or any of its elements are null.- Since:
- 1.8
-
requireAllNotNull
Ensures that the specified array and all of its elements are not null.- Type Parameters:
T- The type of elements in the array.- Parameters:
array- The array that should be checked.msg- The message that should be passed to the exception. If the message ends with"...", a suffix like"must not be null"with a meaningful error description is appended.- Throws:
IllegalArgumentException- If the array is null or any of its elements are null.- Since:
- 1.8
-