Add generic return types

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@831704 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2009-11-01 16:54:34 +00:00
parent 661832fb35
commit dab4ca0812
2 changed files with 191 additions and 51 deletions

View File

@ -195,12 +195,14 @@ public static void isTrue(boolean expression) {
* @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 @@ public static void notNull(Object object, String message) {
* <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 @@ 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 <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 @@ public static void notEmpty(Object[] array, String message) {
* <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 @@ 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 <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 @@ public static void notEmpty(Collection<?> collection, String message) {
* <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 @@ 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 <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 @@ public static void notEmpty(Map<?,?> map, String message) {
* <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 @@ 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 <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,15 +380,37 @@ public static void notEmpty(String string, String message) {
* <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
//---------------------------------------------------------------------------------
/**
* <p>Validate an argument, throwing <code>IllegalArgumentException</code>
* if the argument String is blank (<code>null</code>, empty or whitespace).</p>
*
* <pre>
* Validate.notBlank(myString, "The string must not be blank");
* </pre>
*
* @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 <T extends CharSequence> T notBlank(T string, String message) {
if (StringUtils.isBlank(string)) {
throw new IllegalArgumentException(message);
}
return string;
}
/**
* <p>Validate an argument, throwing <code>IllegalArgumentException</code>
* if the argument String is blank (<code>null</code>, empty or whitespace).</p>
@ -386,32 +422,12 @@ public static void notEmpty(String string) {
* <p>The message in the exception is 'The validated string is blank'.</p>
*
* @param string the string to check is not 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) {
if (StringUtils.isBlank(string)) {
throw new IllegalArgumentException("The validated string is blank");
}
}
/**
* <p>Validate an argument, throwing <code>IllegalArgumentException</code>
* if the argument String is blank (<code>null</code>, empty or whitespace).</p>
*
* <pre>
* Validate.notBlank(myString, "The string must not be blank");
* </pre>
*
* @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 extends CharSequence> 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
* <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 @@ public static void noNullElements(Object[] array, String message) {
* <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 @@ public static void noNullElements(Object[] array) {
* @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 @@ public static void noNullElements(Collection<?> collection, String message) {
* <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 @@ public static void noNullElements(Collection<?> collection) {
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 @@ 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 <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 @@ public static void validIndex(Object[] array, int index, String message) {
* <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 @@ 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 <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 @@ public static void validIndex(Collection<?> coll, int index, String message) {
* <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 @@ 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 <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 @@ public static void validIndex(CharSequence str, int index, String message) {
* <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: ");
}
}

View File

@ -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<Integer> coll = new ArrayList<Integer>();
@ -191,6 +211,9 @@ public void testNotEmptyCollection1() {
}
coll.add(new Integer(8));
Validate.notEmpty(coll);
Collection<Integer> test = Validate.notEmpty(coll);
assertSame(coll, test);
}
//-----------------------------------------------------------------------
@ -210,8 +233,12 @@ public void testNotEmptyCollection2() {
}
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>();
@ -229,6 +256,9 @@ public void testNotEmptyMap1() {
}
map.put("ll", new Integer(8));
Validate.notEmpty(map);
Map<String, Integer> 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<String, Integer> 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<String> coll = new ArrayList<String>();
@ -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<String> 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<String> test = Validate.noNullElements(coll, "Message");
assertSame(coll, test);
}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
public void testAllElementsOfType() {
List<Object> coll = new ArrayList<Object>();
@ -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<String> coll = new ArrayList<String>();
@ -654,6 +741,10 @@ public void testValidIndex_withMessage_collection() {
} 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,8 +765,13 @@ public void testValidIndex_collection() {
} 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";
@ -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);
}
}