Rename allElementsOfClass to allElementsOfType, and change to instanceof check

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137964 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2004-10-08 21:44:41 +00:00
parent b2214a272b
commit 468efa24c7
2 changed files with 42 additions and 22 deletions

View File

@ -35,7 +35,7 @@
* @author Gary Gregory * @author Gary Gregory
* @author Norm Deane * @author Norm Deane
* @since 2.0 * @since 2.0
* @version $Id: Validate.java,v 1.12 2004/06/01 21:25:35 scolebourne Exp $ * @version $Id: Validate.java,v 1.13 2004/10/08 21:44:41 scolebourne Exp $
*/ */
public class Validate { public class Validate {
// Validate has no dependencies on other classes in Commons Lang at present // Validate has no dependencies on other classes in Commons Lang at present
@ -492,52 +492,54 @@ public static void noNullElements(Collection collection) {
} }
} }
} }
/** /**
* <p>Validate an argument, throwing <code>IllegalArgumentException</code> * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
* if the argument collection is <code>null</code> or has elements that * if the argument collection is <code>null</code> or has elements that
* are not of type <code>clazz</code>.</p> * are not of type <code>clazz</code> or a subclass.</p>
* *
* <pre> * <pre>
* Validate.allElementsOfClass(collection, String.class, "Collection has invalid elements"); * Validate.allElementsOfType(collection, String.class, "Collection has invalid elements");
* </pre> * </pre>
* *
* @param collection the collection to check * @param collection the collection to check, not null
* @param clazz the <code>Class</code> which the collection's elements are expected to be * @param clazz the <code>Class</code> which the collection's elements are expected to be, not null
* @param message the exception message if the <code>Collection</code> has elements not of type <code>clazz</code> * @param message the exception message if the <code>Collection</code> has elements not of type <code>clazz</code>
* @since 2.1 * @since 2.1
*/ */
public static void allElementsOfClass(Collection collection, Class clazz, String message) { public static void allElementsOfType(Collection collection, Class clazz, String message) {
Validate.notNull(collection); Validate.notNull(collection);
Validate.notNull(clazz);
for (Iterator it = collection.iterator(); it.hasNext(); ) { for (Iterator it = collection.iterator(); it.hasNext(); ) {
if ((it.next().getClass().equals(clazz)) == false) { if (clazz.isInstance(it.next()) == false) {
throw new IllegalArgumentException(message); throw new IllegalArgumentException(message);
} }
} }
} }
/** /**
* <p>Validate an argument, throwing <code>IllegalArgumentException</code> * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
* if the argument collection is <code>null</code> or has elements that are not of * if the argument collection is <code>null</code> or has elements that are not of
* type <code>clazz</code>.</p> * type <code>clazz</code> or a subclass.</p>
* *
* <pre> * <pre>
* Validate.allElementsOfClass(collection, String.class); * Validate.allElementsOfType(collection, String.class);
* </pre> * </pre>
* *
* <p>The message in the exception is 'The validated collection contains an element not of type clazz at index: '.</p> * <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 collection the collection to check, not null
* @param clazz the <code>Class</code> which the collection's elements are expected to be * @param clazz the <code>Class</code> which the collection's elements are expected to be, not null
* @since 2.1 * @since 2.1
*/ */
public static void allElementsOfClass(Collection collection, Class clazz) { public static void allElementsOfType(Collection collection, Class clazz) {
Validate.notNull(collection); Validate.notNull(collection);
Validate.notNull(clazz);
int i = 0; int i = 0;
for (Iterator it = collection.iterator(); it.hasNext(); i++) { for (Iterator it = collection.iterator(); it.hasNext(); i++) {
if ((it.next().getClass().equals(clazz)) == false) { if (clazz.isInstance(it.next()) == false) {
throw new IllegalArgumentException("The validated collection contains an element not of type " throw new IllegalArgumentException("The validated collection contains an element not of type "
+ (clazz == null ? "null" : clazz.getName()) + " at index: " + i); + clazz.getName() + " at index: " + i);
} }
} }
} }

View File

@ -30,7 +30,7 @@
* *
* @author Stephen Colebourne * @author Stephen Colebourne
* @author Norm Deane * @author Norm Deane
* @version $Id: ValidateTest.java,v 1.5 2004/02/18 23:06:19 ggregory Exp $ * @version $Id: ValidateTest.java,v 1.6 2004/10/08 21:44:41 scolebourne Exp $
*/ */
public class ValidateTest extends TestCase { public class ValidateTest extends TestCase {
@ -358,23 +358,41 @@ public void testNoNullElementsCollection2() {
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
public void testAllElementsOfClass() { public void testAllElementsOfType() {
List coll = new ArrayList(); List coll = new ArrayList();
coll.add("a"); coll.add("a");
coll.add("b"); coll.add("b");
Validate.allElementsOfClass(coll, String.class, "MSG"); Validate.allElementsOfType(coll, String.class, "MSG");
try { try {
Validate.allElementsOfClass(coll, Integer.class, "MSG"); Validate.allElementsOfType(coll, Integer.class, "MSG");
fail("Expecting IllegalArgumentException"); fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage()); assertEquals("MSG", ex.getMessage());
} }
coll.set(1, Boolean.FALSE); coll.set(1, Boolean.FALSE);
try { try {
Validate.allElementsOfClass(coll, String.class); Validate.allElementsOfType(coll, String.class);
fail("Expecting IllegalArgumentException"); fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
assertEquals("The validated collection contains an element not of type java.lang.String at index: 1", ex.getMessage()); assertEquals("The validated collection contains an element not of type java.lang.String at index: 1", ex.getMessage());
} }
coll = new ArrayList();
coll.add(new Integer(5));
coll.add(new Double(2.0d));
Validate.allElementsOfType(coll, Number.class, "MSG");
try {
Validate.allElementsOfType(coll, Integer.class, "MSG");
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage());
}
try {
Validate.allElementsOfType(coll, Double.class, "MSG");
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) {
assertEquals("MSG", ex.getMessage());
}
} }
} }