Class Validate
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 > 0, "The value must be greater than zero: ", i); Validate.notNull( surname, "The surname must not be null");
- Since:
- 2.0
- Version:
- $Id: Validate.java 1057051 2011-01-09 23:15:51Z sebb $
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidallElementsOfType(Collection collection, Class clazz) Validate an argument, throwingIllegalArgumentExceptionif the argument collection isnullor has elements that are not of typeclazzor a subclass.static voidallElementsOfType(Collection collection, Class clazz, String message) Validate an argument, throwingIllegalArgumentExceptionif the argument collection isnullor has elements that are not of typeclazzor a subclass.static voidisTrue(boolean expression) Validate that the argument condition istrue; otherwise throwing an exception.static voidValidate that the argument condition istrue; otherwise throwing an exception with the specified message.static voidValidate that the argument condition istrue; otherwise throwing an exception with the specified message.static voidValidate that the argument condition istrue; otherwise throwing an exception with the specified message.static voidValidate that the argument condition istrue; otherwise throwing an exception with the specified message.static voidnoNullElements(Object[] array) Validate that the specified argument array is neithernullnor contains any elements that arenull; otherwise throwing an exception.static voidnoNullElements(Object[] array, String message) Validate that the specified argument array is neithernullnor contains any elements that arenull; otherwise throwing an exception with the specified message.static voidnoNullElements(Collection collection) Validate that the specified argument collection is neithernullnor contains any elements that arenull; otherwise throwing an exception.static voidnoNullElements(Collection collection, String message) Validate that the specified argument collection is neithernullnor contains any elements that arenull; otherwise throwing an exception with the specified message.static voidValidate that the specified argument array is neithernullnor a length of zero (no elements); otherwise throwing an exception.static voidValidate that the specified argument array is neithernullnor a length of zero (no elements); otherwise throwing an exception with the specified message.static voidValidate that the specified argument string is neithernullnor a length of zero (no characters); otherwise throwing an exception with the specified message.static voidValidate that the specified argument string is neithernullnor a length of zero (no characters); otherwise throwing an exception with the specified message.static voidnotEmpty(Collection collection) Validate that the specified argument collection is neithernullnor a size of zero (no elements); otherwise throwing an exception.static voidnotEmpty(Collection collection, String message) Validate that the specified argument collection is neithernullnor a size of zero (no elements); otherwise throwing an exception with the specified message.static voidValidate that the specified argument map is neithernullnor a size of zero (no elements); otherwise throwing an exception.static voidValidate that the specified argument map is neithernullnor a size of zero (no elements); otherwise throwing an exception with the specified message.static voidValidate that the specified argument is notnull; otherwise throwing an exception.static voidValidate that the specified argument is notnull; otherwise throwing an exception with the specified message.
-
Constructor Details
-
Validate
public Validate()Constructor. This class should not normally be instantiated.
-
-
Method Details
-
isTrue
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.
- Parameters:
expression- the boolean expression to checkmessage- the exception message if invalidvalue- the value to append to the message when invalid- Throws:
IllegalArgumentException- if expression isfalse
-
isTrue
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 > 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.
- Parameters:
expression- the boolean expression to checkmessage- the exception message if invalidvalue- the value to append to the message when invalid- Throws:
IllegalArgumentException- if expression isfalse
-
isTrue
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 > 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.
- Parameters:
expression- the boolean expression to checkmessage- the exception message if invalidvalue- the value to append to the message when invalid- Throws:
IllegalArgumentException- if expression isfalse
-
isTrue
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 > 0), "The value must be greater than zero"); Validate.isTrue( myObject.isOk(), "The object is not OK");
- Parameters:
expression- the boolean expression to checkmessage- the exception message if invalid- Throws:
IllegalArgumentException- if expression isfalse
-
isTrue
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 > 0); Validate.isTrue(myObject.isOk());
The message of the exception is "The validated expression is false".
- Parameters:
expression- the boolean expression to check- Throws:
IllegalArgumentException- if expression isfalse
-
notNull
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".
- Parameters:
object- the object to check- Throws:
IllegalArgumentException- if the object isnull
-
notNull
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");
- Parameters:
object- the object to checkmessage- the exception message if invalid
-
notEmpty
Validate that the specified argument array is neither
nullnor a length of zero (no elements); otherwise throwing an exception with the specified message.Validate.notEmpty(myArray, "The array must not be empty");
- Parameters:
array- the array to checkmessage- the exception message if invalid- Throws:
IllegalArgumentException- if the array is empty
-
notEmpty
Validate that the specified argument array is neither
nullnor a length of zero (no elements); otherwise throwing an exception.Validate.notEmpty(myArray);
The message in the exception is "The validated array is empty".
- Parameters:
array- the array to check- Throws:
IllegalArgumentException- if the array is empty
-
notEmpty
Validate that the specified argument collection is neither
nullnor a size of zero (no elements); otherwise throwing an exception with the specified message.Validate.notEmpty(myCollection, "The collection must not be empty");
- Parameters:
collection- the collection to checkmessage- the exception message if invalid- Throws:
IllegalArgumentException- if the collection is empty
-
notEmpty
Validate that the specified argument collection is neither
nullnor a size of zero (no elements); otherwise throwing an exception.Validate.notEmpty(myCollection);
The message in the exception is "The validated collection is empty".
- Parameters:
collection- the collection to check- Throws:
IllegalArgumentException- if the collection is empty
-
notEmpty
Validate that the specified argument map is neither
nullnor a size of zero (no elements); otherwise throwing an exception with the specified message.Validate.notEmpty(myMap, "The map must not be empty");
- Parameters:
map- the map to checkmessage- the exception message if invalid- Throws:
IllegalArgumentException- if the map is empty
-
notEmpty
Validate that the specified argument map is neither
nullnor a size of zero (no elements); otherwise throwing an exception.Validate.notEmpty(myMap);
The message in the exception is "The validated map is empty".
- Parameters:
map- the map to check- Throws:
IllegalArgumentException- if the map is empty- See Also:
-
notEmpty
Validate that the specified argument string is neither
nullnor a length of zero (no characters); otherwise throwing an exception with the specified message.Validate.notEmpty(myString, "The string must not be empty");
- Parameters:
string- the string to checkmessage- the exception message if invalid- Throws:
IllegalArgumentException- if the string is empty
-
notEmpty
Validate that the specified argument string is neither
nullnor 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".
- Parameters:
string- the string to check- Throws:
IllegalArgumentException- if the string is empty
-
noNullElements
Validate that the specified argument array is neither
nullnor contains any elements that arenull; 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".- Parameters:
array- the array to checkmessage- the exception message if the collection hasnullelements- Throws:
IllegalArgumentException- if the array isnullor an element in the array isnull
-
noNullElements
Validate that the specified argument array is neither
nullnor contains any elements that arenull; 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
nullelement, then the message in the exception is "The validated array contains null element at index: invalid input: '"' followed by the index.- Parameters:
array- the array to check- Throws:
IllegalArgumentException- if the array isnullor an element in the array isnull
-
noNullElements
Validate that the specified argument collection is neither
nullnor contains any elements that arenull; 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".- Parameters:
collection- the collection to checkmessage- the exception message if the collection has- Throws:
IllegalArgumentException- if the collection isnullor an element in the collection isnull
-
noNullElements
Validate that the specified argument collection is neither
nullnor contains any elements that arenull; 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
nullelement, then the message in the exception is "The validated collection contains null element at index: invalid input: '"' followed by the index.- Parameters:
collection- the collection to check- Throws:
IllegalArgumentException- if the collection isnullor an element in the collection isnull
-
allElementsOfType
Validate an argument, throwing
IllegalArgumentExceptionif the argument collection isnullor has elements that are not of typeclazzor a subclass.Validate.allElementsOfType(collection, String.class, "Collection has invalid elements");
- Parameters:
collection- the collection to check, not nullclazz- theClasswhich the collection's elements are expected to be, not nullmessage- the exception message if theCollectionhas elements not of typeclazz- Since:
- 2.1
-
allElementsOfType
Validate an argument, throwing
IllegalArgumentExceptionif the argument collection isnullor has elements that are not of typeclazzor 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: '.
- Parameters:
collection- the collection to check, not nullclazz- theClasswhich the collection's elements are expected to be, not null- Since:
- 2.1
-