Package de.aristaflow.adept2.util
Class ArgChecks
java.lang.Object
de.aristaflow.adept2.util.ArgChecks
Utility class to facilitate common checks on method parameters and create
appropriate
IllegalArgumentExceptions, NullArgumentException,
IndexOutOfBoundsExceptions etc..-
Method Summary
Modifier and TypeMethodDescriptionstatic voidarrayLength(Object array, int minLength, int maxLength, String parameterName) Ensures that the given array has the specified minimum and maximum length.static voidarrayLength(Object array, int length, String parameterName) Ensures that the given array has exactly the specified length.static booleanRejects any expression that evaluates totrue.static voidEnsures that the given index lies within the bounds of any list-like structure with the specified length or size.static <E> E[]Ensures that the given array has the specified minimum and maximum length.static <E> E[]Ensures that the given array has exactly the specified length.static StringEnsures that the given string has the specified minimum and maximum length.static StringEnsures that the given string has exactly the specified length.static <N extends Number>
NEnsures that the given number is less than or equal to the specified value.static <E> E[]Ensures that the given array has the specified maximum length.static StringEnsures that the given string has the specified maximum length.static <E> Collection<E>maxSize(Collection<E> collection, int maxSize, String parameterName) Ensures that the given collection has the specified maximum size.static <K,V> Map<K, V> Ensures that the given map has the specified maximum size.static <N extends Number>
NEnsures that the given number is greater than or equal to the specified value.static <E> E[]Ensures that the given array has the specified minimum length.static StringEnsures that the given string has the specified minimum length.static <E> Collection<E>minSize(Collection<E> collection, int minSize, String parameterName) Ensures that the given collection has the specified minimum size.static <K,V> Map<K, V> Ensures that the given map has the specified minimum size.static String[]Ensures that the given array contains no empty strings.static <K,V> Map<K, V> Ensures that the given map contains no null key.static <E> E[]Ensures that the given array contains no null values.static <E> Collection<E>noNulls(Collection<E> collection, String parameterName) Ensures that the given collection contains no null values.static <K,V> Map<K, V> Ensures that the given map contains neither a null key nor null values.static <K,V> Map<K, V> noNullValues(Map<K, V> map, String parameterName) Ensures that the given map contains no null values.static StringEnsures that the given string is not blank.static byte[]Ensures that the given byte array is not empty.static <E> E[]Ensures that the given array is not empty.static StringEnsures that the given string is not empty.static <E> Collection<E>notEmpty(Collection<E> collection, String parameterName) Ensures that the given collection is not empty.static <K,V> Map<K, V> Ensures that the given map is not empty.static <N extends Number>
NnotNegative(N number, String parameterName) Ensures that the given number is not negative (i.e.static <T> TAsserts that the parameter is not null.static <N extends Number>
NEnsures that the given number is not zero.static StringnoWhitespace(String string, String parameterName) Ensures that the given string contains no whitespace.static StringnullOrNotEmpty(String string, String parameterName) Ensures that the given string is eithernullor not empty.static <T> TAsserts that the parameter has one of the given values (by testing for equality).static <N extends Number>
NEnsures that the given number is positive (i.e.static <E> Collection<E>size(Collection<E> collection, int minSize, int maxSize, String parameterName) Ensures that the given collection has the specified minimum and maximum size.static <E> Collection<E>size(Collection<E> collection, int size, String parameterName) Ensures that the given collection has exactly the specified size.static <K,V> Map<K, V> Ensures that the given map has the specified minimum and maximum size.static <K,V> Map<K, V> Ensures that the given map has exactly the specified size.static <T> TAsserts that the designated value matches the designated predicate.static <T> TAsserts that the designated value matches the designated predicate.
-
Method Details
-
check
Rejects any expression that evaluates totrue. This method is rather generic and can be used if no other method of this class fits the need.- Parameters:
expression-truewill be rejectedparameterName- name of the parameter being checkedmsg- the exception message in case the expression does not evaluate tofalse- Returns:
- The tested expression.
-
test
Asserts that the designated value matches the designated predicate. If not, anIllegalArgumentExceptionwill be thrown with a message including the designated parameter name and the designated predicate name.- Type Parameters:
T- The type of the designated object/parameter value.- Parameters:
object- The value of the parameter. This may benull, the designated predicate needs to allow fornull.paramName- The name of the parameter.predicate- The predicate which needs to be matched. This needs to acceptnull.predicateName- The name of the predicate used in the exception message, e. g. "check for null".- Returns:
- Whether the designated value matches the designated predicate.
-
testNn
public static <T> T testNn(T object, String paramName, Predicate<T> predicate, String predicateName) Asserts that the designated value matches the designated predicate. If not, anIllegalArgumentExceptionwill be thrown with a message including the designated parameter name and the designated predicate name.- Type Parameters:
T- The type of the designated object/parameter value.- Parameters:
object- The value of the parameter. This must not benull.paramName- The name of the parameter.predicate- The predicate which needs to be matched. This need not acceptnull.predicateName- The name of the predicate used in the exception message, e. g. "check for null".- Returns:
- Whether the designated value matches the designated predicate.
-
notNull
Asserts that the parameter is not null.- Parameters:
object- The object to be tested.parameterName- The name of the parameter.- Returns:
- The given object.
- Throws:
NullArgumentException- if the given object isnull.
-
oneOf
Asserts that the parameter has one of the given values (by testing for equality).- Parameters:
object- The object to be testedparameterName- The name of the parameterallowedValues- The allowed values; may includenull- Returns:
- The given object
- Throws:
NullArgumentException- if the given object is not one of the allowed values
-
notNegative
Ensures that the given number is not negative (i.e. >= 0).- Parameters:
number- The number to be tested.parameterName- The name of the parameter.- Returns:
- The given number.
-
positive
Ensures that the given number is positive (i.e. > 0).- Parameters:
number- The number to be tested.parameterName- The name of the parameter.- Returns:
- The given number.
-
notZero
Ensures that the given number is not zero.- Parameters:
number- The number to be tested.parameterName- The name of the parameter.- Returns:
- The given number.
-
min
Ensures that the given number is greater than or equal to the specified value.- Parameters:
number- The number to be tested.min- The smallest acceptable value.parameterName- The name of the parameter.- Returns:
- The given number.
-
max
Ensures that the given number is less than or equal to the specified value.- Parameters:
number- The number to be tested.max- The largest acceptable value.parameterName- The name of the parameter.- Returns:
- The given number.
-
index
Ensures that the given index lies within the bounds of any list-like structure with the specified length or size.- Parameters:
index- The index to be tested.length- The length or size of the list.parameterName- The name of the parameter.
-
notEmpty
Ensures that the given string is not empty.- Parameters:
string- The string to be tested.parameterName- The name of the parameter.- Returns:
- The given string.
-
notBlank
Ensures that the given string is not blank. This method has the same definition of whitespace asString.trim().- Parameters:
string- The string to be tested.parameterName- The name of the parameter.- Returns:
- The given string.
-
nullOrNotEmpty
Ensures that the given string is eithernullor not empty.- Parameters:
string- The string to be tested.parameterName- The name of the parameter.- Returns:
- The given string.
-
noWhitespace
Ensures that the given string contains no whitespace. This method has the same definition of whitespace asCharacter.isWhitespace(char).- Parameters:
string- The string to be tested.parameterName- The name of the parameter.- Returns:
- The given string.
-
minLength
Ensures that the given string has the specified minimum length.- Parameters:
string- The string to be tested.minLength- The expected minimum length.parameterName- The name of the parameter.- Returns:
- The given string.
-
maxLength
Ensures that the given string has the specified maximum length.- Parameters:
string- The string to be tested.maxLength- The expected maximum length.parameterName- The name of the parameter.- Returns:
- The given string.
-
length
Ensures that the given string has the specified minimum and maximum length.- Parameters:
string- The string to be tested.minLength- The expected minimum length.maxLength- The expected maximum length.parameterName- The name of the parameter.- Returns:
- The given string.
-
length
Ensures that the given string has exactly the specified length.- Parameters:
string- The string to be tested.length- The expected length.parameterName- The name of the parameter.- Returns:
- The given string.
-
noNulls
Ensures that the given array contains no null values.- Parameters:
array- The array to be tested.parameterName- The name of the parameter.- Returns:
- The given array.
-
notEmpty
Ensures that the given byte array is not empty.- Parameters:
array- The byte array to be tested.parameterName- The name of the parameter.- Returns:
- The given byte array.
-
notEmpty
Ensures that the given array is not empty.- Parameters:
array- The array to be tested.parameterName- The name of the parameter.- Returns:
- The given array.
-
noEmptys
Ensures that the given array contains no empty strings.- Parameters:
array- The strings to be tested.parameterName- The name of the parameter.- Returns:
- The given array.
-
minLength
Ensures that the given array has the specified minimum length.- Parameters:
array- The array to be tested.minLength- The expected minimum length.parameterName- The name of the parameter.- Returns:
- The given array.
-
maxLength
Ensures that the given array has the specified maximum length.- Parameters:
array- The array to be tested.maxLength- The expected maximum length.parameterName- The name of the parameter.- Returns:
- The given array.
-
length
Ensures that the given array has the specified minimum and maximum length.- Parameters:
array- The array to be tested.minLength- The expected minimum length.maxLength- The expected maximum length.parameterName- The name of the parameter.- Returns:
- The given array.
-
length
Ensures that the given array has exactly the specified length.- Parameters:
array- The array to be tested.length- The expected length.parameterName- The name of the parameter.- Returns:
- The given array.
-
arrayLength
Ensures that the given array has the specified minimum and maximum length. This method is intended for the use with arrays of primitive types.- Parameters:
array- The array to be tested.minLength- The expected minimum length.maxLength- The expected maximum length.parameterName- The name of the parameter.
-
arrayLength
Ensures that the given array has exactly the specified length. This method is intended for the use with arrays of primitive types.- Parameters:
array- The array to be tested.length- The expected length.parameterName- The name of the parameter.
-
noNulls
Ensures that the given collection contains no null values.- Parameters:
collection- The collection to be tested.parameterName- The name of the parameter.- Returns:
- The given collection.
-
notEmpty
Ensures that the given collection is not empty.- Parameters:
collection- The collection to be tested.parameterName- The name of the parameter.- Returns:
- The given collection.
-
minSize
public static <E> Collection<E> minSize(Collection<E> collection, int minSize, String parameterName) Ensures that the given collection has the specified minimum size.- Parameters:
collection- The collection to be tested.minSize- The expected minimum size.parameterName- The name of the parameter.- Returns:
- The given collection.
-
maxSize
public static <E> Collection<E> maxSize(Collection<E> collection, int maxSize, String parameterName) Ensures that the given collection has the specified maximum size.- Parameters:
collection- The collection to be tested.maxSize- The expected maximum size.parameterName- The name of the parameter.- Returns:
- The given collection.
-
size
public static <E> Collection<E> size(Collection<E> collection, int minSize, int maxSize, String parameterName) Ensures that the given collection has the specified minimum and maximum size.- Parameters:
collection- The collection to be tested.minSize- The expected minimum size.maxSize- The expected maximum size.parameterName- The name of the parameter.- Returns:
- The given collection.
-
size
Ensures that the given collection has exactly the specified size.- Parameters:
collection- The collection to be tested.size- The expected size.parameterName- The name of the parameter.- Returns:
- The given collection.
-
noNulls
Ensures that the given map contains neither a null key nor null values. IncludesnotNull(Object, String),noNullKey(Map, String)andnoNullValues(Map, String).- Parameters:
map- The map to be tested.parameterName- The name of the parameter.- Returns:
- The given map.
-
notEmpty
Ensures that the given map is not empty.- Parameters:
map- The map to be tested.parameterName- The name of the parameter.- Returns:
- The given map.
-
noNullKey
Ensures that the given map contains no null key.- Parameters:
map- The map to be tested.parameterName- The name of the parameter.- Returns:
- The given map.
-
noNullValues
Ensures that the given map contains no null values.- Parameters:
map- The map to be tested.parameterName- The name of the parameter.- Returns:
- The given map.
-
minSize
Ensures that the given map has the specified minimum size.- Parameters:
map- The map to be tested.minSize- The expected minimum size.parameterName- The name of the parameter.- Returns:
- The given map.
-
maxSize
Ensures that the given map has the specified maximum size.- Parameters:
map- The map to be tested.maxSize- The expected maximum size.parameterName- The name of the parameter.- Returns:
- The given map.
-
size
Ensures that the given map has the specified minimum and maximum size.- Parameters:
map- The map to be tested.minSize- The expected minimum size.maxSize- The expected maximum size.parameterName- The name of the parameter.- Returns:
- The given map.
-
size
Ensures that the given map has exactly the specified size.- Parameters:
map- The map to be tested.size- The expected size.parameterName- The name of the parameter.- Returns:
- The given map.
-