From dab4ca0812b8717366dc4466616583047e6b4cf9 Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Sun, 1 Nov 2009 16:54:34 +0000 Subject: [PATCH] Add generic return types git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@831704 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/lang/Validate.java | 138 +++++++++++------- .../org/apache/commons/lang/ValidateTest.java | 104 +++++++++++++ 2 files changed, 191 insertions(+), 51 deletions(-) diff --git a/src/java/org/apache/commons/lang/Validate.java b/src/java/org/apache/commons/lang/Validate.java index 58f6d6313..5387dd047 100644 --- a/src/java/org/apache/commons/lang/Validate.java +++ b/src/java/org/apache/commons/lang/Validate.java @@ -195,12 +195,14 @@ public static void isTrue(boolean expression) { * @param object the object to check is not null * @param message the exception message you would like to see * if the object is null + * @return the input object, never null, for chaining * @throws IllegalArgumentException if the object is null */ - public static void notNull(Object object, String message) { + public static T notNull(T object, String message) { if (object == null) { throw new IllegalArgumentException(message); } + return object; } /** @@ -214,10 +216,11 @@ public static void notNull(Object object, String message) { *

The message in the exception is 'The validated object is null'.

* * @param object the object to check is not null + * @return the input object, never null, for chaining * @throws IllegalArgumentException if the object is null */ - public static void notNull(Object object) { - notNull(object, "The validated object is null"); + public static T notNull(T object) { + return notNull(object, "The validated object is null"); } // notEmpty array @@ -233,12 +236,14 @@ public static void notNull(Object object) { * * @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 null or empty, for chaining * @throws IllegalArgumentException if the array is empty */ - public static void notEmpty(Object[] array, String message) { + public static T[] notEmpty(T[] array, String message) { if (array == null || array.length == 0) { throw new IllegalArgumentException(message); } + return array; } /** @@ -252,10 +257,11 @@ public static void notEmpty(Object[] array, String message) { *

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 null 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[] notEmpty(T[] array) { + return notEmpty(array, "The validated array is empty"); } // notEmpty collection @@ -271,12 +277,14 @@ public static void notEmpty(Object[] array) { * * @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 null or empty, for chaining * @throws IllegalArgumentException if the collection is empty */ - public static void notEmpty(Collection collection, String message) { + public static > T notEmpty(T collection, String message) { if (collection == null || collection.size() == 0) { throw new IllegalArgumentException(message); } + return collection; } /** @@ -290,10 +298,11 @@ public static void notEmpty(Collection collection, String message) { *

The message in the exception is 'The validated collection is empty'.

* * @param collection the collection to check is not empty + * @return the input collection, never null 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 notEmpty(T collection) { + return notEmpty(collection, "The validated collection is empty"); } // notEmpty map @@ -309,12 +318,14 @@ public static void notEmpty(Collection collection) { * * @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 null or empty, for chaining * @throws IllegalArgumentException if the map is empty */ - public static void notEmpty(Map map, String message) { + public static > T notEmpty(T map, String message) { if (map == null || map.size() == 0) { throw new IllegalArgumentException(message); } + return map; } /** @@ -328,10 +339,11 @@ public static void notEmpty(Map map, String message) { *

The message in the exception is 'The validated map is empty'.

* * @param map the map to check is not empty + * @return the input map, never null 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 notEmpty(T map) { + return notEmpty(map, "The validated map is empty"); } // notEmpty string @@ -347,12 +359,14 @@ public static void notEmpty(Map map) { * * @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 null or empty, for chaining * @throws IllegalArgumentException if the string is empty */ - public static void notEmpty(String string, String message) { + public static T notEmpty(T string, String message) { if (string == null || string.length() == 0) { throw new IllegalArgumentException(message); } + return string; } /** @@ -366,15 +380,37 @@ public static void notEmpty(String string, String message) { *

The message in the exception is 'The validated string is empty'.

* * @param string the string to check is not empty + * @return the input string, never null 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 notEmpty(T string) { + return notEmpty(string, "The validated string is empty"); } // notBlank string //--------------------------------------------------------------------------------- + /** + *

Validate an argument, throwing IllegalArgumentException + * if the argument String is blank (null, empty or whitespace).

+ * + *
+     * Validate.notBlank(myString, "The string must not be blank");
+     * 
+ * + * @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 null or blank, for chaining + * @throws IllegalArgumentException if the string is blank + * @see StringUtils#isBlank(CharSequence) + */ + public static T notBlank(T string, String message) { + if (StringUtils.isBlank(string)) { + throw new IllegalArgumentException(message); + } + return string; + } + /** *

Validate an argument, throwing IllegalArgumentException * if the argument String is blank (null, empty or whitespace).

@@ -386,32 +422,12 @@ public static void notEmpty(String string) { *

The message in the exception is 'The validated string is blank'.

* * @param string the string to check is not blank + * @return the input string, never null or blank, for chaining * @throws IllegalArgumentException if the string is blank * @see StringUtils#isBlank(CharSequence) */ - public static void notBlank(String string) { - if (StringUtils.isBlank(string)) { - throw new IllegalArgumentException("The validated string is blank"); - } - } - - /** - *

Validate an argument, throwing IllegalArgumentException - * if the argument String is blank (null, empty or whitespace).

- * - *
-     * Validate.notBlank(myString, "The string must not be blank");
-     * 
- * - * @param string the string to check is not blank - * @param message the exception message you would like to see if the string is blank - * @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 notBlank(T string) { + return notBlank(string, "The validated string is blank"); } // notNullElements array @@ -431,16 +447,18 @@ public static void notBlank(String string, String message) { * @param array the array to check * @param message the exception message if the array has * null elements + * @return the validated input array, never null, for chaining * @throws IllegalArgumentException if the array has null * elements or is null */ - public static void noNullElements(Object[] array, String message) { + public static 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 @@ public static void noNullElements(Object[] array, String message) { *

If the array is null then the message in the exception is 'The validated object is null'.

* * @param array the array to check + * @return the validated input array, never null, for chaining * @throws IllegalArgumentException if the array has null * elements or is null */ - public static void noNullElements(Object[] array) { + public static 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 @@ public static void noNullElements(Object[] array) { * @param collection the collection to check * @param message the exception message if the collection has * null elements + * @return the validated input collection, never null, for chaining * @throws IllegalArgumentException if the collection has * null elements or is null */ - public static void noNullElements(Collection collection, String message) { + public static > 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 @@ public static void noNullElements(Collection collection, String message) { *

If the collection is null then the message in the exception is 'The validated object is null'.

* * @param collection the collection to check + * @return the validated input collection, never null, for chaining * @throws IllegalArgumentException if the collection has * null elements or is null */ - public static void noNullElements(Collection collection) { + public static > T noNullElements(T collection) { Validate.notNull(collection); int i = 0; for (Iterator it = collection.iterator(); it.hasNext(); i++) { @@ -524,8 +547,12 @@ public static void noNullElements(Collection collection) { throw new IllegalArgumentException("The validated collection contains null element at index: " + i); } } + return collection; } + // allElementsOfType collection + //--------------------------------------------------------------------------------- + /** *

Validate an argument, throwing IllegalArgumentException * if the argument collection is null or has elements that @@ -595,13 +622,15 @@ public static void allElementsOfType(Collection collection, Class clazz) { * * @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 null, 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[] 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 @@ public static void validIndex(Object[] array, int index, String message) { *

If the array is null then the message in the exception is 'The validated object is null'.

* * @param array the array to check, not null + * @return the validated input array, never null, 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[] validIndex(T[] array, int index) { + return validIndex(array, index, "The validated array index is invalid: "); } // validIndex collection @@ -639,13 +669,15 @@ public static void validIndex(Object[] array, int index) { * * @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 null, 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 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 @@ public static void validIndex(Collection coll, int index, String message) { *

If the collection is null then the message in the exception is 'The validated object is null'.

* * @param coll the collection to check, not null + * @return the validated input collection, never null, 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 validIndex(T coll, int index) { + return validIndex(coll, index, "The validated collection index is invalid: "); } // validIndex string @@ -684,13 +717,15 @@ public static void validIndex(Collection coll, int index) { * * @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 null, 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 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 @@ public static void validIndex(CharSequence str, int index, String message) { *

If the string is null then the message in the exception is 'The validated object is null'.

* * @param str the string to check, not null + * @return the validated input string, never null, 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 validIndex(T str, int index) { + return validIndex(str, index, "The validated string index is invalid: "); } } diff --git a/src/test/org/apache/commons/lang/ValidateTest.java b/src/test/org/apache/commons/lang/ValidateTest.java index eaf05f4dd..2b17f5dee 100644 --- a/src/test/org/apache/commons/lang/ValidateTest.java +++ b/src/test/org/apache/commons/lang/ValidateTest.java @@ -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; @@ -118,6 +119,7 @@ public void testIsTrue5() { } } + //----------------------------------------------------------------------- //----------------------------------------------------------------------- public void testNotNull1() { Validate.notNull(new Object()); @@ -127,6 +129,10 @@ public void testNotNull1() { } catch (IllegalArgumentException ex) { assertEquals("The validated object is null", ex.getMessage()); } + + String str = "Hi"; + String testStr = Validate.notNull(str); + assertSame(str, testStr); } //----------------------------------------------------------------------- @@ -138,8 +144,13 @@ public void testNotNull2() { } 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}); @@ -155,6 +166,10 @@ public void testNotEmptyArray1() { } 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,8 +187,13 @@ public void testNotEmptyArray2() { } 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 coll = new ArrayList(); @@ -191,6 +211,9 @@ public void testNotEmptyCollection1() { } coll.add(new Integer(8)); Validate.notEmpty(coll); + + Collection test = Validate.notEmpty(coll); + assertSame(coll, test); } //----------------------------------------------------------------------- @@ -210,8 +233,12 @@ public void testNotEmptyCollection2() { } coll.add(new Integer(8)); Validate.notEmpty(coll, "MSG"); + + Collection test = Validate.notEmpty(coll, "Message"); + assertSame(coll, test); } + //----------------------------------------------------------------------- //----------------------------------------------------------------------- public void testNotEmptyMap1() { Map map = new HashMap(); @@ -229,6 +256,9 @@ public void testNotEmptyMap1() { } map.put("ll", new Integer(8)); Validate.notEmpty(map); + + Map test = Validate.notEmpty(map); + assertSame(map, test); } //----------------------------------------------------------------------- @@ -248,8 +278,12 @@ public void testNotEmptyMap2() { } map.put("ll", new Integer(8)); Validate.notEmpty(map, "MSG"); + + Map test = Validate.notEmpty(map, "Message"); + assertSame(map, test); } + //----------------------------------------------------------------------- //----------------------------------------------------------------------- public void testNotEmptyString1() { Validate.notEmpty("hjl"); @@ -265,6 +299,10 @@ public void testNotEmptyString1() { } catch (IllegalArgumentException ex) { assertEquals("The validated string is empty", ex.getMessage()); } + + String str = "Hi"; + String testStr = Validate.notEmpty(str); + assertSame(str, testStr); } //----------------------------------------------------------------------- @@ -282,8 +320,13 @@ public void testNotEmptyString2() { } catch (IllegalArgumentException ex) { assertEquals("MSG", ex.getMessage()); } + + String str = "Hi"; + String testStr = Validate.notEmpty(str, "Message"); + assertSame(str, testStr); } + //----------------------------------------------------------------------- //----------------------------------------------------------------------- public void testNotBlankNullStringShouldThrow() { //given @@ -470,6 +513,20 @@ public void testNotBlankMsgNotBlankStringWithNewlinesShouldNotThrow() { //then should not throw } + //----------------------------------------------------------------------- + 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"}; @@ -487,6 +544,10 @@ public void testNoNullElementsArray1() { } 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,8 +567,13 @@ public void testNoNullElementsArray2() { } 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 coll = new ArrayList(); @@ -527,6 +593,10 @@ public void testNoNullElementsCollection1() { } catch (IllegalArgumentException ex) { assertEquals("The validated collection contains null element at index: 1", ex.getMessage()); } + + coll.set(1, "b"); + List test = Validate.noNullElements(coll); + assertSame(coll, test); } //----------------------------------------------------------------------- @@ -548,8 +618,13 @@ public void testNoNullElementsCollection2() { } catch (IllegalArgumentException ex) { assertEquals("MSG", ex.getMessage()); } + + coll.set(1, "b"); + List test = Validate.noNullElements(coll, "Message"); + assertSame(coll, test); } + //----------------------------------------------------------------------- //----------------------------------------------------------------------- public void testAllElementsOfType() { List coll = new ArrayList(); @@ -589,6 +664,8 @@ public void testAllElementsOfType() { } } + //----------------------------------------------------------------------- + //----------------------------------------------------------------------- public void testConstructor() { assertNotNull(new Validate()); Constructor[] cons = Validate.class.getDeclaredConstructors(); @@ -598,6 +675,7 @@ public void testConstructor() { assertEquals(false, Modifier.isFinal(Validate.class.getModifiers())); } + //----------------------------------------------------------------------- //----------------------------------------------------------------------- public void testValidIndex_withMessage_array() { Object[] array = new Object[2]; @@ -615,6 +693,10 @@ public void testValidIndex_withMessage_array() { } 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,8 +715,13 @@ public void testValidIndex_array() { } 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 coll = new ArrayList(); @@ -654,6 +741,10 @@ public void testValidIndex_withMessage_collection() { } catch (IllegalArgumentException ex) { assertEquals("Broken: 2", ex.getMessage()); } + + List strColl = Arrays.asList(new String[] {"Hi"}); + List test = Validate.validIndex(strColl, 0, "Message"); + assertSame(strColl, test); } public void testValidIndex_collection() { @@ -674,8 +765,13 @@ public void testValidIndex_collection() { } catch (IllegalArgumentException ex) { assertEquals("The validated collection index is invalid: 2", ex.getMessage()); } + + List strColl = Arrays.asList(new String[] {"Hi"}); + List test = Validate.validIndex(strColl, 0); + assertSame(strColl, test); } + //----------------------------------------------------------------------- //----------------------------------------------------------------------- public void testValidIndex_withMessage_charSequence() { CharSequence str = "Hi"; @@ -693,6 +789,10 @@ public void testValidIndex_withMessage_charSequence() { } 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 @@ public void testValidIndex_charSequence() { } 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); } }