org.apache.commons.lang

Class Validate


public class Validate
extends Object

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

Constructor Summary

Validate()
Constructor.

Method Summary

static void
allElementsOfType(Collection<E> collection, Class<T> 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<E> collection, Class<T> 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 an argument, throwing IllegalArgumentException if the test result is false.
static void
isTrue(boolean expression, String message)
Validate an argument, throwing IllegalArgumentException if the test result is false.
static void
isTrue(boolean expression, String message, double value)
Validate an argument, throwing IllegalArgumentException if the test result is false.
static void
isTrue(boolean expression, String message, Object value)
Validate an argument, throwing IllegalArgumentException if the test result is false.
static void
isTrue(boolean expression, String message, long value)
Validate an argument, throwing IllegalArgumentException if the test result is false.
static void
noNullElements(Object[] array)
Validate an argument, throwing IllegalArgumentException if the argument array has null elements or is null.
static void
noNullElements(Object[] array, String message)
Validate an argument, throwing IllegalArgumentException if the argument array has null elements or is null.
static void
noNullElements(Collection<E> collection)
Validate an argument, throwing IllegalArgumentException if the argument Collection has null elements or is null.
static void
noNullElements(Collection<E> collection, String message)
Validate an argument, throwing IllegalArgumentException if the argument Collection has null elements or is null.
static void
notEmpty(Object[] array)
Validate an argument, throwing IllegalArgumentException if the argument array is empty (null or no elements).
static void
notEmpty(Object[] array, String message)
Validate an argument, throwing IllegalArgumentException if the argument array is empty (null or no elements).
static void
notEmpty(String string)
Validate an argument, throwing IllegalArgumentException if the argument String is empty (null or zero length).
static void
notEmpty(String string, String message)
Validate an argument, throwing IllegalArgumentException if the argument String is empty (null or zero length).
static void
notEmpty(Collection<E> collection)
Validate an argument, throwing IllegalArgumentException if the argument Collection is empty (null or no elements).
static void
notEmpty(Collection<E> collection, String message)
Validate an argument, throwing IllegalArgumentException if the argument Collection is empty (null or no elements).
static void
notEmpty(Map<K,V> map)
Validate an argument, throwing IllegalArgumentException if the argument Map is empty (null or no elements).
static void
notEmpty(Map<K,V> map, String message)
Validate an argument, throwing IllegalArgumentException if the argument Map is empty (null or no elements).
static void
notNull(Object object)
Validate an argument, throwing IllegalArgumentException if the argument is null.
static void
notNull(Object object, String message)
Validate an argument, throwing IllegalArgumentException if the argument is null.

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

Validate

public Validate()
Constructor. This class should not normally be instantiated.

Method Details

allElementsOfType

public static void allElementsOfType(Collection<E> collection,
                                     Class<T> 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: '.

Parameters:
collection - the collection to check, not null
clazz - the Class which the collection's elements are expected to be, not null
Since:
2.1

allElementsOfType

public static void allElementsOfType(Collection<E> collection,
                                     Class<T> 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");
 
Parameters:
collection - the collection to check, not null
clazz - the Class which the collection's elements are expected to be, not null
message - the exception message if the Collection has elements not of type clazz
Since:
2.1

isTrue

public static void isTrue(boolean expression)
Validate an argument, throwing IllegalArgumentException if the test result is false.

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'.

Parameters:
expression - a boolean expression
Throws:
IllegalArgumentException - if expression is false
Usages and Demos :

View More Examples of isTrue(boolean expression)
   1: 
   2: import org.apache.commons.lang.Validate;
   3: 
   4:         ...
   5: 
   6:         Validate.isTrue(3 == model.getColumnCount());
   7:         ...
   8:         Validate.isTrue(model.getColumnCount() == ProgressMonitorModel.COLUMN_HEADER.length);

View Full Code Here
   1: 
   2: import org.apache.commons.lang.Validate;
   3: import org.apache.commons.logging.Log;
   4:         ...
   5: 
   6:         Validate.isTrue(_tabbedPanel.getTabCount() == 0);
   7: 
   8:         tabbedPanel = _tabbedPanel;
   9:         tabGroup = _tabGroup;

View Full Code Here
   1: 
   2: import org.apache.commons.lang.Validate;
   3: import org.apache.commons.logging.Log;
   4:         ...
   5: 
   6:         Validate.isTrue(_tabbedPanel.getTabCount() == 0);
   7: 
   8:         tabbedPanel = _tabbedPanel;
   9:         tabGroup = _tabGroup;

View Full Code Here
   1: 
   2: import org.apache.commons.lang.Validate;
   3: 
   4:         ...
   5:     public synchronized void addNewTask(final boolean batch, final Task[] tasks) {
   6:         Validate.notNull(tasks);
   7:         ...
   8:         Validate.isTrue(tasks.length > 0);

View Full Code Here
   1: 
   2: import org.apache.commons.lang.Validate;
   3: 
   4:         ...
   5:                 foundDescriptions.add(format == null ? null : formatDescription);
   6:                 Validate.isTrue(foundFormats.size() == fmtCount);
   7:         ...
   8:                 Validate.isTrue(foundDescriptions.size() == fmtCount);
   9:                 if (c[pos.getIndex()] != END_FE) {

View Full Code Here

isTrue

public static void isTrue(boolean expression,
                          String message)
Validate an argument, throwing IllegalArgumentException if the test result is false.

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.

Parameters:
expression - a boolean expression
message - the exception message you would like to see if the expression is false
Throws:
IllegalArgumentException - if expression is false

isTrue

public static void isTrue(boolean expression,
                          String message,
                          double value)
Validate an argument, throwing IllegalArgumentException if the test result is false.

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.

Parameters:
expression - a boolean expression
message - the exception message you would like to see if the expression is false
value - the value to append to the message in case of error
Throws:
IllegalArgumentException - if expression is false

isTrue

public static void isTrue(boolean expression,
                          String message,
                          Object value)
Validate an argument, throwing IllegalArgumentException if the test result is false.

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.

Parameters:
expression - a boolean expression
message - the exception message you would like to see if the expression is false
value - the value to append to the message in case of error
Throws:
IllegalArgumentException - if expression is false

isTrue

public static void isTrue(boolean expression,
                          String message,
                          long value)
Validate an argument, throwing IllegalArgumentException if the test result is false.

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.

Parameters:
expression - a boolean expression
message - the exception message you would like to see if the expression is false
value - the value to append to the message in case of error
Throws:
IllegalArgumentException - if expression is false

noNullElements

public static void noNullElements(Object[] array)
Validate an argument, throwing IllegalArgumentException if the argument array has null elements or is null.
 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'.

Parameters:
array - the array to check
Throws:
IllegalArgumentException - if the array has null elements or is null

noNullElements

public static void noNullElements(Object[] array,
                                  String message)
Validate an argument, throwing IllegalArgumentException if the argument array has null elements or is null.
 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'.

Parameters:
array - the array to check
message - the exception message if the array has null elements
Throws:
IllegalArgumentException - if the array has null elements or is null

noNullElements

public static void noNullElements(Collection<E> collection)
Validate an argument, throwing IllegalArgumentException if the argument Collection has null elements or is null.
 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'.

Parameters:
collection - the collection to check
Throws:
IllegalArgumentException - if the collection has null elements or is null

noNullElements

public static void noNullElements(Collection<E> collection,
                                  String message)
Validate an argument, throwing IllegalArgumentException if the argument Collection has null elements or is null.
 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'.

Parameters:
collection - the collection to check
message - the exception message if the collection has null elements
Throws:
IllegalArgumentException - if the collection has null elements or is null

notEmpty

public static void notEmpty(Object[] array)
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'.

Parameters:
array - the array to check is not empty
Throws:
IllegalArgumentException - if the array is empty

notEmpty

public static void notEmpty(Object[] array,
                            String message)
Validate an argument, throwing IllegalArgumentException if the argument array is empty (null or no elements).
 Validate.notEmpty(myArray, "The array must not be empty");
 
Parameters:
array - the array to check is not empty
message - the exception message you would like to see if the array is empty
Throws:
IllegalArgumentException - if the array is empty

notEmpty

public static void notEmpty(String string)
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'.

Parameters:
string - the string to check is not empty
Throws:
IllegalArgumentException - if the string is empty

notEmpty

public static void notEmpty(String string,
                            String message)
Validate an argument, throwing IllegalArgumentException if the argument String is empty (null or zero length).
 Validate.notEmpty(myString, "The string must not be empty");
 
Parameters:
string - the string to check is not empty
message - the exception message you would like to see if the string is empty
Throws:
IllegalArgumentException - if the string is empty

notEmpty

public static void notEmpty(Collection<E> collection)
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'.

Parameters:
collection - the collection to check is not empty
Throws:
IllegalArgumentException - if the collection is empty

notEmpty

public static void notEmpty(Collection<E> collection,
                            String message)
Validate an argument, throwing IllegalArgumentException if the argument Collection is empty (null or no elements).
 Validate.notEmpty(myCollection, "The collection must not be empty");
 
Parameters:
collection - the collection to check is not empty
message - the exception message you would like to see if the collection is empty
Throws:
IllegalArgumentException - if the collection is empty

notEmpty

public static void notEmpty(Map<K,V> map)
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'.

Parameters:
map - the map to check is not empty
Throws:
IllegalArgumentException - if the map is empty

notEmpty

public static void notEmpty(Map<K,V> map,
                            String message)
Validate an argument, throwing IllegalArgumentException if the argument Map is empty (null or no elements).
 Validate.notEmpty(myMap, "The map must not be empty");
 
Parameters:
map - the map to check is not empty
message - the exception message you would like to see if the map is empty
Throws:
IllegalArgumentException - if the map is empty

notNull

public static void notNull(Object object)
Validate an argument, throwing IllegalArgumentException if the argument is null.
 Validate.notNull(myObject);
 

The message in the exception is 'The validated object is null'.

Parameters:
object - the object to check is not null
Throws:
IllegalArgumentException - if the object is null
Usages and Demos :

View More Examples of notNull(Object object)
   1: 
   2: import org.apache.commons.lang.Validate;
   3: 
   4:         ...
   5:     public ProgressMonitorModel(final ProgressManager _progressManager) {
   6:         Validate.notNull(_progressManager);
   7:         progressManager = _progressManager;
   8:         progressManager.addPropertyChangeListener(
   9:                 ProgressManager.PROPERTY_TASK_START_STOP, this);

View Full Code Here
   1: 
   2: import org.apache.commons.lang.Validate;
   3: 
   4:         ...
   5: 
   6:         Validate.notNull(_eventList);
   7: 
   8:         tableEditorFormat = _tableEditorFormat;
   9:         listSelectionModel = _listSelectionModel;

View Full Code Here
   1: 
   2: import org.apache.commons.lang.Validate;
   3: 
   4:         ...
   5:     public synchronized void addNewTask(final boolean batch, final Task[] tasks) {
   6:         Validate.notNull(tasks);
   7:         ...
   8:         Validate.isTrue(tasks.length > 0);

View Full Code Here
   1: 
   2: import org.apache.commons.lang.Validate;
   3: import org.apache.commons.logging.Log;
   4:         ...
   5: 
   6:         Validate.notNull(parent);
   7:         DefaultMutableTreeNode[] nodes = TreeUtils.findNode(tree, parent);
   8:         DefaultTreeModel treeModel = (DefaultTreeModel) tree.getModel();

View Full Code Here
   1: 
   2: import org.apache.commons.lang.Validate;
   3: 
   4:         ...
   5: 
   6:         Validate.notNull(_initialTask);
   7:         container = _container;
   8:         applicationContext = _appCtxt;
   9:         batch = _batch;

View Full Code Here

notNull

public static void notNull(Object object,
                           String message)
Validate an argument, throwing IllegalArgumentException if the argument is null.
 Validate.notNull(myObject, "The object must not be null");
 
Parameters:
object - the object to check is not null
message - the exception message you would like to see if the object is null
Throws:
IllegalArgumentException - if the object is null