LANG-546 - Validate.validIndex()

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@831689 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2009-11-01 15:45:09 +00:00
parent c1590455d2
commit 661832fb35
2 changed files with 250 additions and 1 deletions

View File

@ -580,4 +580,138 @@ public class Validate {
}
}
// 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: ");
}
}

View File

@ -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<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());
}
}
}