public class Validate extends Object
This class assists in validating arguments.
The class is based along the lines of JUnit. If an argument value is deemed invalid, an IllegalArgumentException is thrown. For example:
Validate.isTrue( i bigger than 0, "The value must be greater than zero: ", i); Validate.notNull( surname, "The surname must not be null");
Constructor and Description |
---|
Validate()
Constructor.
|
Modifier and Type | Method and Description |
---|---|
static void |
allElementsOfType(Collection<?> collection,
Class<?> clazz)
Validate an argument, throwing
IllegalArgumentException if the argument collection is
null or has elements that are not of type clazz or a subclass. |
static void |
allElementsOfType(Collection<?> collection,
Class<?> clazz,
String message)
Validate an argument, throwing
IllegalArgumentException
if the argument collection is null or has elements that
are not of type clazz or a subclass. |
static void |
isTrue(boolean expression)
Validate that the argument condition is
true ; otherwise
throwing an exception. |
static void |
isTrue(boolean expression,
String message)
Validate that the argument condition is
true ; otherwise
throwing an exception with the specified message. |
static void |
isTrue(boolean expression,
String message,
double value)
Validate that the argument condition is
true ; otherwise
throwing an exception with the specified message. |
static void |
isTrue(boolean expression,
String message,
long value)
Validate that the argument condition is
true ; otherwise
throwing an exception with the specified message. |
static void |
isTrue(boolean expression,
String message,
Object value)
Validate that the argument condition is
true ; otherwise
throwing an exception with the specified message. |
static void |
noNullElements(Collection<?> collection)
Validate that the specified argument collection is neither
null nor contains any elements that are null ;
otherwise throwing an exception. |
static void |
noNullElements(Collection<?> collection,
String message)
Validate that the specified argument collection is neither
null nor contains any elements that are null ;
otherwise throwing an exception with the specified message. |
static void |
noNullElements(Object[] array)
Validate that the specified argument array is neither
null nor contains any elements that are null ;
otherwise throwing an exception. |
static void |
noNullElements(Object[] array,
String message)
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 void |
notEmpty(Collection<?> collection)
Validate that the specified argument collection is neither
null
nor a size of zero (no elements); otherwise throwing an exception. |
static void |
notEmpty(Collection<?> collection,
String 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 void |
notEmpty(Map<?,?> map)
Validate that the specified argument map is neither
null
nor a size of zero (no elements); otherwise throwing an exception. |
static void |
notEmpty(Map<?,?> map,
String message)
Validate that the specified argument map is neither
null
nor a size of zero (no elements); otherwise throwing an exception
with the specified message. |
static void |
notEmpty(Object[] array)
Validate that the specified argument array is neither
null
nor a length of zero (no elements); otherwise throwing an exception. |
static void |
notEmpty(Object[] array,
String message)
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 void |
notEmpty(String string)
Validate that the specified argument string is
neither
null nor a length of zero (no characters);
otherwise throwing an exception with the specified message. |
static void |
notEmpty(String string,
String message)
Validate that the specified argument string is
neither
null nor a length of zero (no characters);
otherwise throwing an exception with the specified message. |
static void |
notNull(Object object)
Validate that the specified argument is not
null ;
otherwise throwing an exception. |
static void |
notNull(Object object,
String message)
Validate that the specified argument is not
null ;
otherwise throwing an exception with the specified message. |
public Validate()
public static void isTrue(boolean expression, String message, Object value)
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 an
object or using your own custom validation expression.
Validate.isTrue( myObject.isOk(), "The object is not OK: ", myObject);
For performance reasons, the object value is passed as a separate parameter and appended to the exception message only in the case of an error.
expression
- the boolean expression to checkmessage
- the exception message if invalidvalue
- the value to append to the message when invalidIllegalArgumentException
- if expression is false
public static void isTrue(boolean expression, String message, long value)
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 bigger than 0.0, "The value must be greater than zero: ", i);
For performance reasons, the long value is passed as a separate parameter and appended to the exception message only in the case of an error.
expression
- the boolean expression to checkmessage
- the exception message if invalidvalue
- the value to append to the message when invalidIllegalArgumentException
- if expression is false
public static void isTrue(boolean expression, String message, double value)
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(d bigger than 0.0, "The value must be greater than zero: ", d);
For performance reasons, the double value is passed as a separate parameter and appended to the exception message only in the case of an error.
expression
- the boolean expression to checkmessage
- the exception message if invalidvalue
- the value to append to the message when invalidIllegalArgumentException
- if expression is false
public static void isTrue(boolean expression, String message)
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 bigger than 0), "The value must be greater than zero"); Validate.isTrue( myObject.isOk(), "The object is not OK");
expression
- the boolean expression to checkmessage
- the exception message if invalidIllegalArgumentException
- if expression is false
public static void isTrue(boolean expression)
Validate that the argument condition is true
; otherwise
throwing an exception. 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 bigger than 0); Validate.isTrue(myObject.isOk());
The message of the exception is "The validated expression is false".
expression
- the boolean expression to checkIllegalArgumentException
- if expression is false
public static void notNull(Object object)
Validate that the specified argument is not null
;
otherwise throwing an exception.
Validate.notNull(myObject);
The message of the exception is "The validated object is null".
object
- the object to checkIllegalArgumentException
- if the object is null
public static void notNull(Object object, String message)
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");
object
- the object to checkmessage
- the exception message if invalidpublic static void notEmpty(Object[] array, String message)
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");
array
- the array to checkmessage
- the exception message if invalidIllegalArgumentException
- if the array is emptypublic static void notEmpty(Object[] array)
Validate that the specified argument array is neither null
nor a length of zero (no elements); otherwise throwing an exception.
Validate.notEmpty(myArray);
The message in the exception is "The validated array is empty".
array
- the array to checkIllegalArgumentException
- if the array is emptypublic static void notEmpty(Collection<?> collection, String 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.
Validate.notEmpty(myCollection, "The collection must not be empty");
collection
- the collection to checkmessage
- the exception message if invalidIllegalArgumentException
- if the collection is emptypublic static void notEmpty(Collection<?> collection)
Validate that the specified argument collection is neither null
nor a size of zero (no elements); otherwise throwing an exception.
Validate.notEmpty(myCollection);
The message in the exception is "The validated collection is empty".
collection
- the collection to checkIllegalArgumentException
- if the collection is emptypublic static void notEmpty(Map<?,?> map, String message)
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");
map
- the map to checkmessage
- the exception message if invalidIllegalArgumentException
- if the map is emptypublic static void notEmpty(Map<?,?> map)
Validate that the specified argument map is neither null
nor a size of zero (no elements); otherwise throwing an exception.
Validate.notEmpty(myMap);
The message in the exception is "The validated map is empty".
map
- the map to checkIllegalArgumentException
- if the map is emptynotEmpty(Map, String)
public static void notEmpty(String string, String message)
Validate that the specified argument string 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");
string
- the string to checkmessage
- the exception message if invalidIllegalArgumentException
- if the string is emptypublic static void notEmpty(String string)
Validate that the specified argument string is
neither null
nor a length of zero (no characters);
otherwise throwing an exception with the specified message.
Validate.notEmpty(myString);
The message in the exception is "The validated string is empty".
string
- the string to checkIllegalArgumentException
- if the string is emptypublic static void noNullElements(Object[] array, String message)
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 contain null at position %d");
If the array is null
, then the message in the exception
is "The validated object is null".
array
- the array to checkmessage
- the exception message if the collection has null
elementsIllegalArgumentException
- if the array is null
or
an element in the array is null
public static void noNullElements(Object[] array)
Validate that the specified argument array is neither
null
nor contains any elements that are null
;
otherwise throwing an exception.
Validate.noNullElements(myArray);
If the array is null
, then the message in the exception
is "The validated object is null".
If the array has a null
element, then the message in the
exception is "The validated array contains null element at index:
" followed by the index.
array
- the array to checkIllegalArgumentException
- if the array is null
or
an element in the array is null
public static void noNullElements(Collection<?> collection, String message)
Validate that the specified argument collection is neither
null
nor contains any elements that are null
;
otherwise throwing an exception with the specified message.
Validate.noNullElements(myCollection, "The collection contains null elements");
If the collection is null
, then the message in the exception
is "The validated object is null".
collection
- the collection to checkmessage
- the exception message if the collection hasIllegalArgumentException
- if the collection is null
or
an element in the collection is null
public static void noNullElements(Collection<?> collection)
Validate that the specified argument collection is neither
null
nor contains any elements that are null
;
otherwise throwing an exception.
Validate.noNullElements(myCollection);
If the collection is null
, then the message in the exception
is "The validated object is null".
If the collection has a null
element, then the message in the
exception is "The validated collection contains null element at index:
" followed by the index.
collection
- the collection to checkIllegalArgumentException
- if the collection is null
or
an element in the collection is null
public static void allElementsOfType(Collection<?> collection, Class<?> clazz, String message)
Validate an argument, throwing IllegalArgumentException
if the argument collection is null
or has elements that
are not of type clazz
or a subclass.
Validate.allElementsOfType(collection, String.class, "Collection has invalid elements");
collection
- the collection to check, not nullclazz
- the Class
which the collection's elements are expected to be, not nullmessage
- the exception message if the Collection
has elements not of type clazz
public static void allElementsOfType(Collection<?> collection, Class<?> clazz)
Validate an argument, throwing IllegalArgumentException
if the argument collection is
null
or has elements that are not of type clazz
or a subclass.
Validate.allElementsOfType(collection, String.class);
The message in the exception is 'The validated collection contains an element not of type clazz at index: '.
collection
- the collection to check, not nullclazz
- the Class
which the collection's elements are expected to be, not nullCopyright © 2023. All rights reserved.