scolebourne 2004/10/08 14:44:41 Modified: lang/src/java/org/apache/commons/lang Validate.java lang/src/test/org/apache/commons/lang ValidateTest.java Log: Rename allElementsOfClass to allElementsOfType, and change to instanceof check Revision Changes Path 1.13 +19 -17 jakarta-commons/lang/src/java/org/apache/commons/lang/Validate.java Index: Validate.java =================================================================== RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/Validate.java,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- Validate.java 1 Jun 2004 21:25:35 -0000 1.12 +++ Validate.java 8 Oct 2004 21:44:41 -0000 1.13 @@ -492,52 +492,54 @@ } } } - + /** * <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> + * are not of type <code>clazz</code> or a subclass.</p> * * <pre> - * Validate.allElementsOfClass(collection, String.class, "Collection has invalid elements"); + * Validate.allElementsOfType(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 collection the collection to check, not null + * @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> * @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(clazz); for (Iterator it = collection.iterator(); it.hasNext(); ) { - if ((it.next().getClass().equals(clazz)) == false) { + if (clazz.isInstance(it.next()) == 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> + * type <code>clazz</code> or a subclass.</p> * * <pre> - * Validate.allElementsOfClass(collection, String.class); + * Validate.allElementsOfType(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 + * @param collection the collection to check, not null + * @param clazz the <code>Class</code> which the collection's elements are expected to be, not null * @since 2.1 */ - public static void allElementsOfClass(Collection collection, Class clazz) { + 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 ((it.next().getClass().equals(clazz)) == false) { + if (clazz.isInstance(it.next()) == false) { 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); } } } 1.6 +23 -5 jakarta-commons/lang/src/test/org/apache/commons/lang/ValidateTest.java Index: ValidateTest.java =================================================================== RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/ValidateTest.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- ValidateTest.java 18 Feb 2004 23:06:19 -0000 1.5 +++ ValidateTest.java 8 Oct 2004 21:44:41 -0000 1.6 @@ -358,23 +358,41 @@ } //----------------------------------------------------------------------- - public void testAllElementsOfClass() { + public void testAllElementsOfType() { List coll = new ArrayList(); coll.add("a"); coll.add("b"); - Validate.allElementsOfClass(coll, String.class, "MSG"); + Validate.allElementsOfType(coll, String.class, "MSG"); try { - Validate.allElementsOfClass(coll, Integer.class, "MSG"); + Validate.allElementsOfType(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); + Validate.allElementsOfType(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()); } + + 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()); + } } + }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]