Applying Valentin Rocher's patch from LANG-559, adding isInstanceOf and isAssignableFrom methods.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@918366 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
6699e44845
commit
4f31ae757c
|
@ -65,6 +65,8 @@ public class Validate {
|
||||||
private static final String DEFAULT_VALID_INDEX_CHAR_SEQUENCE_EX_MESSAGE = "The validated character sequence index is invalid: %d";
|
private static final String DEFAULT_VALID_INDEX_CHAR_SEQUENCE_EX_MESSAGE = "The validated character sequence index is invalid: %d";
|
||||||
private static final String DEFAULT_VALID_INDEX_COLLECTION_EX_MESSAGE = "The validated collection index is invalid: %d";
|
private static final String DEFAULT_VALID_INDEX_COLLECTION_EX_MESSAGE = "The validated collection index is invalid: %d";
|
||||||
private static final String DEFAULT_VALID_STATE_EX_MESSAGE = "The validated state is false";
|
private static final String DEFAULT_VALID_STATE_EX_MESSAGE = "The validated state is false";
|
||||||
|
private static final String DEFAULT_IS_ASSIGNABLE_EX_MESSAGE = "The validated class can not be converted to the %s class";
|
||||||
|
private static final String DEFAULT_IS_INSTANCE_OF_EX_MESSAGE = "The validated object is not an instance of %s";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor. This class should not normally be instantiated.
|
* Constructor. This class should not normally be instantiated.
|
||||||
|
@ -915,4 +917,97 @@ public class Validate {
|
||||||
throw new IllegalArgumentException(String.format(message, values));
|
throw new IllegalArgumentException(String.format(message, values));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Validate that the argument is an instance of the specified class; otherwise
|
||||||
|
* throwing an exception. This method is useful when validating according to an arbitrary
|
||||||
|
* class</p>
|
||||||
|
*
|
||||||
|
* <pre>Validate.isInstanceOf(OkClass.class, object);</pre>
|
||||||
|
*
|
||||||
|
* <p>The message of the exception is "The validated object is not an instance of"
|
||||||
|
* followed by the name of the class</p>
|
||||||
|
*
|
||||||
|
* @param type the class the object must be validated against
|
||||||
|
* @param o the object to check
|
||||||
|
* @throws IllegalArgumentException if argument is not of specified class
|
||||||
|
* @see #isInstanceOf(Class, Object, String, Object...)
|
||||||
|
*/
|
||||||
|
public static void isInstanceOf(Class<?> type, Object o)
|
||||||
|
{
|
||||||
|
if (type.isInstance(o) == false)
|
||||||
|
{
|
||||||
|
throw new IllegalArgumentException(String.format(DEFAULT_IS_INSTANCE_OF_EX_MESSAGE, type.getName()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Validate that the argument is an instance of the specified class; otherwise
|
||||||
|
* throwing an exception with the specified message. This method is useful when
|
||||||
|
* validating according to an arbitrary class</p>
|
||||||
|
*
|
||||||
|
* <pre>Validate.isInstanceOf(OkClass.classs, object, "Wrong class, object is of class %s", object.getClass().getName());</pre>
|
||||||
|
*
|
||||||
|
* @param type the class the object must be validated against
|
||||||
|
* @param o the object to check
|
||||||
|
* @param message exception message
|
||||||
|
* @param values optional value for the exception message
|
||||||
|
* @throws IllegalArgumentException if argument is not of specified class
|
||||||
|
* @see #isInstanceOf(Class, Object)
|
||||||
|
*/
|
||||||
|
public static void isInstanceOf(Class<?> type, Object o, String message, Object... values)
|
||||||
|
{
|
||||||
|
if (type.isInstance(o) == false)
|
||||||
|
{
|
||||||
|
throw new IllegalArgumentException(String.format(message, values));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Validate that the argument can be converted to the specified class; otherwise
|
||||||
|
* throwing an exception with the specified message. This method is useful when
|
||||||
|
* validating if there will be no casting errors.</p>
|
||||||
|
*
|
||||||
|
* <pre>Validate.isAssignableFrom(SuperClass.class, object.getClass());</pre>
|
||||||
|
*
|
||||||
|
* <p>The message of the exception is "The validated object can not be converted to the"
|
||||||
|
* followed by the name of the class and "class"</p>
|
||||||
|
*
|
||||||
|
* @param superType the class the class must be validated against
|
||||||
|
* @param type the class to check
|
||||||
|
* @throws IllegalArgumentException if argument can not be converted to the specified class
|
||||||
|
* @see #isAssignableFrom(Class, Class, String, Object...)
|
||||||
|
*/
|
||||||
|
public static void isAssignableFrom(Class<?> superType, Class<?> type)
|
||||||
|
{
|
||||||
|
if (superType.isAssignableFrom(type) == false)
|
||||||
|
{
|
||||||
|
throw new IllegalArgumentException(String.format(DEFAULT_IS_ASSIGNABLE_EX_MESSAGE, superType.getName()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Validate that the argument can be converted to the specified class; otherwise
|
||||||
|
* throwing an exception. This method is useful when validating if there will be no
|
||||||
|
* casting errors.</p>
|
||||||
|
*
|
||||||
|
* <pre>Validate.isAssignableFrom(SuperClass.class, object.getClass());</pre>
|
||||||
|
*
|
||||||
|
* <p>The message of the exception is "The validated object can not be converted to the"
|
||||||
|
* followed by the name of the class and "class"</p>
|
||||||
|
*
|
||||||
|
* @param superType the class the class must be validated against
|
||||||
|
* @param type the class to check
|
||||||
|
* @param message the exception message if invalid
|
||||||
|
* @param values the optional values for the formatted exception message
|
||||||
|
* @throws IllegalArgumentException if argument can not be converted to the specified class
|
||||||
|
* @see #isAssignableFrom(Class, Class)
|
||||||
|
*/
|
||||||
|
public static void isAssignableFrom(Class<?> superType, Class<?> type, String message, Object... values)
|
||||||
|
{
|
||||||
|
if (superType.isAssignableFrom(type) == false)
|
||||||
|
{
|
||||||
|
throw new IllegalArgumentException(String.format(message, values));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,7 @@ package org.apache.commons.lang3;
|
||||||
|
|
||||||
import java.lang.reflect.Constructor;
|
import java.lang.reflect.Constructor;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
|
import java.util.AbstractList;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
@ -846,4 +847,49 @@ public class ValidateTest extends TestCase {
|
||||||
assertEquals("Error", e.getMessage());
|
assertEquals("Error", e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testIsInstanceOf() {
|
||||||
|
Validate.isInstanceOf(String.class, "hi");
|
||||||
|
Validate.isInstanceOf(Integer.class, 1);
|
||||||
|
try {
|
||||||
|
Validate.isInstanceOf(List.class, "hi");
|
||||||
|
fail("Expecting IllegalArgumentException");
|
||||||
|
} catch(IllegalArgumentException e) {
|
||||||
|
assertEquals("The validated object is not an instance of java.util.List", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testIsInstanceOf_withMessage() {
|
||||||
|
Validate.isInstanceOf(String.class, "hi", "Error");
|
||||||
|
Validate.isInstanceOf(Integer.class, 1, "Error");
|
||||||
|
try {
|
||||||
|
Validate.isInstanceOf(List.class, "hi", "Error");
|
||||||
|
fail("Expecting IllegalArgumentException");
|
||||||
|
} catch(IllegalArgumentException e) {
|
||||||
|
assertEquals("Error", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testIsAssignable() {
|
||||||
|
Validate.isAssignableFrom(CharSequence.class, String.class);
|
||||||
|
Validate.isAssignableFrom(AbstractList.class, ArrayList.class);
|
||||||
|
try {
|
||||||
|
Validate.isAssignableFrom(List.class, String.class);
|
||||||
|
fail("Expecting IllegalArgumentException");
|
||||||
|
} catch(IllegalArgumentException e) {
|
||||||
|
assertEquals("The validated class can not be converted to the java.util.List class", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testIsAssignable_withMessage() {
|
||||||
|
Validate.isAssignableFrom(CharSequence.class, String.class, "Error");
|
||||||
|
Validate.isAssignableFrom(AbstractList.class, ArrayList.class, "Error");
|
||||||
|
try {
|
||||||
|
Validate.isAssignableFrom(List.class, String.class, "Error");
|
||||||
|
fail("Expecting IllegalArgumentException");
|
||||||
|
} catch(IllegalArgumentException e) {
|
||||||
|
assertEquals("Error", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue