This class assists in validating arguments. The validation methods are based along the following principles:
All exceptions messages are format strings as defined by the Java platform. For example:
Validate.isTrue(i > 0, "The value must be greater than zero: %d", i); Validate.notNull(surname, "The surname must not be %s", null);
This class's source was modified from the Apache commons-lang library: https://github.com/apache/commons-lang/
#ThreadSafe#
See Also:Method Summary
All MethodsStatic MethodsConcrete Methods
static double
Validate that the specified primitive value falls between the two exclusive values specified; otherwise, throws an exception with the specified message.
static long
Validate that the specified primitive value falls between the two exclusive values specified; otherwise, throws an exception with the specified message.
Validate that the specified argument object fall between the two exclusive values specified; otherwise, throws an exception with the specified message.
Returns the param if non null, otherwise gets a default value from the providedstatic <T> T
Supplier
.static double
Validate that the specified primitive value falls between the two inclusive values specified; otherwise, throws an exception with the specified message.
static long
Validate that the specified primitive value falls between the two inclusive values specified; otherwise, throws an exception with the specified message.
Validate that the specified argument object fall between the two inclusive values specified; otherwise, throws an exception with the specified message.
static <T> Class<? extends T>
Validates that the argument can be converted to the specified class, if not throws an exception.
static void
Validate that the argument condition is
false
; otherwise throwing an exception with the specified message.static <T, U> U
Validate that the argument is an instance of the specified class; otherwise throwing an exception with the specified message.
static int
static long
Asserts that the given duration is positive, including zero.
static <T> void
Validate that the specified argument is
null
; otherwise throwing an exception with the specified message.static double
static int
Asserts that the given number is positive (non-negative and non-zero).
static long
Asserts that the given number is positive (non-negative and non-zero).
Asserts that the given duration is positive (non-negative and non-zero).
Asserts that the given boxed double is positive (non-negative and non-zero) or null.
Asserts that the given boxed integer is positive (non-negative and non-zero) or null.
Asserts that the given boxed long is positive (non-negative and non-zero) or null.
Asserts that the given duration is positive (non-negative and non-zero) or null.
static void
Validate that the argument condition is
true
; otherwise throwing an exception with the specified message.static void
Verify that only one of the objects is non null.
static <T> T[]
Validate that the specified argument array is neither
null
nor contains any elements that arenull
; otherwise throwing an exception with the specified message.Validate that the specified argument iterable is neither
null
nor contains any elements that arenull
; otherwise throwing an exception with the specified message.Validate that the specified argument character sequence is neither
null
, a length of zero (no characters), empty nor whitespace; otherwise throwing an exception with the specified message.static <T> T[]
Validate that the specified argument array is neither
null
nor a length of zero (no elements); otherwise throwing an exception with the specified message.Validate that the specified argument collection is neither
null
nor a size of zero (no elements); otherwise throwing an exception with the specified message.static <T> T
Validate that the specified argument is not
null
; otherwise throwing an exception with the specified message.Validate that the specified char sequence is neither
null
, a length of zero (no characters), empty nor whitespace; otherwise throwing an exception with the specified message.static <T> T
Validate that the specified field/param is not
null
; otherwise throwing an exception with a precanned message that includes the parameter name.static void
Validate that the stateful condition is
true
; otherwise throwing an exception with the specified message.static <T> T
Validate the stateful predicate is true for the given object and return the object; otherwise throw an exception with the specified message.
Method Details
isTrue
public static void isTrue(boolean expression, String message, Object... values)Validate that the argument condition is
true
; otherwise throwing an exception with the specified message. This method is useful when validating according to an arbitrary boolean expression, such as validating a primitive number or using your own custom validation expression.Validate.isTrue(i >= min && i <= max, "The value must be between %d and %d", min, max); Validate.isTrue(myObject.isOk(), "The object is not okay");
Parameters:expression
- the boolean expression to checkmessage
- theString.format(String, Object...)
exception message if invalid, not nullvalues
- the optional values for the formatted exception message, null array not recommended Throws:IllegalArgumentException
- if expression isfalse
isFalse
public static void isFalse(boolean expression, String message, Object... values)Validate that the argument condition is
false
; otherwise throwing an exception with the specified message. This method is useful when validating according to an arbitrary boolean expression, such as validating a primitive number or using your own custom validation expression.Validate.isFalse(myObject.permitsSomething(), "The object is not allowed to permit something");
Parameters:expression
- the boolean expression to checkmessage
- theString.format(String, Object...)
exception message if not false, not nullvalues
- the optional values for the formatted exception message, null array not recommended Throws:IllegalArgumentException
- if expression istrue
notNull
public static <T> T notNull(T object, String message, Object... values)Validate that the specified argument is not
null
; otherwise throwing an exception with the specified message.Validate.notNull(myObject, "The object must not be null");
Type Parameters:T
- the object type Parameters:object
- the object to checkmessage
- theString.format(String, Object...)
exception message if invalid, not nullvalues
- the optional values for the formatted exception message Returns: the validated object (nevernull
for method chaining) Throws:NullPointerException
- if the object isnull
isNull
public static <T> void isNull(T object, String message, Object... values)Validate that the specified argument is
null
; otherwise throwing an exception with the specified message.Validate.isNull(myObject, "The object must be null");
Type Parameters:T
- the object type Parameters:object
- the object to checkmessage
- theString.format(String, Object...)
exception message if invalid, not nullvalues
- the optional values for the formatted exception message Throws:IllegalArgumentException
- if the object is notnull
paramNotNull
public static <T> T paramNotNull(T object, String paramName)Validate that the specified field/param is not
null
; otherwise throwing an exception with a precanned message that includes the parameter name.Validate.paramNotNull(myObject, "myObject");
Type Parameters:T
- the object type Parameters:object
- the object to checkparamName
- The name of the param or field being checked. Returns: the validated object (nevernull
for method chaining) Throws:NullPointerException
- if the object isnull
paramNotBlank
Validate that the specified char sequence is neither
null
, a length of zero (no characters), empty nor whitespace; otherwise throwing an exception with the specified message.Validate.paramNotBlank(myCharSequence, "myCharSequence");
Type Parameters:T
- the char sequence type Parameters:chars
- the character sequence to checkparamName
- The name of the param or field being checked. Returns: the validated char sequence (nevernull
for method chaining) Throws:NullPointerException
- if the char sequence isnull
validState
Validate the stateful predicate is true for the given object and return the object; otherwise throw an exception with the specified message.
String value = Validate.validState(someString, s -> s.length() == 0, "must be blank got: %s", someString);
Type Parameters:T
- the object type Parameters:object
- the object to checktest
- the predicate to apply, will return true if the object is validmessage
- theString.format(String, Object...)
exception message if invalid, not nullvalues
- the optional values for the formatted exception message Returns: the validated object Throws:NullPointerException
- if the object isnull
notEmpty
public static <T> T[] notEmpty(T[] array, String message, Object... values)Validate that the specified argument array is neither
null
nor a length of zero (no elements); otherwise throwing an exception with the specified message.Validate.notEmpty(myArray, "The array must not be empty");
Type Parameters:T
- the array type Parameters:array
- the array to check, validated not null by this methodmessage
- theString.format(String, Object...)
exception message if invalid, not nullvalues
- the optional values for the formatted exception message, null array not recommended Returns: the validated array (nevernull
method for chaining) Throws:NullPointerException
- if the array isnull
IllegalArgumentException
- if the array is emptynotEmpty
Validate that the specified argument collection is neither
null
nor a size of zero (no elements); otherwise throwing an exception with the specified message.Validate.notEmpty(myCollection, "The collection must not be empty");
Type Parameters:T
- the collection type Parameters:collection
- the collection to check, validated not null by this methodmessage
- theString.format(String, Object...)
exception message if invalid, not nullvalues
- the optional values for the formatted exception message, null array not recommended Returns: the validated collection (nevernull
method for chaining) Throws:NullPointerException
- if the collection isnull
IllegalArgumentException
- if the collection is emptynotEmpty
public static <T extends Map<?, ?>> T notEmpty(T map, String message, Object... values)Validate that the specified argument map is neither
null
nor a size of zero (no elements); otherwise throwing an exception with the specified message.Validate.notEmpty(myMap, "The map must not be empty");
Type Parameters:T
- the map type Parameters:map
- the map to check, validated not null by this methodmessage
- theString.format(String, Object...)
exception message if invalid, not nullvalues
- the optional values for the formatted exception message, null array not recommended Returns: the validated map (nevernull
method for chaining) Throws:NullPointerException
- if the map isnull
IllegalArgumentException
- if the map is emptynotEmpty
Validate that the specified argument character sequence is neither
null
nor a length of zero (no characters); otherwise throwing an exception with the specified message.Validate.notEmpty(myString, "The string must not be empty");
Type Parameters:T
- the character sequence type Parameters:chars
- the character sequence to check, validated not null by this methodmessage
- theString.format(String, Object...)
exception message if invalid, not nullvalues
- the optional values for the formatted exception message, null array not recommended Returns: the validated character sequence (nevernull
method for chaining) Throws:NullPointerException
- if the character sequence isnull
IllegalArgumentException
- if the character sequence is emptynotBlank
Validate that the specified argument character sequence is neither
null
, a length of zero (no characters), empty nor whitespace; otherwise throwing an exception with the specified message.Validate.notBlank(myString, "The string must not be blank");
Type Parameters:T
- the character sequence type Parameters:chars
- the character sequence to check, validated not null by this methodmessage
- theString.format(String, Object...)
exception message if invalid, not nullvalues
- the optional values for the formatted exception message, null array not recommended Returns: the validated character sequence (nevernull
method for chaining) Throws:NullPointerException
- if the character sequence isnull
IllegalArgumentException
- if the character sequence is blanknoNullElements
public static <T> T[] noNullElements(T[] array, String message, Object... values)Validate that the specified argument array is neither
null
nor contains any elements that arenull
; otherwise throwing an exception with the specified message.Validate.noNullElements(myArray, "The array is null or contains null.");
Type Parameters:T
- the array type Parameters:array
- the array to check, validated not null by this methodmessage
- theString.format(String, Object...)
exception message if invalid, not nullvalues
- the optional values for the formatted exception message Returns: the validated array (nevernull
method for chaining) Throws:NullPointerException
- if the array isnull
IllegalArgumentException
- if an element isnull
noNullElements
public static <T extends Iterable<?>> T noNullElements(T iterable, String message, Object... values)Validate that the specified argument iterable is neither
null
nor contains any elements that arenull
; otherwise throwing an exception with the specified message.Validate.noNullElements(myCollection, "The collection is null or contains null.");
Type Parameters:T
- the iterable type Parameters:iterable
- the iterable to check, validated not null by this methodmessage
- theString.format(String, Object...)
exception message if invalid, not nullvalues
- the optional values for the formatted exception message. Returns: the validated iterable (nevernull
method for chaining) Throws:NullPointerException
- if the array isnull
IllegalArgumentException
- if an element isnull
validState
public static void validState(boolean expression, String message, Object... values)Validate that the stateful condition is
true
; otherwise throwing an exception with the specified message. This method is useful when validating according to an arbitrary boolean expression, such as validating a primitive number or using your own custom validation expression.Validate.validState(this.isOk(), "The state is not OK: %s", myObject);
Parameters:expression
- the boolean expression to checkmessage
- theString.format(String, Object...)
exception message if invalid, not nullvalues
- the optional values for the formatted exception message, null array not recommended Throws:IllegalStateException
- if expression isfalse
inclusiveBetween
public static <T extends Comparable<U>, U> T inclusiveBetween(U start, U end, T value, String message, Object... values)Validate that the specified argument object fall between the two inclusive values specified; otherwise, throws an exception with the specified message.
Validate.inclusiveBetween(0, 2, 1, "Not in boundaries");
Type Parameters:T
- the type of the argument object Parameters:start
- the inclusive start value, not nullend
- the inclusive end value, not nullvalue
- the object to validate, not nullmessage
- theString.format(String, Object...)
exception message if invalid, not nullvalues
- the optional values for the formatted exception message, null array not recommended Throws:IllegalArgumentException
- if the value falls outside the boundariesinclusiveBetween
public static long inclusiveBetween(long start, long end, long value, String message) Validate that the specified primitive value falls between the two inclusive values specified; otherwise, throws an exception with the specified message.Validate.inclusiveBetween(0, 2, 1, "Not in range");
Parameters:start
- the inclusive start valueend
- the inclusive end valuevalue
- the value to validatemessage
- the exception message if invalid, not null Throws:IllegalArgumentException
- if the value falls outside the boundariesinclusiveBetween
public static double inclusiveBetween(double start, double end, double value, String message) Validate that the specified primitive value falls between the two inclusive values specified; otherwise, throws an exception with the specified message.Validate.inclusiveBetween(0.1, 2.1, 1.1, "Not in range");
Parameters:start
- the inclusive start valueend
- the inclusive end valuevalue
- the value to validatemessage
- the exception message if invalid, not null Throws:IllegalArgumentException
- if the value falls outside the boundariesexclusiveBetween
public static <T extends Comparable<U>, U> T exclusiveBetween(U start, U end, T value, String message, Object... values)Validate that the specified argument object fall between the two exclusive values specified; otherwise, throws an exception with the specified message.
Validate.exclusiveBetween(0, 2, 1, "Not in boundaries");
Type Parameters:T
- the type of the argument object Parameters:start
- the exclusive start value, not nullend
- the exclusive end value, not nullvalue
- the object to validate, not nullmessage
- theString.format(String, Object...)
exception message if invalid, not nullvalues
- the optional values for the formatted exception message, null array not recommended Throws:IllegalArgumentException
- if the value falls outside the boundariesexclusiveBetween
public static long exclusiveBetween(long start, long end, long value, String message) Validate that the specified primitive value falls between the two exclusive values specified; otherwise, throws an exception with the specified message.Validate.exclusiveBetween(0, 2, 1, "Not in range");
Parameters:start
- the exclusive start valueend
- the exclusive end valuevalue
- the value to validatemessage
- the exception message if invalid, not null Throws:IllegalArgumentException
- if the value falls outside the boundariesexclusiveBetween
public static double exclusiveBetween(double start, double end, double value, String message) Validate that the specified primitive value falls between the two exclusive values specified; otherwise, throws an exception with the specified message.Validate.exclusiveBetween(0.1, 2.1, 1.1, "Not in range");
Parameters:start
- the exclusive start valueend
- the exclusive end valuevalue
- the value to validatemessage
- the exception message if invalid, not null Throws:IllegalArgumentException
- if the value falls outside the boundariesisInstanceOf
public static <T, U> U isInstanceOf(Class<U> type, T obj, String message, Object... values)Validate that the argument is an instance of the specified class; otherwise throwing an exception with the specified message. This method is useful when validating according to an arbitrary class
Validate.isInstanceOf(OkClass.class, object, "Wrong class, object is of class %s", object.getClass().getName());
Parameters:type
- the class the object must be validated against, not nullobj
- the object to check, null throws an exceptionmessage
- theString.format(String, Object...)
exception message if invalid, not nullvalues
- the optional values for the formatted exception message, null array not recommended Throws:IllegalArgumentException
- if argument is not of specified classisAssignableFrom
Validates that the argument can be converted to the specified class, if not throws an exception.This method is useful when validating if there will be no casting errors.
Validate.isAssignableFrom(SuperClass.class, object.getClass());
The message of the exception is "The validated object can not be converted to the" followed by the name of the class and "class"
Parameters:superType
- the class the class must be validated against, not nulltype
- the class to check, not nullmessage
- theString.format(String, Object...)
exception message if invalid, not nullvalues
- the optional values for the formatted exception message, null array not recommended Throws:IllegalArgumentException
- if argument can not be converted to the specified classisPositive
public static int isPositive(int num, String fieldName)Asserts that the given number is positive (non-negative and non-zero).
Parameters:num
- Number to validatefieldName
- Field name to display in exception message if not positive. Returns: Number if positive.isPositive
public static long isPositive(long num, String fieldName)Asserts that the given number is positive (non-negative and non-zero).
Parameters:num
- Number to validatefieldName
- Field name to display in exception message if not positive. Returns: Number if positive.isPositive
public static double isPositive(double num, String fieldName)isNotNegative
public static int isNotNegative(int num, String fieldName)isNotNegativeOrNull
public static Long isNotNegativeOrNull(Long num, String fieldName)isNotNegative
public static long isNotNegative(long num, String fieldName)isPositive
Asserts that the given duration is positive (non-negative and non-zero).
Parameters:duration
- Number to validatefieldName
- Field name to display in exception message if not positive. Returns: Duration if positive.isPositiveOrNull
Asserts that the given duration is positive (non-negative and non-zero) or null.
Parameters:duration
- Number to validatefieldName
- Field name to display in exception message if not positive. Returns: Duration if positive or null.isPositiveOrNull
Asserts that the given boxed integer is positive (non-negative and non-zero) or null.
Parameters:num
- Boxed integer to validatefieldName
- Field name to display in exception message if not positive. Returns: Duration if positive or null.isPositiveOrNull
Asserts that the given boxed double is positive (non-negative and non-zero) or null.
Parameters:num
- Boxed double to validatefieldName
- Field name to display in exception message if not positive. Returns: Duration if double or null.isPositiveOrNull
Asserts that the given boxed long is positive (non-negative and non-zero) or null.
Parameters:num
- Boxed long to validatefieldName
- Field name to display in exception message if not positive. Returns: Duration if positive or null.isNotNegative
Asserts that the given duration is positive, including zero.
Parameters:duration
- Number to validatefieldName
- Field name to display in exception message if not positive. Returns: Duration if positive or zero.getOrDefault
public static <T> T getOrDefault(T param, Supplier<T> defaultValue) Returns the param if non null, otherwise gets a default value from the providedSupplier
. Type Parameters:T
- Type of value. Parameters:param
- Param to return if non null.defaultValue
- Supplier of default value. Returns: Value of param or default value if param was null.mutuallyExclusive
public static void mutuallyExclusive(String message, Object... objs)Verify that only one of the objects is non null. If all objects are null this method does not throw.
Parameters:message
- Error message if more than one object is non-null.objs
- Objects to validate. Throws:IllegalArgumentException
- if more than one of the objects was non-null.
ncG1vNJzZmirlKB7ornAs6anmaeoe6S7zGihmq6RZK6xtY6lmK2do6l8tLvFra6aqpVkrq6t2ailaJmnqMClt46uq6Kko2SjorjInZitnV6dwa64