mirror of
https://github.com/apache/commons-lang.git
synced 2025-02-08 11:05:09 +00:00
Add validate methods for all elements same type
bug 25683, from Norm Deane git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137788 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ca635e0108
commit
7ba2d8aa0d
@ -71,8 +71,9 @@
|
||||
* @author <a href="mailto:ola.berg@arkitema.se">Ola Berg</a>
|
||||
* @author Stephen Colebourne
|
||||
* @author Gary Gregory
|
||||
* @author Norm Deane
|
||||
* @since 2.0
|
||||
* @version $Id: Validate.java,v 1.8 2004/02/11 23:33:23 ggregory Exp $
|
||||
* @version $Id: Validate.java,v 1.9 2004/02/14 00:48:19 scolebourne Exp $
|
||||
*/
|
||||
public class Validate {
|
||||
|
||||
@ -528,5 +529,54 @@ public static void noNullElements(Collection collection) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Validate an argument, throwing <code>IllegalArgumentException</code>
|
||||
* if the argument collection is <code>null</code> or has elements that
|
||||
* are not of type <code>clazz</code>.</p>
|
||||
*
|
||||
* <pre>
|
||||
* Validate.allElementsOfClass(collection, String.class, "Collection has invalid elements");
|
||||
* </pre>
|
||||
*
|
||||
* @param collection the collection to check
|
||||
* @param clazz the <code>Class</code> which the collection's elements are expected to be
|
||||
* @param message the exception message if the <code>Collection</code> has elements not of type <code>clazz</code>
|
||||
* @since 2.1
|
||||
*/
|
||||
public static void allElementsOfClass(Collection collection, Class clazz, String message) {
|
||||
Validate.notNull(collection);
|
||||
for (Iterator it = collection.iterator(); it.hasNext(); ) {
|
||||
if ((it.next().getClass().equals(clazz)) == false) {
|
||||
throw new IllegalArgumentException(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Validate an argument, throwing <code>IllegalArgumentException</code>
|
||||
* if the argument collection is <code>null</code> or has elements that are not of
|
||||
* type <code>clazz</code>.</p>
|
||||
*
|
||||
* <pre>
|
||||
* Validate.allElementsOfClass(collection, String.class);
|
||||
* </pre>
|
||||
*
|
||||
* <p>The message in the exception is 'The validated collection contains an element not of type clazz at index: '.</p>
|
||||
*
|
||||
* @param collection the collection to check
|
||||
* @param clazz the <code>Class</code> which the collection's elements are expected to be
|
||||
* @since 2.1
|
||||
*/
|
||||
public static void allElementsOfClass(Collection collection, Class clazz) {
|
||||
Validate.notNull(collection);
|
||||
int i = 0;
|
||||
for (Iterator it = collection.iterator(); it.hasNext(); i++) {
|
||||
if ((it.next().getClass().equals(clazz)) == false) {
|
||||
throw new IllegalArgumentException("The validated collection contains an element not of type "
|
||||
+ (clazz == null ? "null" : clazz.getName()) + " at index: " + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -67,7 +67,8 @@
|
||||
* Unit tests {@link org.apache.commons.lang.util.Validate}.
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
* @version $Id: ValidateTest.java,v 1.3 2003/08/19 02:32:16 bayard Exp $
|
||||
* @author Norm Deane
|
||||
* @version $Id: ValidateTest.java,v 1.4 2004/02/14 00:48:20 scolebourne Exp $
|
||||
*/
|
||||
public class ValidateTest extends TestCase {
|
||||
|
||||
@ -395,4 +396,23 @@ public void testNoNullElementsCollection2() {
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testAllElementsOfClass() {
|
||||
List coll = new ArrayList();
|
||||
coll.add("a");
|
||||
coll.add("b");
|
||||
Validate.allElementsOfClass(coll, String.class, "MSG");
|
||||
try {
|
||||
Validate.allElementsOfClass(coll, Integer.class, "MSG");
|
||||
fail("Expecting IllegalArgumentException");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
assertEquals("MSG", ex.getMessage());
|
||||
}
|
||||
coll.set(1, Boolean.FALSE);
|
||||
try {
|
||||
Validate.allElementsOfClass(coll, String.class);
|
||||
fail("Expecting IllegalArgumentException");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
assertEquals("The validated collection contains an element not of type java.lang.String at index: 1", ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user