commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject svn commit: r831689 - in /commons/proper/lang/trunk/src: java/org/apache/commons/lang/Validate.java test/org/apache/commons/lang/ValidateTest.java
Date Sun, 01 Nov 2009 15:45:09 GMT
Author: scolebourne
Date: Sun Nov  1 15:45:09 2009
New Revision: 831689

URL: http://svn.apache.org/viewvc?rev=831689&view=rev
Log:
LANG-546 - Validate.validIndex()

Modified:
    commons/proper/lang/trunk/src/java/org/apache/commons/lang/Validate.java
    commons/proper/lang/trunk/src/test/org/apache/commons/lang/ValidateTest.java

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/Validate.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/Validate.java?rev=831689&r1=831688&r2=831689&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/Validate.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/Validate.java Sun Nov  1 15:45:09
2009
@@ -580,4 +580,138 @@
         }
     }
 
+    // validIndex array
+    //---------------------------------------------------------------------------------
+
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
if the
+     * index for the argument array is invalid or if the array is <code>null</code>.</p>
+     *
+     * <pre>
+     * Validate.validIndex(myArray, 2, "The array index is invalid: ");
+     * </pre>
+     * 
+     * <p>If the array is null then the message in the exception is 'The validated
object is null'.</p>
+     *
+     * @param array  the array to check, not null
+     * @param message  the exception message if the array index is invalid
+     * @throws IllegalArgumentException if the array index is invalid or null
+     */
+    public static void validIndex(Object[] array, int index, String message) {
+        Validate.notNull(array);
+        if (index < 0 || index >= array.length) {
+            throw new IllegalArgumentException(message + index);
+        }
+    }
+
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
if the
+     * index for the argument array is invalid or if the array is <code>null</code>.</p>
+     *
+     * <pre>
+     * Validate.validIndex(myArray, 2);
+     * </pre>
+     *
+     * <p>If the array index is invalid the message in the exception is
+     * 'The validated array index is invalid: ' followed by the index.</p>
+     *
+     * <p>If the array is null then the message in the exception is 'The validated
object is null'.</p>
+     * 
+     * @param array  the array to check, not null
+     * @throws IllegalArgumentException if the array index is invalid or null
+     */
+    public static void validIndex(Object[] array, int index) {
+        validIndex(array, index, "The validated array index is invalid: ");
+    }
+
+    // validIndex collection
+    //---------------------------------------------------------------------------------
+
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
if the
+     * index for the argument collection is invalid or if the collection is <code>null</code>.</p>
+     *
+     * <pre>
+     * Validate.validIndex(myCollection, 2, "The collection index is invalid: ");
+     * </pre>
+     * 
+     * <p>If the collection is null then the message in the exception is 'The validated
object is null'.</p>
+     *
+     * @param coll  the collection to check, not null
+     * @param message  the exception message if the collection index is invalid
+     * @throws IllegalArgumentException if the collection index is invalid or null
+     */
+    public static void validIndex(Collection<?> coll, int index, String message) {
+        Validate.notNull(coll);
+        if (index < 0 || index >= coll.size()) {
+            throw new IllegalArgumentException(message + index);
+        }
+    }
+
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
if the
+     * index for the argument collection is invalid or if the collection is <code>null</code>.</p>
+     *
+     * <pre>
+     * Validate.validIndex(myCollection, 2);
+     * </pre>
+     *
+     * <p>If the collection index is invalid the message in the exception is
+     * 'The validated collection index is invalid: ' followed by the index.</p>
+     *
+     * <p>If the collection is null then the message in the exception is 'The validated
object is null'.</p>
+     * 
+     * @param coll  the collection to check, not null
+     * @throws IllegalArgumentException if the collection index is invalid or null
+     */
+    public static void validIndex(Collection<?> coll, int index) {
+        validIndex(coll, index, "The validated collection index is invalid: ");
+    }
+
+    // validIndex string
+    //---------------------------------------------------------------------------------
+
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
if the
+     * index for the argument character sequence (including String and StringBuffer)
+     * is invalid or if the string is <code>null</code>.</p>
+     *
+     * <pre>
+     * Validate.validIndex(myStr, 2, "The string index is invalid: ");
+     * </pre>
+     * 
+     * <p>If the string is null then the message in the exception is 'The validated
object is null'.</p>
+     *
+     * @param str  the string to check, not null
+     * @param message  the exception message if the string index is invalid
+     * @throws IllegalArgumentException if the string index is invalid or null
+     */
+    public static void validIndex(CharSequence str, int index, String message) {
+        Validate.notNull(str);
+        if (index < 0 || index >= str.length()) {
+            throw new IllegalArgumentException(message + index);
+        }
+    }
+
+    /**
+     * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
if the
+     * index for the argument character sequence (including String and StringBuffer)
+     * is invalid or if the string is <code>null</code>.</p>
+     *
+     * <pre>
+     * Validate.validIndex(myStr, 2);
+     * </pre>
+     *
+     * <p>If the string index is invalid the message in the exception is
+     * 'The validated string index is invalid: ' followed by the index.</p>
+     *
+     * <p>If the string is null then the message in the exception is 'The validated
object is null'.</p>
+     * 
+     * @param str  the string to check, not null
+     * @throws IllegalArgumentException if the string index is invalid or null
+     */
+    public static void validIndex(CharSequence str, int index) {
+        validIndex(str, index, "The validated string index is invalid: ");
+    }
+
 }

Modified: commons/proper/lang/trunk/src/test/org/apache/commons/lang/ValidateTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/ValidateTest.java?rev=831689&r1=831688&r2=831689&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang/ValidateTest.java (original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang/ValidateTest.java Sun Nov 
1 15:45:09 2009
@@ -597,5 +597,120 @@
         assertEquals(true, Modifier.isPublic(Validate.class.getModifiers()));
         assertEquals(false, Modifier.isFinal(Validate.class.getModifiers()));
     }
-    
+
+    //-----------------------------------------------------------------------
+    public void testValidIndex_withMessage_array() {
+        Object[] array = new Object[2];
+        Validate.validIndex(array, 0, "Broken: ");
+        Validate.validIndex(array, 1, "Broken: ");
+        try {
+            Validate.validIndex(array, -1, "Broken: ");
+            fail("Expecting IllegalArgumentException");
+        } catch (IllegalArgumentException ex) {
+            assertEquals("Broken: -1", ex.getMessage());
+        }
+        try {
+            Validate.validIndex(array, 2, "Broken: ");
+            fail("Expecting IllegalArgumentException");
+        } catch (IllegalArgumentException ex) {
+            assertEquals("Broken: 2", ex.getMessage());
+        }
+    }
+
+    public void testValidIndex_array() {
+        Object[] array = new Object[2];
+        Validate.validIndex(array, 0);
+        Validate.validIndex(array, 1);
+        try {
+            Validate.validIndex(array, -1);
+            fail("Expecting IllegalArgumentException");
+        } catch (IllegalArgumentException ex) {
+            assertEquals("The validated array index is invalid: -1", ex.getMessage());
+        }
+        try {
+            Validate.validIndex(array, 2);
+            fail("Expecting IllegalArgumentException");
+        } catch (IllegalArgumentException ex) {
+            assertEquals("The validated array index is invalid: 2", ex.getMessage());
+        }
+    }
+
+    //-----------------------------------------------------------------------
+    public void testValidIndex_withMessage_collection() {
+        Collection<String> coll = new ArrayList<String>();
+        coll.add(null);
+        coll.add(null);
+        Validate.validIndex(coll, 0, "Broken: ");
+        Validate.validIndex(coll, 1, "Broken: ");
+        try {
+            Validate.validIndex(coll, -1, "Broken: ");
+            fail("Expecting IllegalArgumentException");
+        } catch (IllegalArgumentException ex) {
+            assertEquals("Broken: -1", ex.getMessage());
+        }
+        try {
+            Validate.validIndex(coll, 2, "Broken: ");
+            fail("Expecting IllegalArgumentException");
+        } catch (IllegalArgumentException ex) {
+            assertEquals("Broken: 2", ex.getMessage());
+        }
+    }
+
+    public void testValidIndex_collection() {
+        Collection<String> coll = new ArrayList<String>();
+        coll.add(null);
+        coll.add(null);
+        Validate.validIndex(coll, 0);
+        Validate.validIndex(coll, 1);
+        try {
+            Validate.validIndex(coll, -1);
+            fail("Expecting IllegalArgumentException");
+        } catch (IllegalArgumentException ex) {
+            assertEquals("The validated collection index is invalid: -1", ex.getMessage());
+        }
+        try {
+            Validate.validIndex(coll, 2);
+            fail("Expecting IllegalArgumentException");
+        } catch (IllegalArgumentException ex) {
+            assertEquals("The validated collection index is invalid: 2", ex.getMessage());
+        }
+    }
+
+    //-----------------------------------------------------------------------
+    public void testValidIndex_withMessage_charSequence() {
+        CharSequence str = "Hi";
+        Validate.validIndex(str, 0, "Broken: ");
+        Validate.validIndex(str, 1, "Broken: ");
+        try {
+            Validate.validIndex(str, -1, "Broken: ");
+            fail("Expecting IllegalArgumentException");
+        } catch (IllegalArgumentException ex) {
+            assertEquals("Broken: -1", ex.getMessage());
+        }
+        try {
+            Validate.validIndex(str, 2, "Broken: ");
+            fail("Expecting IllegalArgumentException");
+        } catch (IllegalArgumentException ex) {
+            assertEquals("Broken: 2", ex.getMessage());
+        }
+    }
+
+    public void testValidIndex_charSequence() {
+        CharSequence str = "Hi";
+        Validate.validIndex(str, 0);
+        Validate.validIndex(str, 1);
+        try {
+            Validate.validIndex(str, -1);
+            fail("Expecting IllegalArgumentException");
+        } catch (IllegalArgumentException ex) {
+            assertEquals("The validated string index is invalid: -1", ex.getMessage());
+        }
+        try {
+            Validate.validIndex(str, 2);
+            fail("Expecting IllegalArgumentException");
+        } catch (IllegalArgumentException ex) {
+            assertEquals("The validated string index is invalid: 2", ex.getMessage());
+        }
+    }
+
 }



Mime
View raw message