By the way, I promised some time ago to make a Validate class. Well, here is a first 
cut at it.

Improvements, disapprovements etc are welcome as usual.

/O
package org.apache.commons.lang;

/* ====================================================================
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2002 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact [EMAIL PROTECTED]
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

import java.util.Collection;
import java.util.Iterator;

/**
 This class contains static utility methods for validating arguments. If a
 argument value is deemed invalid, an IllegalArgumentException is thrown.
 
 @author <a href="mailto:[EMAIL PROTECTED]";>Ola Berg</a>
 @version $Id$
 */
public class Validate {

	/**
	 Throws the exception if the argument 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.
	 <pre>
	 Validate.isTrue( (i > 0), "The number i is not positive, it is " + i);
	 Validate.isTrue( (myObject.isOk()), "The object " + myObject.toString() + " is not OK");
	 </pre>
	 
	 @param expression
	 A boolean expression.
	 
	 @param message
	 The exception message you would like to see if the expression is false.
	 
	 @exception IllegalArgumentException
	 Thrown if expression is false, or if the message is null.
	 */
	public static void isTrue( boolean expression, String message){
		notNull( message, "The provided exception message for isTrue() is null");
		if (!expression){
			throw new IllegalArgumentException( message);
		}
	}
	
	
	/**
	 A version of the {@link #isTrue(boolean,String)} method without the 
	 possibility to supply a custom
	 exception message.
	 */
	public static void isTrue( boolean expression){
		isTrue( expression, "The expression is false");
	}
	
	
	/**
	 Throws the exception if the argument is null.
	 
	 @param object
	 An object.
	 
	 @param message
	 The exception message you would like to see if the object is null.
	 
	 @exception IllegalArgumentException
	 Thrown if the object is null, or if the message is null. 
	 */
	public static void notNull( Object object, String message){
		notNull( message, "The provided exception message for notNull() is null");
		if (object == null){
			throw new IllegalArgumentException( message);
		}
	}
	
	
	/**
	 A version of the {@link #notNull(Object,String)} method without the 
	 possibility to supply a custom
	 exception message.
	 */
	public static void notNull( Object object){
		if (object == null){
			throw new IllegalArgumentException( "The object is null");
		}
	}

	
	/**
	 Throws the exception if the supplied array is empty, after it has 
	 been validated using {@link #notNull(Object)}.

	 @param array
	 An array.
	 
	 @param message
	 The exception message you would like to see if the array is empty.
	 
	 @exception IllegalArgumentException
	 Thrown if the array is empty or null, or if the message is null.
	 */
	public static void notEmpty( Object [] array, String message){
		notNull( array, "The provided array is null");
		notNull( message, "The provided exception message for notEmpty() is null");
		if (array.length == 0){
			throw new IllegalArgumentException( message);
		}
	}
	
	
	/**
	 A version of the {@link #notEmpty(Object[],String)} method without the 
	 possibility to supply a custom
	 exception message.
	 */
	public static void notEmpty( Object [] array){
		notEmpty( array, "The provided array is empty");
	}
	
	
	/**
	 Throws the exception if the supplied Collection is empty, after it has 
	 been validated using {@link #notNull(Object)}.

	 @param collection
	 A collection object.
	 
	 @param message
	 The exception message you would like to see if the collection is empty.
	 
	 @exception IllegalArgumentException
	 Thrown if the collection is empty or null, or if the message is null.	 
	 */
	public static void notEmpty( Collection collection, String message){
		notNull( collection, "The provided collection is null");
		notNull( message, "The provided exception message for notEmpty() is null");
		if (collection.isEmpty()){
			throw new IllegalArgumentException( message);
		}
	}


	/**
	 A version of the {@link #notEmpty(Collection,String)} method without the 
	 possibility to supply a custom
	 exception message.
	 */
	public static void notEmpty( Collection collection){
		notEmpty( collection, "The provided collection is empty");
	}
	
	
	/**
	 Throws the exception if the supplied String is empty, after it has 
	 been validated using {@link #notNull(Object)}.

	 @param string
	 A string.
	 
	 @param message
	 The exception message you would like to see if the string is empty.
	 
	 @exception IllegalArgumentException
	 Thrown if the string is empty or null, or if the message is null.	 
	 */
	public static void notEmpty( String string, String message){
		notNull( string, "The provided string is null");
		notNull( message, "The provided exception message for notEmpty() is null");
		if (string.length() == 0){
			throw new IllegalArgumentException( message);
		}
	}


	/**
	 A version of the {@link #notEmpty(String,String)} method without the 
	 possibility to supply a custom
	 exception message.
	 */
	public static void notEmpty( String string){
		notEmpty( string, "The provided string is empty");
	}
	
	
	/**
	 Throws the exception if the supplied array contains null elements, after
	 it has been validated using {@link #notNull(Object)}.
	 
	 @param array
	 An array.
	 
	 @param message
	 The exception message you would like to see if the array contains null
	 elements.
	 
	 @exception IllegalArgumentException
	 Thrown if the array is null or contains null elements, 
	 or if the message is null.
	 */
	public static void noNullElements( Object [] array, String message){
		notNull( array, "The provided array is null");
		notNull( message, "The provided exception message for noNullElements() is null");
		for( int i=0; i<array.length; i++){
			if (array[i] == null){
				throw new IllegalArgumentException( message);
			}
		}
	}
	

	/**
	 A version of the {@link #noNullElements(Object[],String)} method 
	 without the possibility to supply a custom exception message.
	 */
	public static void noNullElements( Object[] array){
		noNullElements( array, "The provided array contains null elements");
	}
	
	
	/**
	 Throws the exception if the supplied collection contains null elements, after
	 it has been validated using {@link #notNull(Object)}.
	 
	 @param array
	 An array.
	 
	 @param message
	 The exception message you would like to see if the array contains null
	 elements.
	 
	 @exception IllegalArgumentException
	 Thrown if the array is null or contains null elements, 
	 or if the message is null.
	 */
	public static void noNullElements( Collection collection, String message){
		notNull( collection, "The provided collection is null");
		notNull( message, "The provided exception message for noNullElements() is null");
		Iterator i = collection.iterator();
		while( i.hasNext()){
			if (i.next() == null){
				throw new IllegalArgumentException( message);
			}
		}
	}
	

	/**
	 A version of the {@link #noNullElements(Collection,String)} method 
	 without the possibility to supply a custom exception message.
	 */
	public static void noNullElements( Collection collection){
		noNullElements( collection, "The provided collection contains null elements");
	}
	
	
}



--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to