From 661832fb35df819f6642141bbeb043fe7b2374c0 Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Sun, 1 Nov 2009 15:45:09 +0000 Subject: [PATCH] LANG-546 - Validate.validIndex() git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@831689 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/lang/Validate.java | 134 ++++++++++++++++++ .../org/apache/commons/lang/ValidateTest.java | 117 ++++++++++++++- 2 files changed, 250 insertions(+), 1 deletion(-) diff --git a/src/java/org/apache/commons/lang/Validate.java b/src/java/org/apache/commons/lang/Validate.java index 675c12796..58f6d6313 100644 --- a/src/java/org/apache/commons/lang/Validate.java +++ b/src/java/org/apache/commons/lang/Validate.java @@ -580,4 +580,138 @@ public class Validate { } } + // validIndex array + //--------------------------------------------------------------------------------- + + /** + *

Validate an argument, throwing IllegalArgumentException if the + * index for the argument array is invalid or if the array is null.

+ * + *
+     * Validate.validIndex(myArray, 2, "The array index is invalid: ");
+     * 
+ * + *

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 + * @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); + } + } + + /** + *

Validate an argument, throwing IllegalArgumentException if the + * index for the argument array is invalid or if the array is null.

+ * + *
+     * Validate.validIndex(myArray, 2);
+     * 
+ * + *

If the array index is invalid the message in the exception is + * 'The validated array index is invalid: ' followed by the index.

+ * + *

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 + * @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 + //--------------------------------------------------------------------------------- + + /** + *

Validate an argument, throwing IllegalArgumentException if the + * index for the argument collection is invalid or if the collection is null.

+ * + *
+     * Validate.validIndex(myCollection, 2, "The collection index is invalid: ");
+     * 
+ * + *

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 + * @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); + } + } + + /** + *

Validate an argument, throwing IllegalArgumentException if the + * index for the argument collection is invalid or if the collection is null.

+ * + *
+     * Validate.validIndex(myCollection, 2);
+     * 
+ * + *

If the collection index is invalid the message in the exception is + * 'The validated collection index is invalid: ' followed by the index.

+ * + *

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 + * @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 + //--------------------------------------------------------------------------------- + + /** + *

Validate an argument, throwing IllegalArgumentException if the + * index for the argument character sequence (including String and StringBuffer) + * is invalid or if the string is null.

+ * + *
+     * Validate.validIndex(myStr, 2, "The string index is invalid: ");
+     * 
+ * + *

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 + * @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); + } + } + + /** + *

Validate an argument, throwing IllegalArgumentException if the + * index for the argument character sequence (including String and StringBuffer) + * is invalid or if the string is null.

+ * + *
+     * Validate.validIndex(myStr, 2);
+     * 
+ * + *

If the string index is invalid the message in the exception is + * 'The validated string index is invalid: ' followed by the index.

+ * + *

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 + * @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: "); + } + } diff --git a/src/test/org/apache/commons/lang/ValidateTest.java b/src/test/org/apache/commons/lang/ValidateTest.java index 7080f9c59..eaf05f4dd 100644 --- a/src/test/org/apache/commons/lang/ValidateTest.java +++ b/src/test/org/apache/commons/lang/ValidateTest.java @@ -597,5 +597,120 @@ public class ValidateTest extends TestCase { 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 coll = new ArrayList(); + 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 coll = new ArrayList(); + 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()); + } + } + }