type) {
return isNumber(value, null, type);
}
/**
* Ensures that a string argument is a number according to {@code Integer.parseInt}
*
* @param value
* value which must be a number
* @param name
* name of object reference (in source code)
* @return the given string argument converted to an int
*
* @throws IllegalNumberArgumentException
* if the given argument {@code value} is no number
*/
@ArgumentsChecked
@Throws({ IllegalNullArgumentException.class, IllegalNumberArgumentException.class })
public static int isNumber(@Nonnull final String value, @Nullable final String name) {
Check.notNull(value, "value");
return Check.isNumber(value, name, Integer.class).intValue();
}
/**
* Ensures that a String argument is a number. This overload supports all subclasses of {@code Number}. The number
* is first converted to a {@code BigDecimal} or {@code BigInteger}. Floating point types are only supported if the
* {@code type} is one of {@code Float, Double, BigDecimal}.
*
*
* This method does also check against the ranges of the given datatypes.
*
* @param value
* value which must be a number and in the range of the given datatype.
* @param name
* (optional) name of object reference (in source code).
* @param type
* requested return value type, must be a subclass of {@code Number}, i.e. one of {@code BigDecimal,
* BigInteger, Byte, Double, Float, Integer, Long, Short}
* @return the given string argument converted to a number of the requested type
*
* @throws IllegalNumberArgumentException
* if the given argument {@code value} is no number
*/
@ArgumentsChecked
@Throws({ IllegalNullArgumentException.class, IllegalNumberArgumentException.class })
public static T isNumber(@Nonnull final String value, @Nullable final String name, @Nonnull final Class type) {
Check.notNull(value, "value");
Check.notNull(type, "type");
final Number ret;
try {
ret = checkNumberInRange(value, type);
} catch (final NumberFormatException nfe) {
if (name == null) {
throw new IllegalNumberArgumentException(nfe);
} else {
throw new IllegalNumberArgumentException(name, nfe);
}
}
return type.cast(ret);
}
/**
* Ensures that a readable sequence of {@code char} values is numeric. Numeric arguments consist only of the
* characters 0-9 and may start with 0 (compared to number arguments, which must be valid numbers - think of a bank
* account number).
*
*
* We recommend to use the overloaded method {@link Check#isNumeric(CharSequence, String)} and pass as second
* argument the name of the parameter to enhance the exception message.
*
* @param value
* a readable sequence of {@code char} values which must be a number
* @return the given string argument
* @throws IllegalNumberArgumentException
* if the given argument {@code value} is no number
*/
@ArgumentsChecked
@Throws({ IllegalNullArgumentException.class, IllegalNumberArgumentException.class })
public static T isNumeric(@Nonnull final T value) {
return isNumeric(value, EMPTY_ARGUMENT_NAME);
}
/**
* Ensures that a readable sequence of {@code char} values is numeric. Numeric arguments consist only of the
* characters 0-9 and may start with 0 (compared to number arguments, which must be valid numbers - think of a bank
* account number).
*
* @param value
* a readable sequence of {@code char} values which must be a number
* @param name
* name of object reference (in source code)
* @return the given string argument
* @throws IllegalNumberArgumentException
* if the given argument {@code value} is no number
*/
@ArgumentsChecked
@Throws({ IllegalNullArgumentException.class, IllegalNumericArgumentException.class })
public static T isNumeric(@Nonnull final T value, @Nullable final String name) {
Check.notNull(value, "value");
if (!matches(NumericRegularExpressionHolder.getPattern(), value)) {
throw new IllegalNumericArgumentException(name);
}
return value;
}
/**
* Ensures that a passed {@code Comparable} is less than another {@code Comparable}. The comparison is made using
* {@code expected.compareTo(check) <= 0}.
*
* @param expected
* Expected value
* @param check
* Comparable to be checked
* @return the passed {@code Comparable} argument {@code check}
*
* @throws IllegalNotLesserThanException
* if the argument value {@code check} is not lesser than value {@code expected} when using method
* {@code compareTo}
*/
@ArgumentsChecked
@Throws({ IllegalNullArgumentException.class, IllegalNotLesserThanException.class })
public static > T lesserThan(@Nonnull final T expected, @Nonnull final T check) {
Check.notNull(expected, "expected");
Check.notNull(check, "check");
if (expected.compareTo(check) <= 0) {
throw new IllegalNotLesserThanException();
}
return check;
}
/**
* Ensures that a passed {@code Comparable} is less than another {@code Comparable}. The comparison is made using
* {@code expected.compareTo(check) <= 0}.
*
* @param expected
* Expected value
* @param check
* Comparable to be checked
* @param message
* an error message describing why the comparables must be less than a value (will be passed to
* {@code IllegalNotLessThanException})
* @return the passed {@code Comparable} argument {@code check}
*
* @throws IllegalNotLesserThanException
* if the argument value {@code check} is not lesser than value {@code expected} when using method
* {@code compareTo}
*/
@ArgumentsChecked
@Throws({ IllegalNullArgumentException.class, IllegalNotLesserThanException.class })
public static > T lesserThan(@Nonnull final T expected, @Nonnull final T check, final String message) {
Check.notNull(expected, "expected");
Check.notNull(check, "check");
if (expected.compareTo(check) <= 0) {
throw new IllegalNotLesserThanException(message);
}
return check;
}
/**
* Checks whether a character sequence matches against a specified pattern or not.
*
* @param pattern
* pattern, that the {@code chars} must correspond to
* @param chars
* a readable sequence of {@code char} values which should match the given pattern
* @return {@code true} when {@code chars} matches against the passed {@code pattern}, otherwise {@code false}
*/
private static boolean matches(@Nonnull final Pattern pattern, @Nonnull final CharSequence chars) {
return pattern.matcher(chars).matches();
}
/**
* Ensures that a readable sequence of {@code char} values matches a specified pattern. If the given character
* sequence does not match against the passed pattern, an {@link IllegalPatternArgumentException} will be thrown.
*
*
* We recommend to use the overloaded method {@link Check#matchesPattern(Pattern, CharSequence, String)} and pass as
* second argument the name of the parameter to enhance the exception message.
*
* @param pattern
* pattern, that the {@code chars} must correspond to
* @param chars
* a readable sequence of {@code char} values which should match the given pattern
* @return the passed {@code chars} that matches the given pattern
*
* @throws IllegalNullArgumentException
* if the given argument {@code chars} is {@code null}
* @throws IllegalPatternArgumentException
* if the given {@code chars} that does not match the {@code pattern}
*/
@ArgumentsChecked
@Throws({ IllegalNullArgumentException.class, IllegalPatternArgumentException.class })
public static T matchesPattern(@Nonnull final Pattern pattern, @Nonnull final T chars) {
return matchesPattern(pattern, chars, EMPTY_ARGUMENT_NAME);
}
/**
* Ensures that a readable sequence of {@code char} values matches a specified pattern. If the given character
* sequence does not match against the passed pattern, an {@link IllegalPatternArgumentException} will be thrown.
*
* @param pattern
* pattern, that the {@code chars} must correspond to
* @param chars
* a readable sequence of {@code char} values which should match the given pattern
* @param name
* name of object reference (in source code)
* @return the passed {@code chars} that matches the given pattern
*
* @throws IllegalNullArgumentException
* if the given argument {@code chars} is {@code null}
* @throws IllegalPatternArgumentException
* if the given {@code chars} that does not match the {@code pattern}
*/
@ArgumentsChecked
@Throws({ IllegalNullArgumentException.class, IllegalPatternArgumentException.class })
public static T matchesPattern(@Nonnull final Pattern pattern, @Nonnull final T chars,
@Nullable final String name) {
Check.notNull(pattern, "pattern");
Check.notNull(chars, "chars");
if (!matches(pattern, chars)) {
throw new IllegalPatternArgumentException(name, pattern);
}
return chars;
}
/**
* Ensures that an iterable reference is neither {@code null} nor contains any elements that are {@code null}.
*
*
* We recommend to use the overloaded method {@link Check#noNullElements(Iterable, String)} and pass as second
* argument the name of the parameter to enhance the exception message.
*
* @param iterable
* the iterable reference which should not contain {@code null}
* @return the passed reference which contains no elements that are {@code null}
*
* @throws IllegalNullElementsException
* if the given argument {@code iterable} contains elements that are {@code null}
*/
@ArgumentsChecked
@Throws({ IllegalNullArgumentException.class, IllegalNullElementsException.class })
public static > T noNullElements(@Nonnull final T iterable) {
return noNullElements(iterable, EMPTY_ARGUMENT_NAME);
}
/**
* Ensures that an iterable reference is neither {@code null} nor contains any elements that are {@code null}.
*
* @param iterable
* the iterable reference which should not contain {@code null}
* @param name
* name of object reference (in source code)
* @return the passed reference which contains no elements that are {@code null}
* @throws IllegalNullElementsException
* if the given argument {@code iterable} contains elements that are {@code null}
*/
@ArgumentsChecked
@Throws({ IllegalNullArgumentException.class, IllegalNullElementsException.class })
public static > T noNullElements(@Nonnull final T iterable, final String name) {
Check.notNull(iterable, "iterable");
for (final Object element : iterable) {
if (element == null) {
throw new IllegalNullElementsException(name);
}
}
return iterable;
}
/**
* Ensures that an array does not contain {@code null}.
*
*
* We recommend to use the overloaded method {@link Check#noNullElements(Object[], String)} and pass as second
* argument the name of the parameter to enhance the exception message.
*
* @param array
* reference to an array
* @return the passed reference which contains no elements that are {@code null}
* @throws IllegalNullElementsException
* if the given argument {@code array} contains {@code null}
*/
@ArgumentsChecked
@Throws({ IllegalNullArgumentException.class, IllegalNullElementsException.class })
public static T[] noNullElements(@Nonnull final T[] array) {
return noNullElements(array, EMPTY_ARGUMENT_NAME);
}
/**
* Ensures that an array does not contain {@code null}.
*
* @param array
* reference to an array
* @param name
* name of object reference (in source code)
* @return the passed reference which contains no elements that are {@code null}
* @throws IllegalNullElementsException
* if the given argument {@code array} contains {@code null}
*/
@ArgumentsChecked
@Throws({ IllegalNullArgumentException.class, IllegalNullElementsException.class })
public static T[] noNullElements(@Nonnull final T[] array, @Nullable final String name) {
Check.notNull(array, "array");
if (containsNullElements(array)) {
throw new IllegalNullElementsException(name);
}
return array;
}
/**
* Ensures that a passed parameter of the calling method is not empty, using the passed expression to evaluate the
* emptiness.
*
*
* We recommend to use the overloaded method {@link Check#notEmpty(boolean, String)} and pass as second argument the
* name of the parameter to enhance the exception message.
*
* @param expression
* the result of the expression to verify the emptiness of a reference ({@code true} means empty,
* {@code false} means not empty)
*
* @throws IllegalNullArgumentException
* if the given argument {@code reference} is {@code null}
* @throws IllegalEmptyArgumentException
* if the given argument {@code reference} is empty
*/
@ArgumentsChecked
@Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class })
public static void notEmpty(final boolean expression) {
notEmpty(expression, EMPTY_ARGUMENT_NAME);
}
/**
* Ensures that a passed parameter of the calling method is not empty, using the passed expression to evaluate the
* emptiness.
*
* @param expression
* the result of the expression to verify the emptiness of a reference ({@code true} means empty,
* {@code false} means not empty)
* @param name
* name of object reference (in source code)
*
* @throws IllegalEmptyArgumentException
* if the given argument {@code reference} is empty
*/
@ArgumentsChecked
@Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class })
public static void notEmpty(final boolean expression, @Nullable final String name) {
if (expression) {
throw new IllegalEmptyArgumentException(name);
}
}
/**
* Ensures that a passed string as a parameter of the calling method is not empty.
*
*
* We recommend to use the overloaded method {@link Check#notEmpty(CharSequence, String)} and pass as second
* argument the name of the parameter to enhance the exception message.
*
* @param chars
* a readable sequence of {@code char} values which should not be empty
* @return the passed reference that is not empty
* @throws IllegalNullArgumentException
* if the given argument {@code reference} is {@code null}
* @throws IllegalEmptyArgumentException
* if the given argument {@code reference} is empty
*/
@ArgumentsChecked
@Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class })
public static T notEmpty(@Nonnull final T chars) {
notNull(chars);
notEmpty(chars, chars.length() == 0, EMPTY_ARGUMENT_NAME);
return chars;
}
/**
* Ensures that a passed collection as a parameter of the calling method is not empty.
*
*
* We recommend to use the overloaded method {@link Check#notEmpty(Collection, String)} and pass as second argument
* the name of the parameter to enhance the exception message.
*
* @param collection
* a collection which should not be empty
* @return the passed reference that is not empty
* @throws IllegalNullArgumentException
* if the given argument {@code collection} is {@code null}
* @throws IllegalEmptyArgumentException
* if the given argument {@code collection} is empty
*/
@ArgumentsChecked
@Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class })
public static > T notEmpty(@Nonnull final T collection) {
notNull(collection);
notEmpty(collection, collection.isEmpty(), EMPTY_ARGUMENT_NAME);
return collection;
}
/**
* Ensures that a passed map as a parameter of the calling method is not empty.
*
*
* We recommend to use the overloaded method {@link Check#notEmpty(Collection, String)} and pass as second argument
* the name of the parameter to enhance the exception message.
*
* @param map
* a map which should not be empty
* @return the passed reference that is not empty
* @throws IllegalNullArgumentException
* if the given argument {@code map} is {@code null}
* @throws IllegalEmptyArgumentException
* if the given argument {@code map} is empty
*/
@ArgumentsChecked
@Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class })
public static > T notEmpty(@Nonnull final T map) {
notNull(map);
notEmpty(map, map.isEmpty(), EMPTY_ARGUMENT_NAME);
return map;
}
/**
* Ensures that an object reference passed as a parameter to the calling method is not empty. The passed boolean
* value is the result of checking whether the reference is empty or not.
*
*
* The following example describes how to use it.
*
*
* @ArgumentsChecked
* public setText(String text) {
* Check.notEmpty(text, text.isEmpty(), "text");
* this.text = text;
* }
*
*
* @param reference
* an object reference which should not be empty
* @param expression
* the result of the expression to verify the emptiness of a reference ({@code true} means empty,
* {@code false} means not empty)
* @param name
* name of object reference (in source code)
* @return the passed reference that is not empty
* @throws IllegalNullArgumentException
* if the given argument {@code reference} is {@code null}
* @throws IllegalEmptyArgumentException
* if the given argument {@code reference} is empty
*/
@ArgumentsChecked
@Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class })
public static T notEmpty(@Nonnull final T reference, final boolean expression, @Nullable final String name) {
notNull(reference, name);
if (expression) {
throw new IllegalEmptyArgumentException(name);
}
return reference;
}
/**
* Ensures that a passed string as a parameter of the calling method is not empty.
*
*
* The following example describes how to use it.
*
*
* @ArgumentsChecked
* public setText(String text) {
* this.text = Check.notEmpty(text, "text");
* }
*
*
* @param chars
* a readable sequence of {@code char} values which should not be empty
* @param name
* name of object reference (in source code)
* @return the passed reference that is not empty
* @throws IllegalNullArgumentException
* if the given argument {@code string} is {@code null}
* @throws IllegalEmptyArgumentException
* if the given argument {@code string} is empty
*/
@ArgumentsChecked
@Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class })
public static T notEmpty(@Nonnull final T chars, @Nullable final String name) {
notNull(chars, name);
notEmpty(chars, chars.length() == 0, name);
return chars;
}
/**
* Ensures that a passed map as a parameter of the calling method is not empty.
*
*
* We recommend to use the overloaded method {@link Check#notEmpty(Collection, String)} and pass as second argument
* the name of the parameter to enhance the exception message.
*
* @param map
* a map which should not be empty
* @param name
* name of object reference (in source code)
* @return the passed reference that is not empty
* @throws IllegalNullArgumentException
* if the given argument {@code map} is {@code null}
* @throws IllegalEmptyArgumentException
* if the given argument {@code map} is empty
*/
@ArgumentsChecked
@Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class })
public static > T notEmpty(@Nonnull final T map, @Nullable final String name) {
notNull(map);
notEmpty(map, map.isEmpty(), name);
return map;
}
/**
* Ensures that a passed collection as a parameter of the calling method is not empty.
*
*
* The following example describes how to use it.
*
*
* @ArgumentsChecked
* public setCollection(Collection<String> collection) {
* this.collection = Check.notEmpty(collection, "collection");
* }
*
*
* @param collection
* a collection which should not be empty
* @param name
* name of object reference (in source code)
* @return the passed reference that is not empty
* @throws IllegalNullArgumentException
* if the given argument {@code collection} is {@code null}
* @throws IllegalEmptyArgumentException
* if the given argument {@code collection} is empty
*/
@ArgumentsChecked
@Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class })
public static > T notEmpty(@Nonnull final T collection, @Nullable final String name) {
notNull(collection, name);
notEmpty(collection, collection.isEmpty(), name);
return collection;
}
/**
* Ensures that a passed map as a parameter of the calling method is not empty.
*
*
* We recommend to use the overloaded method {@link Check#notEmpty(Object[], String)} and pass as second argument
* the name of the parameter to enhance the exception message.
*
* @param array
* a map which should not be empty
* @return the passed reference that is not empty
* @throws IllegalNullArgumentException
* if the given argument {@code array} is {@code null}
* @throws IllegalEmptyArgumentException
* if the given argument {@code array} is empty
*/
@ArgumentsChecked
@Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class })
public static T[] notEmpty(@Nonnull final T[] array) {
notNull(array);
notEmpty(array, array.length == 0, EMPTY_ARGUMENT_NAME);
return array;
}
/**
* Ensures that a passed map as a parameter of the calling method is not empty.
*
* @param array
* a map which should not be empty
* @param name
* name of object reference (in source code)
* @return the passed reference that is not empty
* @throws IllegalNullArgumentException
* if the given argument {@code array} is {@code null}
* @throws IllegalEmptyArgumentException
* if the given argument {@code array} is empty
*/
@ArgumentsChecked
@Throws({ IllegalNullArgumentException.class, IllegalEmptyArgumentException.class })
public static T[] notEmpty(@Nonnull final T[] array, @Nullable final String name) {
notNull(array);
notEmpty(array, array.length == 0, EMPTY_ARGUMENT_NAME);
return array;
}
/**
* Do not perform any check and just return {@code t}.
*
* This is useful if you have several checks on some arguments, but do not check other arguments on purpose. This
* checks helps to document that a check was omitted on purpose instead of forgotten.
*
* @param t
* any object
* @return t
*/
public static T nothing(final T t) {
return t;
}
/**
* Ensures that a double argument is not NaN (not a number).
*
*
* We recommend to use the overloaded method {@link Check#notNaN(double, String)} and pass as second argument the
* name of the parameter to enhance the exception message.
*
* @see java.lang.Double#NaN
*
* @param value
* value which should not be NaN
* @return the given double value
* @throws IllegalNaNArgumentException
* if the given argument {@code value} is NaN
*/
@Throws(IllegalNaNArgumentException.class)
public static double notNaN(final double value) {
return notNaN(value, EMPTY_ARGUMENT_NAME);
}
/**
* Ensures that a double argument is not NaN (not a number).
*
* @see java.lang.Double#NaN
*
* @param value
* value which should not be NaN
* @param name
* name of object reference (in source code)
* @return the given double value
* @throws IllegalNaNArgumentException
* if the given argument {@code value} is NaN
*/
@Throws(IllegalNaNArgumentException.class)
public static double notNaN(final double value, @Nullable final String name) {
// most efficient check for NaN, see Double.isNaN(value))
if (value != value) {
throw new IllegalNaNArgumentException(name);
}
return value;
}
/**
* Ensures that a double argument is not NaN (not a number).
*
*
* We recommend to use the overloaded method {@link Check#notNaN(float, String)} and pass as second argument the
* name of the parameter to enhance the exception message.
*
* @see java.lang.Float#NaN
*
* @param value
* value which should not be NaN
* @return the given double value
* @throws IllegalNaNArgumentException
* if the given argument {@code value} is NaN
*/
@Throws(IllegalNaNArgumentException.class)
public static float notNaN(final float value) {
return notNaN(value, EMPTY_ARGUMENT_NAME);
}
/**
* Ensures that a double argument is not NaN (not a number).
*
* @see java.lang.Float#NaN
*
* @param value
* value which should not be NaN
* @param name
* name of object reference (in source code)
* @return the given float value
* @throws IllegalNaNArgumentException
* if the given argument {@code value} is NaN
*/
@Throws(IllegalNaNArgumentException.class)
public static float notNaN(final float value, @Nullable final String name) {
// most efficient check for NaN, see Float.isNaN(value))
if (value != value) {
throw new IllegalNaNArgumentException(name);
}
return value;
}
/**
* Ensures that an double reference passed as a parameter to the calling method is not smaller than {@code 0}.
*
*
* We recommend to use the overloaded method {@link Check#notNegative(double, String)} and pass as second argument
* the name of the parameter to enhance the exception message.
*
* @param value
* a number
* @return the non-null reference that was validated
* @throws IllegalNegativeArgumentException
* if the given argument {@code reference} is smaller than {@code 0}
*/
@Throws(IllegalNegativeArgumentException.class)
public static double notNegative(@Nonnull final double value) {
if (value < 0.0) {
throw new IllegalNegativeArgumentException();
}
return value;
}
/**
* Ensures that an double reference passed as a parameter to the calling method is not smaller than {@code 0}.
*
* @param value
* a number
* @param name
* name of the number reference (in source code)
* @return the non-null reference that was validated
* @throws IllegalNullArgumentException
* if the given argument {@code reference} is smaller than {@code 0}
*/
@Throws(IllegalNegativeArgumentException.class)
public static double notNegative(@Nonnull final double value, @Nullable final String name) {
if (value < 0.0) {
throw new IllegalNegativeArgumentException(name);
}
return value;
}
/**
* Ensures that an float reference passed as a parameter to the calling method is not smaller than {@code 0}.
*
*
* We recommend to use the overloaded method {@link Check#notNegative(float, String)} and pass as second argument
* the name of the parameter to enhance the exception message.
*
* @param value
* a number
* @return the non-null reference that was validated
* @throws IllegalNegativeArgumentException
* if the given argument {@code reference} is smaller than {@code 0}
*/
@Throws(IllegalNegativeArgumentException.class)
public static float notNegative(@Nonnull final float value) {
if (value < 0.0f) {
throw new IllegalNegativeArgumentException();
}
return value;
}
/**
* Ensures that an float reference passed as a parameter to the calling method is not smaller than {@code 0}.
*
* @param value
* a number
* @param name
* name of the number reference (in source code)
* @return the non-null reference that was validated
* @throws IllegalNullArgumentException
* if the given argument {@code reference} is smaller than {@code 0}
*/
@Throws(IllegalNegativeArgumentException.class)
public static float notNegative(@Nonnull final float value, @Nullable final String name) {
if (value < 0.0f) {
throw new IllegalNegativeArgumentException(name);
}
return value;
}
/**
* Ensures that an integer reference passed as a parameter to the calling method is not smaller than {@code 0}.
*
*
* We recommend to use the overloaded method {@link Check#notNegative(int, String)} and pass as second argument the
* name of the parameter to enhance the exception message.
*
* @param value
* a number
* @return the non-null reference that was validated
* @throws IllegalNegativeArgumentException
* if the given argument {@code reference} is smaller than {@code 0}
*/
@Throws(IllegalNegativeArgumentException.class)
public static int notNegative(@Nonnull final int value) {
if (value < 0) {
throw new IllegalNegativeArgumentException();
}
return value;
}
/**
* Ensures that an integer reference passed as a parameter to the calling method is not smaller than {@code 0}.
*
* @param value
* a number
* @param name
* name of the number reference (in source code)
* @return the non-null reference that was validated
* @throws IllegalNullArgumentException
* if the given argument {@code reference} is smaller than {@code 0}
*/
@Throws(IllegalNegativeArgumentException.class)
public static int notNegative(@Nonnull final int value, @Nullable final String name) {
if (value < 0) {
throw new IllegalNegativeArgumentException(name);
}
return value;
}
/**
* Ensures that an long reference passed as a parameter to the calling method is not smaller than {@code 0}.
*
*
* We recommend to use the overloaded method {@link Check#notNegative(long, String)} and pass as second argument the
* name of the parameter to enhance the exception message.
*
* @param value
* a number
* @return the non-null reference that was validated
* @throws IllegalNegativeArgumentException
* if the given argument {@code reference} is smaller than {@code 0}
*/
@Throws(IllegalNegativeArgumentException.class)
public static long notNegative(@Nonnull final long value) {
if (value < 0L) {
throw new IllegalNegativeArgumentException();
}
return value;
}
/**
* Ensures that an long reference passed as a parameter to the calling method is not smaller than {@code 0}.
*
* @param value
* a number
* @param name
* name of the number reference (in source code)
* @return the non-null reference that was validated
* @throws IllegalNullArgumentException
* if the given argument {@code reference} is smaller than {@code 0}
*/
@Throws(IllegalNegativeArgumentException.class)
public static long notNegative(@Nonnull final long value, @Nullable final String name) {
if (value < 0L) {
throw new IllegalNegativeArgumentException(name);
}
return value;
}
/**
* Ensures that an short reference passed as a parameter to the calling method is not smaller than {@code 0}.
*
*
* We recommend to use the overloaded method {@link Check#notNegative(short, String)} and pass as second argument
* the name of the parameter to enhance the exception message.
*
* @param value
* a number
* @return the non-null reference that was validated
* @throws IllegalNegativeArgumentException
* if the given argument {@code reference} is smaller than {@code 0}
*/
@Throws(IllegalNegativeArgumentException.class)
public static short notNegative(@Nonnull final short value) {
if (value < (short) 0) {
throw new IllegalNegativeArgumentException();
}
return value;
}
/**
* Ensures that an short reference passed as a parameter to the calling method is not smaller than {@code 0}.
*
* @param value
* a number
* @param name
* name of the number reference (in source code)
* @return the non-null reference that was validated
* @throws IllegalNullArgumentException
* if the given argument {@code reference} is smaller than {@code 0}
*/
@Throws(IllegalNegativeArgumentException.class)
public static short notNegative(@Nonnull final short value, @Nullable final String name) {
if (value < (short) 0) {
throw new IllegalNegativeArgumentException(name);
}
return value;
}
/**
* Ensures that an object reference passed as a parameter to the calling method is not {@code null}.
*
*
* We recommend to use the overloaded method {@link Check#notNull(Object, String)} and pass as second argument the
* name of the parameter to enhance the exception message.
*
* @param reference
* an object reference
* @return the non-null reference that was validated
* @throws IllegalNullArgumentException
* if the given argument {@code reference} is {@code null}
*/
@Throws(IllegalNullArgumentException.class)
public static T notNull(@Nonnull final T reference) {
if (reference == null) {
throw new IllegalNullArgumentException();
}
return reference;
}
/**
* Ensures that an object reference passed as a parameter to the calling method is not {@code null}.
*
* @param reference
* an object reference
* @param name
* name of object reference (in source code)
* @return the non-null reference that was validated
* @throws IllegalNullArgumentException
* if the given argument {@code reference} is {@code null}
*/
@Throws(IllegalNullArgumentException.class)
public static T notNull(@Nonnull final T reference, @Nullable final String name) {
if (reference == null) {
throw new IllegalNullArgumentException(name);
}
return reference;
}
/**
* Ensures that an double reference passed as a parameter to the calling method is not greater than {@code 0}.
*
*
* We recommend to use the overloaded method {@link Check#notPositive(double, String)} and pass as second argument
* the name of the parameter to enhance the exception message.
*
* @param value
* a number
* @return the non-null reference that was validated
* @throws IllegalPositiveArgumentException
* if the given argument {@code reference} is smaller than {@code 0}
*/
@Throws(IllegalPositiveArgumentException.class)
public static double notPositive(@Nonnull final double value) {
if (value > 0.0) {
throw new IllegalPositiveArgumentException();
}
return value;
}
/**
* Ensures that an double reference passed as a parameter to the calling method is not greater than {@code 0}.
*
* @param value
* a number
* @param name
* name of the number reference (in source code)
* @return the non-null reference that was validated
* @throws IllegalNullArgumentException
* if the given argument {@code reference} is smaller than {@code 0}
*/
@Throws(IllegalPositiveArgumentException.class)
public static double notPositive(@Nonnull final double value, @Nullable final String name) {
if (value > 0.0) {
throw new IllegalPositiveArgumentException(name);
}
return value;
}
/**
* Ensures that an float reference passed as a parameter to the calling method is not greater than {@code 0}.
*
*
* We recommend to use the overloaded method {@link Check#notPositive(float, String)} and pass as second argument
* the name of the parameter to enhance the exception message.
*
* @param value
* a number
* @return the non-null reference that was validated
* @throws IllegalPositiveArgumentException
* if the given argument {@code reference} is smaller than {@code 0}
*/
@Throws(IllegalPositiveArgumentException.class)
public static float notPositive(@Nonnull final float value) {
if (value > 0.0f) {
throw new IllegalPositiveArgumentException();
}
return value;
}
/**
* Ensures that an float reference passed as a parameter to the calling method is not greater than {@code 0}.
*
* @param value
* a number
* @param name
* name of the number reference (in source code)
* @return the non-null reference that was validated
* @throws IllegalNullArgumentException
* if the given argument {@code reference} is smaller than {@code 0}
*/
@Throws(IllegalPositiveArgumentException.class)
public static float notPositive(@Nonnull final float value, @Nullable final String name) {
if (value > 0.0f) {
throw new IllegalPositiveArgumentException(name);
}
return value;
}
/**
* Ensures that an integer reference passed as a parameter to the calling method is not greater than {@code 0}.
*
*
* We recommend to use the overloaded method {@link Check#notPositive(int, String)} and pass as second argument the
* name of the parameter to enhance the exception message.
*
* @param value
* a number
* @return the non-null reference that was validated
* @throws IllegalPositiveArgumentException
* if the given argument {@code reference} is smaller than {@code 0}
*/
@Throws(IllegalPositiveArgumentException.class)
public static int notPositive(@Nonnull final int value) {
if (value > 0) {
throw new IllegalPositiveArgumentException();
}
return value;
}
/**
* Ensures that an integer reference passed as a parameter to the calling method is not greater than {@code 0}.
*
* @param value
* a number
* @param name
* name of the number reference (in source code)
* @return the non-null reference that was validated
* @throws IllegalNullArgumentException
* if the given argument {@code reference} is smaller than {@code 0}
*/
@Throws(IllegalPositiveArgumentException.class)
public static int notPositive(@Nonnull final int value, @Nullable final String name) {
if (value > 0) {
throw new IllegalPositiveArgumentException(name);
}
return value;
}
/**
* Ensures that an long reference passed as a parameter to the calling method is not greater than {@code 0}.
*
*
* We recommend to use the overloaded method {@link Check#notPositive(long, String)} and pass as second argument the
* name of the parameter to enhance the exception message.
*
* @param value
* a number
* @return the non-null reference that was validated
* @throws IllegalPositiveArgumentException
* if the given argument {@code reference} is smaller than {@code 0}
*/
@Throws(IllegalPositiveArgumentException.class)
public static long notPositive(@Nonnull final long value) {
if (value > 0L) {
throw new IllegalPositiveArgumentException();
}
return value;
}
/**
* Ensures that an long reference passed as a parameter to the calling method is not greater than {@code 0}.
*
* @param value
* a number
* @param name
* name of the number reference (in source code)
* @return the non-null reference that was validated
* @throws IllegalNullArgumentException
* if the given argument {@code reference} is smaller than {@code 0}
*/
@Throws(IllegalPositiveArgumentException.class)
public static long notPositive(@Nonnull final long value, @Nullable final String name) {
if (value > 0L) {
throw new IllegalPositiveArgumentException(name);
}
return value;
}
/**
* Ensures that an short reference passed as a parameter to the calling method is not greater than {@code 0}.
*
*
* We recommend to use the overloaded method {@link Check#notPositive(short, String)} and pass as second argument
* the name of the parameter to enhance the exception message.
*
* @param value
* a number
* @return the non-null reference that was validated
* @throws IllegalPositiveArgumentException
* if the given argument {@code reference} is smaller than {@code 0}
*/
@Throws(IllegalPositiveArgumentException.class)
public static short notPositive(@Nonnull final short value) {
if (value > (short) 0) {
throw new IllegalPositiveArgumentException();
}
return value;
}
/**
* Ensures that an short reference passed as a parameter to the calling method is not greater than {@code 0}.
*
* @param value
* a number
* @param name
* name of the number reference (in source code)
* @return the non-null reference that was validated
* @throws IllegalNullArgumentException
* if the given argument {@code reference} is smaller than {@code 0}
*/
@Throws(IllegalPositiveArgumentException.class)
public static short notPositive(@Nonnull final short value, @Nullable final String name) {
if (value > (short) 0) {
throw new IllegalPositiveArgumentException(name);
}
return value;
}
/**
* Ensures that a given position index is valid within the size of an array, list or string ...
*
* @param index
* index of an array, list or string
* @param size
* size of an array list or string
* @return the index
*
* @throws IllegalPositionIndexException
* if the index is not a valid position index within an array, list or string of size size
*
*/
@Throws(IllegalPositionIndexException.class)
public static int positionIndex(final int index, final int size) {
final boolean isIndexValid = (size >= 0) && (index >= 0) && (index < size);
if (!isIndexValid) {
throw new IllegalPositionIndexException(index, size);
}
return index;
}
/**
* Ensures that the given arguments are a valid range.
*
* A range (start, end, size) is valid if the following conditions are {@code true}:
*
* - start <= size
* - end <= size
* - start <= end
* - size >= 0
* - start >= 0
* - end >= 0
*
*
* @param start
* the start value of the range (must be a positive integer or 0)
* @param end
* the end value of the range (must be a positive integer or 0)
* @param size
* the size value of the range (must be a positive integer or 0)
*
* @throws IllegalRangeException
* if the given arguments do not form a valid range
*/
@Throws(IllegalRangeException.class)
public static void range(@Nonnegative final int start, @Nonnegative final int end, @Nonnegative final int size) {
final boolean rangeIsValid = (start <= size) && (end <= size) && (start <= end);
final boolean inputValuesAreValid = (size >= 0) && (start >= 0) && (end >= 0);
if (!rangeIsValid || !inputValuesAreValid) {
throw new IllegalRangeException(start, end, size);
}
}
/**
* Ensures that a given state is {@code true}.
*
*
* We recommend to use the overloaded method {@link Check#stateIsTrue(boolean, String)} and pass as second argument
* the name of the parameter to enhance the exception message. A better way is to create specific exceptions (with a
* good wording) for your case and to use the overloaded method {@link Check#stateIsTrue(boolean, Class)} and pass
* as second argument your exception.
*
* @param expression
* an expression that must be true to indicate a valid state
*
* @throws IllegalStateOfArgumentException
* if the given arguments caused an invalid state
*/
@Throws(IllegalStateOfArgumentException.class)
public static void stateIsTrue(final boolean expression) {
if (!expression) {
throw new IllegalStateOfArgumentException();
}
}
/**
* Ensures that a given state is {@code true} and allows to specify the class of exception which is thrown in case
* the state is not {@code true}.
*
* @param expression
* an expression that must be {@code true} to indicate a valid state
* @param clazz
* an subclass of {@link RuntimeException} which will be thrown if the given state is not valid
* @throws clazz
* a new instance of {@code clazz} if the given arguments caused an invalid state
* @throws RuntimeInstantiationException
* Attention: Be aware, that a {@code RuntimeInstantiationException} can be thrown when
* the given {@code clazz} cannot be instantiated
*/
@ArgumentsChecked
@Throws({ IllegalNullArgumentException.class, RuntimeInstantiationException.class })
public static void stateIsTrue(final boolean expression, final Class extends RuntimeException> clazz) {
Check.notNull(clazz, "clazz");
if (!expression) {
RuntimeException re;
try {
re = clazz.newInstance();
} catch (final InstantiationException e) {
throw new RuntimeInstantiationException(clazz.getSimpleName(), e);
} catch (final IllegalAccessException e) {
throw new RuntimeInstantiationException(clazz.getSimpleName(), e);
}
throw re;
}
}
/**
* Ensures that a given state is {@code true}.
*
* @param expression
* an expression that must be {@code true} to indicate a valid state
* @param description
* will be used in the error message to describe why the arguments caused an invalid state
* @throws IllegalStateOfArgumentException
* if the given arguments caused an invalid state
*/
@Throws(IllegalStateOfArgumentException.class)
public static void stateIsTrue(final boolean expression, @Nonnull final String description) {
if (!expression) {
throw new IllegalStateOfArgumentException(description);
}
}
/**
* Ensures that a given state is {@code true}
*
* @param expression
* an expression that must be {@code true} to indicate a valid state
* @param descriptionTemplate
* format string template that explains why the state is invalid
* @param descriptionTemplateArgs
* format string template arguments to explain why the state is invalid
* @throws IllegalStateOfArgumentException
* if the given arguments caused an invalid state
*/
@Throws(IllegalStateOfArgumentException.class)
public static void stateIsTrue(final boolean expression, @Nonnull final String descriptionTemplate,
final Object... descriptionTemplateArgs) {
if (!expression) {
throw new IllegalStateOfArgumentException(descriptionTemplate, descriptionTemplateArgs);
}
}
/**
* Attention: This class is not intended to create objects from it.
*/
private Check() {
// This class is not intended to create objects from it.
}
}