From 4f31ae757ce3e2ebee99af485050e4aa3a23f4f4 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Wed, 3 Mar 2010 08:56:22 +0000 Subject: [PATCH] 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 --- .../org/apache/commons/lang3/Validate.java | 95 +++++++++++++++++++ .../apache/commons/lang3/ValidateTest.java | 46 +++++++++ 2 files changed, 141 insertions(+) diff --git a/src/main/java/org/apache/commons/lang3/Validate.java b/src/main/java/org/apache/commons/lang3/Validate.java index 02a9bf7b8..44f5cbe41 100644 --- a/src/main/java/org/apache/commons/lang3/Validate.java +++ b/src/main/java/org/apache/commons/lang3/Validate.java @@ -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_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 @@ public class Validate { throw new IllegalArgumentException(String.format(message, values)); } } + + /** + *

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

+ * + *
Validate.isInstanceOf(OkClass.class, object);
+ * + *

The message of the exception is "The validated object is not an instance of" + * followed by the name of the class

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

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

+ * + *
Validate.isInstanceOf(OkClass.classs, object, "Wrong class, object is of class %s", object.getClass().getName());
+ * + * @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)); + } + } + + /** + *

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.

+ * + *
Validate.isAssignableFrom(SuperClass.class, object.getClass());
+ * + *

The message of the exception is "The validated object can not be converted to the" + * followed by the name of the class and "class"

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

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.

+ * + *
Validate.isAssignableFrom(SuperClass.class, object.getClass());
+ * + *

The message of the exception is "The validated object can not be converted to the" + * followed by the name of the class and "class"

+ * + * @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)); + } + } } diff --git a/src/test/java/org/apache/commons/lang3/ValidateTest.java b/src/test/java/org/apache/commons/lang3/ValidateTest.java index 647bc4630..ba7c68c07 100644 --- a/src/test/java/org/apache/commons/lang3/ValidateTest.java +++ b/src/test/java/org/apache/commons/lang3/ValidateTest.java @@ -20,6 +20,7 @@ package org.apache.commons.lang3; 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 @@ public class ValidateTest extends TestCase { 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()); + } + } + }