org.apache.commons.lang.Validate Maven / Gradle / Ivy
Show all versions of commons-lang Show documentation
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
/**
* 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");
*
*
* @author Ola Berg
* @author Stephen Colebourne
* @author Gary Gregory
* @author Norm Deane
* @since 2.0
* @version $Id: Validate.java 437554 2006-08-28 06:21:41Z bayard $
*/
public class Validate {
// Validate has no dependencies on other classes in Commons Lang at present
/**
* Constructor. This class should not normally be instantiated.
*/
public Validate() {
super();
}
// isTrue
//---------------------------------------------------------------------------------
/**
* 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.
*
* @param expression a boolean expression
* @param message the exception message you would like to see if the
* expression is false
* @param value the value to append to the message in case of error
* @throws IllegalArgumentException if expression is false
*/
public static void isTrue(boolean expression, String message, Object value) {
if (expression == false) {
throw new IllegalArgumentException(message + 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.
*
* @param expression a boolean expression
* @param message the exception message you would like to see if the expression is false
* @param value the value to append to the message in case of error
* @throws IllegalArgumentException if expression is false
*/
public static void isTrue(boolean expression, String message, long value) {
if (expression == false) {
throw new IllegalArgumentException(message + 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.
*
* @param expression a boolean expression
* @param message the exception message you would like to see if the expression
* is false
* @param value the value to append to the message in case of error
* @throws IllegalArgumentException if expression is false
*/
public static void isTrue(boolean expression, String message, double value) {
if (expression == false) {
throw new IllegalArgumentException(message + 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");
* Validate.isTrue( myObject.isOk(), "The object is not OK");
*
*
* For performance reasons, the message string should not involve a string append,
* instead use the {@link #isTrue(boolean, String, Object)} method.
*
* @param expression a boolean expression
* @param message the exception message you would like to see if the expression
* is false
* @throws IllegalArgumentException if expression is false
*/
public static void isTrue(boolean expression, String message) {
if (expression == false) {
throw new IllegalArgumentException(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 );
* Validate.isTrue( myObject.isOk() );
*
*
* The message in the exception is 'The validated expression is false'.
*
* @param expression a boolean expression
* @throws IllegalArgumentException if expression is false
*/
public static void isTrue(boolean expression) {
if (expression == false) {
throw new IllegalArgumentException("The validated expression is false");
}
}
// notNull
//---------------------------------------------------------------------------------
/**
* Validate an argument, throwing IllegalArgumentException
* if the argument is null
.
*
*
* Validate.notNull(myObject, "The object must not be null");
*
*
* @param object the object to check is not null
* @param message the exception message you would like to see
* if the object is null
* @throws IllegalArgumentException if the object is null
*/
public static void notNull(Object object, String message) {
if (object == null) {
throw new IllegalArgumentException(message);
}
}
/**
* Validate an argument, throwing IllegalArgumentException
* if the argument is null
.
*
*
* Validate.notNull(myObject);
*
*
* The message in the exception is 'The validated object is null'.
*
* @param object the object to check is not null
* @throws IllegalArgumentException if the object is null
*/
public static void notNull(Object object) {
if (object == null) {
throw new IllegalArgumentException("The validated object is null");
}
}
// notEmpty array
//---------------------------------------------------------------------------------
/**
* Validate an argument, throwing IllegalArgumentException
* if the argument array is empty (null
or no elements).
*
*
* Validate.notEmpty(myArray, "The array must not be empty");
*
*
* @param array the array to check is not empty
* @param message the exception message you would like to see if the array is empty
* @throws IllegalArgumentException if the array is empty
*/
public static void notEmpty(Object[] array, String message) {
if (array == null || array.length == 0) {
throw new IllegalArgumentException(message);
}
}
/**
* 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'.
*
* @param array the array to check is not empty
* @throws IllegalArgumentException if the array is empty
*/
public static void notEmpty(Object[] array) {
if (array == null || array.length == 0) {
throw new IllegalArgumentException("The validated array is empty");
}
}
// notEmpty collection
//---------------------------------------------------------------------------------
/**
*
Validate an argument, throwing IllegalArgumentException
* if the argument Collection is empty (null
or no elements).
*
*
* Validate.notEmpty(myCollection, "The collection must not be empty");
*
*
* @param collection the collection to check is not empty
* @param message the exception message you would like to see if the collection is empty
* @throws IllegalArgumentException if the collection is empty
*/
public static void notEmpty(Collection collection, String message) {
if (collection == null || collection.size() == 0) {
throw new IllegalArgumentException(message);
}
}
/**
* 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'.
*
* @param collection the collection to check is not empty
* @throws IllegalArgumentException if the collection is empty
*/
public static void notEmpty(Collection collection) {
if (collection == null || collection.size() == 0) {
throw new IllegalArgumentException("The validated collection is empty");
}
}
// notEmpty map
//---------------------------------------------------------------------------------
/**
* Validate an argument, throwing IllegalArgumentException
* if the argument Map is empty (null
or no elements).
*
*
* Validate.notEmpty(myMap, "The map must not be empty");
*
*
* @param map the map to check is not empty
* @param message the exception message you would like to see if the map is empty
* @throws IllegalArgumentException if the map is empty
*/
public static void notEmpty(Map map, String message) {
if (map == null || map.size() == 0) {
throw new IllegalArgumentException(message);
}
}
/**
* 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'.
*
* @param map the map to check is not empty
* @throws IllegalArgumentException if the map is empty
*/
public static void notEmpty(Map map) {
if (map == null || map.size() == 0) {
throw new IllegalArgumentException("The validated map is empty");
}
}
// notEmpty string
//---------------------------------------------------------------------------------
/**
* Validate an argument, throwing IllegalArgumentException
* if the argument String is empty (null
or zero length).
*
*
* Validate.notEmpty(myString, "The string must not be empty");
*
*
* @param string the string to check is not empty
* @param message the exception message you would like to see if the string is empty
* @throws IllegalArgumentException if the string is empty
*/
public static void notEmpty(String string, String message) {
if (string == null || string.length() == 0) {
throw new IllegalArgumentException(message);
}
}
/**
* 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'.
*
* @param string the string to check is not empty
* @throws IllegalArgumentException if the string is empty
*/
public static void notEmpty(String string) {
if (string == null || string.length() == 0) {
throw new IllegalArgumentException("The validated string is empty");
}
}
// notNullElements array
//---------------------------------------------------------------------------------
/**
* 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'.
*
* @param array the array to check
* @param message the exception message if the array has
* null
elements
* @throws IllegalArgumentException if the array has null
* elements or is null
*/
public static void noNullElements(Object[] array, String message) {
Validate.notNull(array);
for (int i = 0; i < array.length; i++) {
if (array[i] == null) {
throw new IllegalArgumentException(message);
}
}
}
/**
* 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'.
*
* @param array the array to check
* @throws IllegalArgumentException if the array has null
* elements or is null
*/
public static void noNullElements(Object[] array) {
Validate.notNull(array);
for (int i = 0; i < array.length; i++) {
if (array[i] == null) {
throw new IllegalArgumentException("The validated array contains null element at index: " + i);
}
}
}
// notNullElements collection
//---------------------------------------------------------------------------------
/**
* 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'.
*
* @param collection the collection to check
* @param message the exception message if the collection has
* null
elements
* @throws IllegalArgumentException if the collection has
* null
elements or is null
*/
public static void noNullElements(Collection collection, String message) {
Validate.notNull(collection);
for (Iterator it = collection.iterator(); it.hasNext();) {
if (it.next() == null) {
throw new IllegalArgumentException(message);
}
}
}
/**
* 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'.
*
* @param collection the collection to check
* @throws IllegalArgumentException if the collection has
* null
elements or is null
*/
public static void noNullElements(Collection collection) {
Validate.notNull(collection);
int i = 0;
for (Iterator it = collection.iterator(); it.hasNext(); i++) {
if (it.next() == null) {
throw new IllegalArgumentException("The validated collection contains null element at index: " + i);
}
}
}
/**
* 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");
*
*
* @param collection the collection to check, not null
* @param clazz the Class
which the collection's elements are expected to be, not null
* @param message the exception message if the Collection
has elements not of type clazz
* @since 2.1
*/
public static void allElementsOfType(Collection collection, Class clazz, String message) {
Validate.notNull(collection);
Validate.notNull(clazz);
for (Iterator it = collection.iterator(); it.hasNext(); ) {
if (clazz.isInstance(it.next()) == false) {
throw new IllegalArgumentException(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);
*
*
*
* The message in the exception is 'The validated collection contains an element not of type clazz at index: '.
*
*
* @param collection
* the collection to check, not null
* @param clazz
* the Class
which the collection's elements are expected to be, not null
* @since 2.1
*/
public static void allElementsOfType(Collection collection, Class clazz) {
Validate.notNull(collection);
Validate.notNull(clazz);
int i = 0;
for (Iterator it = collection.iterator(); it.hasNext(); i++) {
if (clazz.isInstance(it.next()) == false) {
throw new IllegalArgumentException("The validated collection contains an element not of type "
+ clazz.getName() + " at index: " + i);
}
}
}
}