Klasse Validate
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");
- Seit:
- 2.0
- Version:
- $Id$
- Autor:
- Ola Berg, Stephen Colebourne, Gary Gregory, Norm Deane
-
Konstruktorübersicht
Konstruktoren -
Methodenübersicht
Modifizierer und TypMethodeBeschreibungstatic void
allElementsOfType
(Collection<?> collection, Class<?> clazz) Validate an argument, throwingIllegalArgumentException
if the argument collection isnull
or has elements that are not of typeclazz
or a subclass.static void
allElementsOfType
(Collection<?> collection, Class<?> clazz, String message) Validate an argument, throwingIllegalArgumentException
if the argument collection isnull
or has elements that are not of typeclazz
or a subclass.static void
isTrue
(boolean expression) Validate an argument, throwingIllegalArgumentException
if the test result isfalse
.static void
Validate an argument, throwingIllegalArgumentException
if the test result isfalse
.static void
Validate an argument, throwingIllegalArgumentException
if the test result isfalse
.static void
Validate an argument, throwingIllegalArgumentException
if the test result isfalse
.static void
Validate an argument, throwingIllegalArgumentException
if the test result isfalse
.static void
noNullElements
(Object[] array) Validate an argument, throwingIllegalArgumentException
if the argument array hasnull
elements or isnull
.static void
noNullElements
(Object[] array, String message) Validate an argument, throwingIllegalArgumentException
if the argument array hasnull
elements or isnull
.static void
noNullElements
(Collection<?> collection) Validate an argument, throwingIllegalArgumentException
if the argument Collection hasnull
elements or isnull
.static void
noNullElements
(Collection<?> collection, String message) Validate an argument, throwingIllegalArgumentException
if the argument Collection hasnull
elements or isnull
.static void
Validate an argument, throwingIllegalArgumentException
if the argument array is empty (null
or no elements).static void
Validate an argument, throwingIllegalArgumentException
if the argument array is empty (null
or no elements).static void
Validate an argument, throwingIllegalArgumentException
if the argument String is empty (null
or zero length).static void
Validate an argument, throwingIllegalArgumentException
if the argument String is empty (null
or zero length).static void
notEmpty
(Collection<?> collection) Validate an argument, throwingIllegalArgumentException
if the argument Collection is empty (null
or no elements).static void
notEmpty
(Collection<?> collection, String message) Validate an argument, throwingIllegalArgumentException
if the argument Collection is empty (null
or no elements).static void
Validate an argument, throwingIllegalArgumentException
if the argument Map is empty (null
or no elements).static void
Validate an argument, throwingIllegalArgumentException
if the argument Map is empty (null
or no elements).static void
Validate an argument, throwingIllegalArgumentException
if the argument isnull
.static void
Validate an argument, throwingIllegalArgumentException
if the argument isnull
.
-
Konstruktordetails
-
Validate
public Validate()Constructor. This class should not normally be instantiated.
-
-
Methodendetails
-
isTrue
Validate an argument, throwing
IllegalArgumentException
if the test result isfalse
.This is used when validating according to an arbitrary boolean expression, such as validating a primitive number or using your own custom validation expression.
Validate.isTrue( myObject.isOk(), "The object is not OK: ", myObject);
For performance reasons, the object is passed as a separate parameter and appended to the message string only in the case of an error.
- Parameter:
expression
- a boolean expressionmessage
- the exception message you would like to see if the expression isfalse
value
- the value to append to the message in case of error- Löst aus:
IllegalArgumentException
- if expression isfalse
-
isTrue
Validate an argument, throwing
IllegalArgumentException
if the test result isfalse
.This is used 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: ", i);
For performance reasons, the long value is passed as a separate parameter and appended to the message string only in the case of an error.
- Parameter:
expression
- a boolean expressionmessage
- the exception message you would like to see if the expression isfalse
value
- the value to append to the message in case of error- Löst aus:
IllegalArgumentException
- if expression isfalse
-
isTrue
Validate an argument, throwing
IllegalArgumentException
if the test result isfalse
.This is used 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 message string only in the case of an error.
- Parameter:
expression
- a boolean expressionmessage
- the exception message you would like to see if the expression isfalse
value
- the value to append to the message in case of error- Löst aus:
IllegalArgumentException
- if expression isfalse
-
isTrue
Validate an argument, throwing
IllegalArgumentException
if the test result isfalse
.This is used 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");
For performance reasons, the message string should not involve a string append, instead use the
isTrue(boolean, String, Object)
method.- Parameter:
expression
- a boolean expressionmessage
- the exception message you would like to see if the expression isfalse
- Löst aus:
IllegalArgumentException
- if expression isfalse
-
isTrue
public static void isTrue(boolean expression) Validate an argument, throwing
IllegalArgumentException
if the test result isfalse
.This is used 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 in the exception is 'The validated expression is false'.
- Parameter:
expression
- a boolean expression- Löst aus:
IllegalArgumentException
- if expression isfalse
-
notNull
Validate an argument, throwing
IllegalArgumentException
if the argument isnull
.Validate.notNull(myObject, "The object must not be null");
- Parameter:
object
- the object to check is notnull
message
- the exception message you would like to see if the object isnull
- Löst aus:
IllegalArgumentException
- if the object isnull
-
notNull
Validate an argument, throwing
IllegalArgumentException
if the argument isnull
.Validate.notNull(myObject);
The message in the exception is 'The validated object is null'.
- Parameter:
object
- the object to check is notnull
- Löst aus:
IllegalArgumentException
- if the object isnull
-
notEmpty
Validate an argument, throwing
IllegalArgumentException
if the argument array is empty (null
or no elements).Validate.notEmpty(myArray, "The array must not be empty");
- Parameter:
array
- the array to check is not emptymessage
- the exception message you would like to see if the array is empty- Löst aus:
IllegalArgumentException
- if the array is empty
-
notEmpty
Validate an argument, throwing
IllegalArgumentException
if the argument array is empty (null
or no elements).Validate.notEmpty(myArray);
The message in the exception is 'The validated array is empty'.
- Parameter:
array
- the array to check is not empty- Löst aus:
IllegalArgumentException
- if the array is empty
-
notEmpty
Validate an argument, throwing
IllegalArgumentException
if the argument Collection is empty (null
or no elements).Validate.notEmpty(myCollection, "The collection must not be empty");
- Parameter:
collection
- the collection to check is not emptymessage
- the exception message you would like to see if the collection is empty- Löst aus:
IllegalArgumentException
- if the collection is empty
-
notEmpty
Validate an argument, throwing
IllegalArgumentException
if the argument Collection is empty (null
or no elements).Validate.notEmpty(myCollection);
The message in the exception is 'The validated collection is empty'.
- Parameter:
collection
- the collection to check is not empty- Löst aus:
IllegalArgumentException
- if the collection is empty
-
notEmpty
Validate an argument, throwing
IllegalArgumentException
if the argument Map is empty (null
or no elements).Validate.notEmpty(myMap, "The map must not be empty");
- Parameter:
map
- the map to check is not emptymessage
- the exception message you would like to see if the map is empty- Löst aus:
IllegalArgumentException
- if the map is empty
-
notEmpty
Validate an argument, throwing
IllegalArgumentException
if the argument Map is empty (null
or no elements).Validate.notEmpty(myMap);
The message in the exception is 'The validated map is empty'.
- Parameter:
map
- the map to check is not empty- Löst aus:
IllegalArgumentException
- if the map is empty
-
notEmpty
Validate an argument, throwing
IllegalArgumentException
if the argument String is empty (null
or zero length).Validate.notEmpty(myString, "The string must not be empty");
- Parameter:
string
- the string to check is not emptymessage
- the exception message you would like to see if the string is empty- Löst aus:
IllegalArgumentException
- if the string is empty
-
notEmpty
Validate an argument, throwing
IllegalArgumentException
if the argument String is empty (null
or zero length).Validate.notEmpty(myString);
The message in the exception is 'The validated string is empty'.
- Parameter:
string
- the string to check is not empty- Löst aus:
IllegalArgumentException
- if the string is empty
-
noNullElements
Validate an argument, throwing
IllegalArgumentException
if the argument array hasnull
elements or isnull
.Validate.noNullElements(myArray, "The array must not contain null elements");
If the array is null then the message in the exception is 'The validated object is null'.
- Parameter:
array
- the array to checkmessage
- the exception message if the array hasnull
elements- Löst aus:
IllegalArgumentException
- if the array hasnull
elements or isnull
-
noNullElements
Validate an argument, throwing
IllegalArgumentException
if the argument array hasnull
elements or isnull
.Validate.noNullElements(myArray);
If the array has a null element the message in the exception is 'The validated array contains null element at index: '.
If the array is null then the message in the exception is 'The validated object is null'.
- Parameter:
array
- the array to check- Löst aus:
IllegalArgumentException
- if the array hasnull
elements or isnull
-
noNullElements
Validate an argument, throwing
IllegalArgumentException
if the argument Collection hasnull
elements or isnull
.Validate.noNullElements(myCollection, "The collection must not contain null elements");
If the collection is null then the message in the exception is 'The validated object is null'.
- Parameter:
collection
- the collection to checkmessage
- the exception message if the collection hasnull
elements- Löst aus:
IllegalArgumentException
- if the collection hasnull
elements or isnull
-
noNullElements
Validate an argument, throwing
IllegalArgumentException
if the argument Collection hasnull
elements or isnull
.Validate.noNullElements(myCollection);
The message in the exception is 'The validated collection contains null element at index: '.
If the collection is null then the message in the exception is 'The validated object is null'.
- Parameter:
collection
- the collection to check- Löst aus:
IllegalArgumentException
- if the collection hasnull
elements or isnull
-
allElementsOfType
Validate an argument, throwing
IllegalArgumentException
if the argument collection isnull
or has elements that are not of typeclazz
or a subclass.Validate.allElementsOfType(collection, String.class, "Collection has invalid elements");
- Parameter:
collection
- the collection to check, not nullclazz
- theClass
which the collection's elements are expected to be, not nullmessage
- the exception message if theCollection
has elements not of typeclazz
- Seit:
- 2.1
-
allElementsOfType
Validate an argument, throwing
IllegalArgumentException
if the argument collection isnull
or has elements that are not of typeclazz
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: '.
- Parameter:
collection
- the collection to check, not nullclazz
- theClass
which the collection's elements are expected to be, not null- Seit:
- 2.1
-