public class Validate extends Object
This class assists in validating arguments. The validation methods are based along the following principles:
null argument causes a NullPointerException.null argument causes an IllegalArgumentException.IndexOutOfBoundsException.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#
String.format(String, Object...)| Constructor | Description |
|---|---|
Validate() |
Constructor.
|
| Modifier and Type | Method | Description |
|---|---|---|
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.
|
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.
|
static <T extends Comparable<U>,U> |
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.
|
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.
|
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.
|
static <T extends Comparable<U>,U> |
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.
|
static void |
isAssignableFrom(Class<?> superType,
Class<?> type,
String message,
Object... values) |
Validates that the argument can be converted to the specified class, if not throws an exception.
|
static String |
isInstanceOf(Class<?> type,
Object obj,
String message,
Object... values) |
Validate that the argument is an instance of the specified class; otherwise
throwing an exception with the specified message.
|
static void |
isTrue(boolean expression,
String message,
Object... values) |
Validate that the argument condition is
true; otherwise
throwing an exception with the specified message. |
static <T> T[] |
noNullElements(T[] array,
String message,
Object... values) |
Validate that the specified argument array is neither
null nor contains any elements that are null;
otherwise throwing an exception with the specified message. |
static <T extends Iterable<?>> |
noNullElements(T iterable,
String message,
Object... values) |
Validate that the specified argument iterable is neither
null nor contains any elements that are null;
otherwise throwing an exception with the specified message. |
static <T extends CharSequence> |
notBlank(T chars,
String message,
Object... values) |
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[] |
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. |
static <T extends Collection<?>> |
notEmpty(T collection,
String message,
Object... values) |
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 |
notNull(T object,
String message,
Object... values) |
Validate that the specified argument is not
null;
otherwise throwing an exception with the specified message. |
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. |
static void |
validState(boolean expression,
String message,
Object... values) |
Validate that the stateful condition is
true; otherwise
throwing an exception with the specified message. |
public Validate()
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");
expression - the boolean expression to checkmessage - the String.format(String, Object...) exception message if invalid, not nullvalues - the optional values for the formatted exception message, null array not recommendedIllegalArgumentException - if expression is falsepublic 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");
T - the object typeobject - the object to checkmessage - the String.format(String, Object...) exception message if invalid, not nullvalues - the optional values for the formatted exception messagenull for method chaining)NullPointerException - if the object is nullpublic 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");
T - the object typeobject - the object to checkparamName - The name of the param or field being checked.null for method chaining)NullPointerException - if the object is nullpublic 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");
T - the array typearray - the array to check, validated not null by this methodmessage - the String.format(String, Object...) exception message if invalid, not nullvalues - the optional values for the formatted exception message, null array not recommendednull method for chaining)NullPointerException - if the array is nullIllegalArgumentException - if the array is emptypublic static <T extends Collection<?>> T notEmpty(T collection, String message, Object... values)
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");
T - the collection typecollection - the collection to check, validated not null by this methodmessage - the String.format(String, Object...) exception message if invalid, not nullvalues - the optional values for the formatted exception message, null array not recommendednull method for chaining)NullPointerException - if the collection is nullIllegalArgumentException - if the collection is emptypublic 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");
T - the map typemap - the map to check, validated not null by this methodmessage - the String.format(String, Object...) exception message if invalid, not nullvalues - the optional values for the formatted exception message, null array not recommendednull method for chaining)NullPointerException - if the map is nullIllegalArgumentException - if the map is emptypublic static <T extends CharSequence> T notEmpty(T chars, String message, Object... values)
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");
T - the character sequence typechars - the character sequence to check, validated not null by this methodmessage - the String.format(String, Object...) exception message if invalid, not nullvalues - the optional values for the formatted exception message, null array not recommendednull method for chaining)NullPointerException - if the character sequence is nullIllegalArgumentException - if the character sequence is emptypublic static <T extends CharSequence> T notBlank(T chars, String message, Object... values)
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");
T - the character sequence typechars - the character sequence to check, validated not null by this methodmessage - the String.format(String, Object...) exception message if invalid, not nullvalues - the optional values for the formatted exception message, null array not recommendednull method for chaining)NullPointerException - if the character sequence is nullIllegalArgumentException - if the character sequence is blankpublic static <T> T[] noNullElements(T[] array,
String message,
Object... values)
Validate that the specified argument array is neither
null nor contains any elements that are null;
otherwise throwing an exception with the specified message.
Validate.noNullElements(myArray, "The array is null or contains null.");
T - the array typearray - the array to check, validated not null by this methodmessage - the String.format(String, Object...) exception message if invalid, not nullvalues - the optional values for the formatted exception messagenull method for chaining)NullPointerException - if the array is nullIllegalArgumentException - if an element is nullpublic 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 are null;
otherwise throwing an exception with the specified message.
Validate.noNullElements(myCollection, "The collection is null or contains null.");
T - the iterable typeiterable - the iterable to check, validated not null by this methodmessage - the String.format(String, Object...) exception message if invalid, not nullvalues - the optional values for the formatted exception message.null method for chaining)NullPointerException - if the array is nullIllegalArgumentException - if an element is nullpublic 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);
expression - the boolean expression to checkmessage - the String.format(String, Object...) exception message if invalid, not nullvalues - the optional values for the formatted exception message, null array not recommendedIllegalStateException - if expression is falsepublic 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");
T - the type of the argument objectstart - the inclusive start value, not nullend - the inclusive end value, not nullvalue - the object to validate, not nullmessage - the String.format(String, Object...) exception message if invalid, not nullvalues - the optional values for the formatted exception message, null array not recommendedIllegalArgumentException - if the value falls outside the boundariespublic static long inclusiveBetween(long start,
long end,
long value,
String message)
Validate.inclusiveBetween(0, 2, 1, "Not in range");
start - the inclusive start valueend - the inclusive end valuevalue - the value to validatemessage - the exception message if invalid, not nullIllegalArgumentException - if the value falls outside the boundariespublic static double inclusiveBetween(double start,
double end,
double value,
String message)
Validate.inclusiveBetween(0.1, 2.1, 1.1, "Not in range");
start - the inclusive start valueend - the inclusive end valuevalue - the value to validatemessage - the exception message if invalid, not nullIllegalArgumentException - if the value falls outside the boundariespublic 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");
T - the type of the argument objectstart - the exclusive start value, not nullend - the exclusive end value, not nullvalue - the object to validate, not nullmessage - the String.format(String, Object...) exception message if invalid, not nullvalues - the optional values for the formatted exception message, null array not recommendedIllegalArgumentException - if the value falls outside the boundariespublic static long exclusiveBetween(long start,
long end,
long value,
String message)
Validate.exclusiveBetween(0, 2, 1, "Not in range");
start - the exclusive start valueend - the exclusive end valuevalue - the value to validatemessage - the exception message if invalid, not nullIllegalArgumentException - if the value falls outside the boundariespublic static double exclusiveBetween(double start,
double end,
double value,
String message)
Validate.exclusiveBetween(0.1, 2.1, 1.1, "Not in range");
start - the exclusive start valueend - the exclusive end valuevalue - the value to validatemessage - the exception message if invalid, not nullIllegalArgumentException - if the value falls outside the boundariespublic static String isInstanceOf(Class<?> type, Object 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());
type - the class the object must be validated against, not nullobj - the object to check, null throws an exceptionmessage - the String.format(String, Object...) exception message if invalid, not nullvalues - the optional values for the formatted exception message, null array not recommendedIllegalArgumentException - if argument is not of specified classpublic static void isAssignableFrom(Class<?> superType, Class<?> type, String message, Object... values)
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"
superType - the class the class must be validated against, not nulltype - the class to check, not nullmessage - the String.format(String, Object...) exception message if invalid, not nullvalues - the optional values for the formatted exception message, null array not recommendedIllegalArgumentException - if argument can not be converted to the specified classCopyright © 2017 Amazon Web Services, Inc. All Rights Reserved.