commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject svn commit: r831704 - 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 16:54:34 GMT
Author: scolebourne
Date: Sun Nov  1 16:54:34 2009
New Revision: 831704

URL: http://svn.apache.org/viewvc?rev=831704&view=rev
Log:
Add generic return types

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=831704&r1=831703&r2=831704&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 16:54:34
2009
@@ -195,12 +195,14 @@
      * @param object  the object to check is not <code>null</code>
      * @param message  the exception message you would like to see
      *  if the object is <code>null</code>
+     * @return the input object, never <code>null</code>, for chaining
      * @throws IllegalArgumentException if the object is <code>null</code>
      */
-    public static void notNull(Object object, String message) {
+    public static <T> T notNull(T object, String message) {
         if (object == null) {
             throw new IllegalArgumentException(message);
         }
+        return object;
     }
 
     /**
@@ -214,10 +216,11 @@
      * <p>The message in the exception is 'The validated object is null'.</p>
      * 
      * @param object  the object to check is not <code>null</code>
+     * @return the input object, never <code>null</code>, for chaining
      * @throws IllegalArgumentException if the object is <code>null</code>
      */
-    public static void notNull(Object object) {
-        notNull(object, "The validated object is null");
+    public static <T> T notNull(T object) {
+        return notNull(object, "The validated object is null");
     }
 
     // notEmpty array
@@ -233,12 +236,14 @@
      * 
      * @param array  the array to check is not empty
      * @param message  the exception message you would like to see if the array is empty
+     * @return the input array, never <code>null</code> or empty, for chaining
      * @throws IllegalArgumentException if the array is empty
      */
-    public static void notEmpty(Object[] array, String message) {
+    public static <T> T[] notEmpty(T[] array, String message) {
         if (array == null || array.length == 0) {
             throw new IllegalArgumentException(message);
         }
+        return array;
     }
 
     /**
@@ -252,10 +257,11 @@
      * <p>The message in the exception is 'The validated array is empty'.
      * 
      * @param array  the array to check is not empty
+     * @return the input array, never <code>null</code> or empty, for chaining
      * @throws IllegalArgumentException if the array is empty
      */
-    public static void notEmpty(Object[] array) {
-        notEmpty(array, "The validated array is empty");
+    public static <T> T[] notEmpty(T[] array) {
+        return notEmpty(array, "The validated array is empty");
     }
 
     // notEmpty collection
@@ -271,12 +277,14 @@
      * 
      * @param collection  the collection to check is not empty
      * @param message  the exception message you would like to see if the collection is empty
+     * @return the input collection, never <code>null</code> or empty, for chaining
      * @throws IllegalArgumentException if the collection is empty
      */
-    public static void notEmpty(Collection<?> collection, String message) {
+    public static <T extends Collection<?>> T notEmpty(T collection, String message)
{
         if (collection == null || collection.size() == 0) {
             throw new IllegalArgumentException(message);
         }
+        return collection;
     }
 
     /**
@@ -290,10 +298,11 @@
      * <p>The message in the exception is 'The validated collection is empty'.</p>
      * 
      * @param collection  the collection to check is not empty
+     * @return the input collection, never <code>null</code> or empty, for chaining
      * @throws IllegalArgumentException if the collection is empty
      */
-    public static void notEmpty(Collection<?> collection) {
-        notEmpty(collection, "The validated collection is empty");
+    public static <T extends Collection<?>> T notEmpty(T collection) {
+        return notEmpty(collection, "The validated collection is empty");
     }
 
     // notEmpty map
@@ -309,12 +318,14 @@
      * 
      * @param map  the map to check is not empty
      * @param message  the exception message you would like to see if the map is empty
+     * @return the input map, never <code>null</code> or empty, for chaining
      * @throws IllegalArgumentException if the map is empty
      */
-    public static void notEmpty(Map<?,?> map, String message) {
+    public static <T extends Map<?, ?>> T notEmpty(T map, String message) {
         if (map == null || map.size() == 0) {
             throw new IllegalArgumentException(message);
         }
+        return map;
     }
 
     /**
@@ -328,10 +339,11 @@
      * <p>The message in the exception is 'The validated map is empty'.</p>
      * 
      * @param map  the map to check is not empty
+     * @return the input map, never <code>null</code> or empty, for chaining
      * @throws IllegalArgumentException if the map is empty
      */
-    public static void notEmpty(Map<?,?> map) {
-        notEmpty(map, "The validated map is empty");
+    public static <T extends Map<?, ?>> T notEmpty(T map) {
+        return notEmpty(map, "The validated map is empty");
     }
 
     // notEmpty string
@@ -347,12 +359,14 @@
      * 
      * @param string  the string to check is not empty
      * @param message  the exception message you would like to see if the string is empty
+     * @return the input string, never <code>null</code> or empty, for chaining
      * @throws IllegalArgumentException if the string is empty
      */
-    public static void notEmpty(String string, String message) {
+    public static <T extends CharSequence> T notEmpty(T string, String message) {
         if (string == null || string.length() == 0) {
             throw new IllegalArgumentException(message);
         }
+        return string;
     }
 
     /**
@@ -366,10 +380,11 @@
      * <p>The message in the exception is 'The validated string is empty'.</p>
      * 
      * @param string  the string to check is not empty
+     * @return the input string, never <code>null</code> or empty, for chaining
      * @throws IllegalArgumentException if the string is empty
      */
-    public static void notEmpty(String string) {
-        notEmpty(string, "The validated string is empty");
+    public static <T extends CharSequence> T notEmpty(T string) {
+        return notEmpty(string, "The validated string is empty");
     }
 
     // notBlank string
@@ -380,19 +395,20 @@
      * if the argument String is blank (<code>null</code>, empty or whitespace).</p>
      *
      * <pre>
-     * Validate.notBlank(myString);
+     * Validate.notBlank(myString, "The string must not be blank");
      * </pre>
      *
-     * <p>The message in the exception is 'The validated string is blank'.</p>
-     *
      * @param string  the string to check is not blank
+     * @param message  the exception message you would like to see if the string is blank
+     * @return the input string, never <code>null</code> or blank, for chaining
      * @throws IllegalArgumentException if the string is blank
      * @see StringUtils#isBlank(CharSequence)
      */
-    public static void notBlank(String string) {
+    public static <T extends CharSequence> T notBlank(T string, String message) {
         if (StringUtils.isBlank(string)) {
-            throw new IllegalArgumentException("The validated string is blank");
+            throw new IllegalArgumentException(message);
         }
+        return string;
     }
 
     /**
@@ -400,18 +416,18 @@
      * if the argument String is blank (<code>null</code>, empty or whitespace).</p>
      *
      * <pre>
-     * Validate.notBlank(myString, "The string must not be blank");
+     * Validate.notBlank(myString);
      * </pre>
      *
+     * <p>The message in the exception is 'The validated string is blank'.</p>
+     *
      * @param string  the string to check is not blank
-     * @param message  the exception message you would like to see if the string is blank
+     * @return the input string, never <code>null</code> or blank, for chaining
      * @throws IllegalArgumentException if the string is blank
      * @see StringUtils#isBlank(CharSequence)
      */
-    public static void notBlank(String string, String message) {
-        if (StringUtils.isBlank(string)) {
-            throw new IllegalArgumentException(message);
-        }
+    public static <T extends CharSequence> T notBlank(T string) {
+        return notBlank(string, "The validated string is blank");
     }
 
     // notNullElements array
@@ -431,16 +447,18 @@
      * @param array  the array to check
      * @param message  the exception message if the array has
      *  <code>null</code> elements
+     * @return the validated input array, never <code>null</code>, for chaining
      * @throws IllegalArgumentException if the array has <code>null</code>
      *  elements or is <code>null</code>
      */
-    public static void noNullElements(Object[] array, String message) {
+    public static <T> T[] noNullElements(T[] array, String message) {
         Validate.notNull(array);
         for (int i = 0; i < array.length; i++) {
             if (array[i] == null) {
                 throw new IllegalArgumentException(message);
             }
         }
+        return array;
     }
 
     /**
@@ -458,16 +476,18 @@
      * <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
+     * @return the validated input array, never <code>null</code>, for chaining
      * @throws IllegalArgumentException if the array has <code>null</code>
      *  elements or is <code>null</code>
      */
-    public static void noNullElements(Object[] array) {
+    public static <T> T[] noNullElements(T[] array) {
         Validate.notNull(array);
         for (int i = 0; i < array.length; i++) {
             if (array[i] == null) {
                 throw new IllegalArgumentException("The validated array contains null element
at index: " + i);
             }
         }
+        return array;
     }
 
     // notNullElements collection
@@ -487,16 +507,18 @@
      * @param collection  the collection to check
      * @param message  the exception message if the collection has
      *  <code>null</code> elements
+     * @return the validated input collection, never <code>null</code>, for chaining
      * @throws IllegalArgumentException if the collection has
      *  <code>null</code> elements or is <code>null</code>
      */
-    public static void noNullElements(Collection<?> collection, String message) {
+    public static <T extends Collection<?>> T noNullElements(T collection, String
message) {
         Validate.notNull(collection);
         for (Iterator<?> it = collection.iterator(); it.hasNext();) {
             if (it.next() == null) {
                 throw new IllegalArgumentException(message);
             }
         }
+        return collection;
     }
 
     /**
@@ -513,10 +535,11 @@
      * <p>If the collection is null then the message in the exception is 'The validated
object is null'.</p>
      * 
      * @param collection  the collection to check
+     * @return the validated input collection, never <code>null</code>, for chaining
      * @throws IllegalArgumentException if the collection has
      *  <code>null</code> elements or is <code>null</code>
      */
-    public static void noNullElements(Collection<?> collection) {
+    public static <T extends Collection<?>> T noNullElements(T collection) {
         Validate.notNull(collection);
         int i = 0;
         for (Iterator<?> it = collection.iterator(); it.hasNext(); i++) {
@@ -524,8 +547,12 @@
                 throw new IllegalArgumentException("The validated collection contains null
element at index: " + i);
             }
         }
+        return collection;
     }
 
+    // allElementsOfType collection
+    //---------------------------------------------------------------------------------
+
     /**
      * <p>Validate an argument, throwing <code>IllegalArgumentException</code>
      * if the argument collection  is <code>null</code> or has elements that
@@ -595,13 +622,15 @@
      *
      * @param array  the array to check, not null
      * @param message  the exception message if the array index is invalid
+     * @return the validated input array, never <code>null</code>, for chaining
      * @throws IllegalArgumentException if the array index is invalid or null
      */
-    public static void validIndex(Object[] array, int index, String message) {
+    public static <T> T[] validIndex(T[] array, int index, String message) {
         Validate.notNull(array);
         if (index < 0 || index >= array.length) {
             throw new IllegalArgumentException(message + index);
         }
+        return array;
     }
 
     /**
@@ -618,10 +647,11 @@
      * <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
+     * @return the validated input array, never <code>null</code>, for chaining
      * @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: ");
+    public static <T> T[] validIndex(T[] array, int index) {
+        return validIndex(array, index, "The validated array index is invalid: ");
     }
 
     // validIndex collection
@@ -639,13 +669,15 @@
      *
      * @param coll  the collection to check, not null
      * @param message  the exception message if the collection index is invalid
+     * @return the validated input collection, never <code>null</code>, for chaining
      * @throws IllegalArgumentException if the collection index is invalid or null
      */
-    public static void validIndex(Collection<?> coll, int index, String message) {
+    public static <T extends Collection<?>> T validIndex(T coll, int index, String
message) {
         Validate.notNull(coll);
         if (index < 0 || index >= coll.size()) {
             throw new IllegalArgumentException(message + index);
         }
+        return coll;
     }
 
     /**
@@ -662,10 +694,11 @@
      * <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
+     * @return the validated input collection, never <code>null</code>, for chaining
      * @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: ");
+    public static <T extends Collection<?>> T validIndex(T coll, int index) {
+        return validIndex(coll, index, "The validated collection index is invalid: ");
     }
 
     // validIndex string
@@ -684,13 +717,15 @@
      *
      * @param str  the string to check, not null
      * @param message  the exception message if the string index is invalid
+     * @return the validated input string, never <code>null</code>, for chaining
      * @throws IllegalArgumentException if the string index is invalid or null
      */
-    public static void validIndex(CharSequence str, int index, String message) {
+    public static <T extends CharSequence> T validIndex(T str, int index, String message)
{
         Validate.notNull(str);
         if (index < 0 || index >= str.length()) {
             throw new IllegalArgumentException(message + index);
         }
+        return str;
     }
 
     /**
@@ -708,10 +743,11 @@
      * <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
+     * @return the validated input string, never <code>null</code>, for chaining
      * @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: ");
+    public static <T extends CharSequence> T validIndex(T str, int index) {
+        return 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=831704&r1=831703&r2=831704&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 16:54:34 2009
@@ -21,6 +21,7 @@
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Modifier;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
@@ -119,6 +120,7 @@
     }
 
     //-----------------------------------------------------------------------
+    //-----------------------------------------------------------------------
     public void testNotNull1() {
         Validate.notNull(new Object());
         try {
@@ -127,6 +129,10 @@
         } catch (IllegalArgumentException ex) {
             assertEquals("The validated object is null", ex.getMessage());
         }
+        
+        String str = "Hi";
+        String testStr = Validate.notNull(str);
+        assertSame(str, testStr);
     }
 
     //-----------------------------------------------------------------------
@@ -138,9 +144,14 @@
         } catch (IllegalArgumentException ex) {
             assertEquals("MSG", ex.getMessage());
         }
+        
+        String str = "Hi";
+        String testStr = Validate.notNull(str, "Message");
+        assertSame(str, testStr);
     }
 
     //-----------------------------------------------------------------------
+    //-----------------------------------------------------------------------
     public void testNotEmptyArray1() {
         Validate.notEmpty(new Object[] {null});
         try {
@@ -155,6 +166,10 @@
         } catch (IllegalArgumentException ex) {
             assertEquals("The validated array is empty", ex.getMessage());
         }
+        
+        String[] array = new String[] {"hi"};
+        String[] test = Validate.notEmpty(array);
+        assertSame(array, test);
     }
 
     //-----------------------------------------------------------------------
@@ -172,9 +187,14 @@
         } catch (IllegalArgumentException ex) {
             assertEquals("MSG", ex.getMessage());
         }
+        
+        String[] array = new String[] {"hi"};
+        String[] test = Validate.notEmpty(array, "Message");
+        assertSame(array, test);
     }
 
     //-----------------------------------------------------------------------
+    //-----------------------------------------------------------------------
     public void testNotEmptyCollection1() {
         Collection<Integer> coll = new ArrayList<Integer>();
         try {
@@ -191,6 +211,9 @@
         }
         coll.add(new Integer(8));
         Validate.notEmpty(coll);
+        
+        Collection<Integer> test = Validate.notEmpty(coll);
+        assertSame(coll, test);
     }
 
     //-----------------------------------------------------------------------
@@ -210,9 +233,13 @@
         }
         coll.add(new Integer(8));
         Validate.notEmpty(coll, "MSG");
+        
+        Collection<Integer> test = Validate.notEmpty(coll, "Message");
+        assertSame(coll, test);
     }
 
     //-----------------------------------------------------------------------
+    //-----------------------------------------------------------------------
     public void testNotEmptyMap1() {
         Map<String, Integer> map = new HashMap<String, Integer>();
         try {
@@ -229,6 +256,9 @@
         }
         map.put("ll", new Integer(8));
         Validate.notEmpty(map);
+        
+        Map<String, Integer> test = Validate.notEmpty(map);
+        assertSame(map, test);
     }
 
     //-----------------------------------------------------------------------
@@ -248,9 +278,13 @@
         }
         map.put("ll", new Integer(8));
         Validate.notEmpty(map, "MSG");
+        
+        Map<String, Integer> test = Validate.notEmpty(map, "Message");
+        assertSame(map, test);
     }
 
     //-----------------------------------------------------------------------
+    //-----------------------------------------------------------------------
     public void testNotEmptyString1() {
         Validate.notEmpty("hjl");
         try {
@@ -265,6 +299,10 @@
         } catch (IllegalArgumentException ex) {
             assertEquals("The validated string is empty", ex.getMessage());
         }
+        
+        String str = "Hi";
+        String testStr = Validate.notEmpty(str);
+        assertSame(str, testStr);
     }
 
     //-----------------------------------------------------------------------
@@ -282,9 +320,14 @@
         } catch (IllegalArgumentException ex) {
             assertEquals("MSG", ex.getMessage());
         }
+        
+        String str = "Hi";
+        String testStr = Validate.notEmpty(str, "Message");
+        assertSame(str, testStr);
     }
 
     //-----------------------------------------------------------------------
+    //-----------------------------------------------------------------------
     public void testNotBlankNullStringShouldThrow() {
         //given
         String string = null;
@@ -471,6 +514,20 @@
     }
 
     //-----------------------------------------------------------------------
+    public void testNotBlankReturnValues1() {
+        String str = "Hi";
+        String test = Validate.notBlank(str);
+        assertSame(str, test);
+    }
+
+    public void testNotBlankReturnValues2() {
+        String str = "Hi";
+        String test = Validate.notBlank(str, "Message");
+        assertSame(str, test);
+    }
+
+    //-----------------------------------------------------------------------
+    //-----------------------------------------------------------------------
     public void testNoNullElementsArray1() {
         String[] array = new String[] {"a", "b"};
         Validate.noNullElements(array);
@@ -487,6 +544,10 @@
         } catch (IllegalArgumentException ex) {
             assertEquals("The validated array contains null element at index: 1", ex.getMessage());
         }
+        
+        array = new String[] {"a", "b"};
+        String[] test = Validate.noNullElements(array);
+        assertSame(array, test);
     }
 
     //-----------------------------------------------------------------------
@@ -506,9 +567,14 @@
         } catch (IllegalArgumentException ex) {
             assertEquals("MSG", ex.getMessage());
         }
+        
+        array = new String[] {"a", "b"};
+        String[] test = Validate.noNullElements(array, "Message");
+        assertSame(array, test);
     }
 
     //-----------------------------------------------------------------------
+    //-----------------------------------------------------------------------
     public void testNoNullElementsCollection1() {
         List<String> coll = new ArrayList<String>();
         coll.add("a");
@@ -527,6 +593,10 @@
         } catch (IllegalArgumentException ex) {
             assertEquals("The validated collection contains null element at index: 1", ex.getMessage());
         }
+        
+        coll.set(1, "b");
+        List<String> test = Validate.noNullElements(coll);
+        assertSame(coll, test);
     }
 
     //-----------------------------------------------------------------------
@@ -548,9 +618,14 @@
         } catch (IllegalArgumentException ex) {
             assertEquals("MSG", ex.getMessage());
         }
+        
+        coll.set(1, "b");
+        List<String> test = Validate.noNullElements(coll, "Message");
+        assertSame(coll, test);
     }
 
     //-----------------------------------------------------------------------
+    //-----------------------------------------------------------------------
     public void testAllElementsOfType() {
         List<Object> coll = new ArrayList<Object>();
         coll.add("a");
@@ -589,6 +664,8 @@
         }
     }
 
+    //-----------------------------------------------------------------------
+    //-----------------------------------------------------------------------
     public void testConstructor() {
         assertNotNull(new Validate());
         Constructor<?>[] cons = Validate.class.getDeclaredConstructors();
@@ -599,6 +676,7 @@
     }
 
     //-----------------------------------------------------------------------
+    //-----------------------------------------------------------------------
     public void testValidIndex_withMessage_array() {
         Object[] array = new Object[2];
         Validate.validIndex(array, 0, "Broken: ");
@@ -615,6 +693,10 @@
         } catch (IllegalArgumentException ex) {
             assertEquals("Broken: 2", ex.getMessage());
         }
+        
+        String[] strArray = new String[] {"Hi"};
+        String[] test = Validate.noNullElements(strArray, "Message");
+        assertSame(strArray, test);
     }
 
     public void testValidIndex_array() {
@@ -633,9 +715,14 @@
         } catch (IllegalArgumentException ex) {
             assertEquals("The validated array index is invalid: 2", ex.getMessage());
         }
+        
+        String[] strArray = new String[] {"Hi"};
+        String[] test = Validate.noNullElements(strArray);
+        assertSame(strArray, test);
     }
 
     //-----------------------------------------------------------------------
+    //-----------------------------------------------------------------------
     public void testValidIndex_withMessage_collection() {
         Collection<String> coll = new ArrayList<String>();
         coll.add(null);
@@ -654,6 +741,10 @@
         } catch (IllegalArgumentException ex) {
             assertEquals("Broken: 2", ex.getMessage());
         }
+        
+        List<String> strColl = Arrays.asList(new String[] {"Hi"});
+        List<String> test = Validate.validIndex(strColl, 0, "Message");
+        assertSame(strColl, test);
     }
 
     public void testValidIndex_collection() {
@@ -674,9 +765,14 @@
         } catch (IllegalArgumentException ex) {
             assertEquals("The validated collection index is invalid: 2", ex.getMessage());
         }
+        
+        List<String> strColl = Arrays.asList(new String[] {"Hi"});
+        List<String> test = Validate.validIndex(strColl, 0);
+        assertSame(strColl, test);
     }
 
     //-----------------------------------------------------------------------
+    //-----------------------------------------------------------------------
     public void testValidIndex_withMessage_charSequence() {
         CharSequence str = "Hi";
         Validate.validIndex(str, 0, "Broken: ");
@@ -693,6 +789,10 @@
         } catch (IllegalArgumentException ex) {
             assertEquals("Broken: 2", ex.getMessage());
         }
+        
+        String input = "Hi";
+        String test = Validate.validIndex(input, 0, "Message");
+        assertSame(input, test);
     }
 
     public void testValidIndex_charSequence() {
@@ -711,6 +811,10 @@
         } catch (IllegalArgumentException ex) {
             assertEquals("The validated string index is invalid: 2", ex.getMessage());
         }
+        
+        String input = "Hi";
+        String test = Validate.validIndex(input, 0);
+        assertSame(input, test);
     }
 
 }



Mime
View raw message