Author: bayard
Date: Wed Mar 3 08:56:22 2010
New Revision: 918366
URL: http://svn.apache.org/viewvc?rev=918366&view=rev
Log:
Applying Valentin Rocher's patch from LANG-559, adding isInstanceOf and
isAssignableFrom methods.
Modified:
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Validate.java
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ValidateTest.java
Modified:
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Validate.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Validate.java?rev=918366&r1=918365&r2=918366&view=diff
==============================================================================
---
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Validate.java
(original)
+++
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Validate.java
Wed Mar 3 08:56:22 2010
@@ -65,6 +65,8 @@
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_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.
@@ -915,4 +917,97 @@
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));
+ }
+ }
}
Modified:
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ValidateTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ValidateTest.java?rev=918366&r1=918365&r2=918366&view=diff
==============================================================================
---
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ValidateTest.java
(original)
+++
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ValidateTest.java
Wed Mar 3 08:56:22 2010
@@ -20,6 +20,7 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
+import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -846,4 +847,49 @@
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());
+ }
+ }
+
}