Author: ggregory
Date: Fri Sep 23 00:47:45 2011
New Revision: 1174469
URL: http://svn.apache.org/viewvc?rev=1174469&view=rev
Log:
[LANG-756] Add APIs ClassUtils.isPrimitiveWrapper(Class<?>) and isPrimitiveOrWrapper(Class<?>)
Modified:
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ClassUtils.java
commons/proper/lang/trunk/src/site/changes/changes.xml
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java
Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ClassUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ClassUtils.java?rev=1174469&r1=1174468&r2=1174469&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ClassUtils.java (original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/ClassUtils.java Fri Sep
23 00:47:45 2011
@@ -524,6 +524,37 @@ public class ClassUtils {
}
/**
+ * Returns whether the given {@code type} is a primitive or primitive wrapper ({@link
Boolean}, {@link Byte}, {@link Character},
+ * {@link Short}, {@link Integer}, {@link Long}, {@link Double}, {@link Float}).
+ *
+ * @param type
+ * The class to query or null.
+ * @return true if the given {@code type} is a primitive or primitive wrapper ({@link
Boolean}, {@link Byte}, {@link Character},
+ * {@link Short}, {@link Integer}, {@link Long}, {@link Double}, {@link Float}).
+ * @since 3.0.2
+ */
+ public static boolean isPrimitiveOrWrapper(Class<?> type) {
+ if (type == null) {
+ return false;
+ }
+ return type.isPrimitive() || isPrimitiveWrapper(type);
+ }
+
+ /**
+ * Returns whether the given {@code type} is a primitive wrapper ({@link Boolean}, {@link
Byte}, {@link Character}, {@link Short},
+ * {@link Integer}, {@link Long}, {@link Double}, {@link Float}).
+ *
+ * @param type
+ * The class to query or null.
+ * @return true if the given {@code type} is a primitive wrapper ({@link Boolean}, {@link
Byte}, {@link Character}, {@link Short},
+ * {@link Integer}, {@link Long}, {@link Double}, {@link Float}).
+ * @since 3.0.2
+ */
+ public static boolean isPrimitiveWrapper(Class<?> type) {
+ return wrapperPrimitiveMap.containsKey(type);
+ }
+
+ /**
* <p>Checks if one {@code Class} can be assigned to a variable of
* another {@code Class}.</p>
*
@@ -1100,4 +1131,5 @@ public class ClassUtils {
}
}
}
+
}
Modified: commons/proper/lang/trunk/src/site/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/site/changes/changes.xml?rev=1174469&r1=1174468&r2=1174469&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/site/changes/changes.xml (original)
+++ commons/proper/lang/trunk/src/site/changes/changes.xml Fri Sep 23 00:47:45 2011
@@ -21,7 +21,8 @@
</properties>
<body>
- <release version="3.0.2" date="unreleased" description="September release">
+ <release version="3.0.2" date="unreleased" description="September release">
+ <action type="add" issue="LANG-756">Add APIs ClassUtils.isPrimitiveWrapper(Class<?>)
and isPrimitiveOrWrapper(Class<?>)</action>
<action type="update" issue="LANG-752">Fix createLong() so it behaves like createInteger()</action>
<action type="update" issue="LANG-751">Include the actual type in the Validate.isInstance
and isAssignableFrom exception messages</action>
<action type="fix" issue="LANG-746">NumberUtils does not handle upper-case hex:
0X and -0X</action>
Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java?rev=1174469&r1=1174468&r2=1174469&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java Fri
Sep 23 00:47:45 2011
@@ -716,6 +716,66 @@ public class ClassUtilsTest extends Test
assertTrue("boolean -> boolean", ClassUtils.isAssignable(Boolean.class, Boolean.TYPE,
true));
}
+ public void testIsPrimitiveOrWrapper() {
+
+ // test primitive wrapper classes
+ assertTrue("Boolean.class", ClassUtils.isPrimitiveOrWrapper(Boolean.class));
+ assertTrue("Byte.class", ClassUtils.isPrimitiveOrWrapper(Byte.class));
+ assertTrue("Character.class", ClassUtils.isPrimitiveOrWrapper(Character.class));
+ assertTrue("Short.class", ClassUtils.isPrimitiveOrWrapper(Short.class));
+ assertTrue("Integer.class", ClassUtils.isPrimitiveOrWrapper(Integer.class));
+ assertTrue("Long.class", ClassUtils.isPrimitiveOrWrapper(Long.class));
+ assertTrue("Double.class", ClassUtils.isPrimitiveOrWrapper(Double.class));
+ assertTrue("Float.class", ClassUtils.isPrimitiveOrWrapper(Float.class));
+
+ // test primitive classes
+ assertTrue("boolean", ClassUtils.isPrimitiveOrWrapper(Boolean.TYPE));
+ assertTrue("byte", ClassUtils.isPrimitiveOrWrapper(Byte.TYPE));
+ assertTrue("char", ClassUtils.isPrimitiveOrWrapper(Character.TYPE));
+ assertTrue("short", ClassUtils.isPrimitiveOrWrapper(Short.TYPE));
+ assertTrue("int", ClassUtils.isPrimitiveOrWrapper(Integer.TYPE));
+ assertTrue("long", ClassUtils.isPrimitiveOrWrapper(Long.TYPE));
+ assertTrue("double", ClassUtils.isPrimitiveOrWrapper(Double.TYPE));
+ assertTrue("float", ClassUtils.isPrimitiveOrWrapper(Float.TYPE));
+ assertTrue("Void.TYPE", ClassUtils.isPrimitiveOrWrapper(Void.TYPE));
+
+ // others
+ assertFalse("null", ClassUtils.isPrimitiveOrWrapper(null));
+ assertFalse("Void.class", ClassUtils.isPrimitiveOrWrapper(Void.class));
+ assertFalse("String.class", ClassUtils.isPrimitiveOrWrapper(String.class));
+ assertFalse("this.getClass()", ClassUtils.isPrimitiveOrWrapper(this.getClass()));
+ }
+
+ public void testIsPrimitiveWrapper() {
+
+ // test primitive wrapper classes
+ assertTrue("Boolean.class", ClassUtils.isPrimitiveWrapper(Boolean.class));
+ assertTrue("Byte.class", ClassUtils.isPrimitiveWrapper(Byte.class));
+ assertTrue("Character.class", ClassUtils.isPrimitiveWrapper(Character.class));
+ assertTrue("Short.class", ClassUtils.isPrimitiveWrapper(Short.class));
+ assertTrue("Integer.class", ClassUtils.isPrimitiveWrapper(Integer.class));
+ assertTrue("Long.class", ClassUtils.isPrimitiveWrapper(Long.class));
+ assertTrue("Double.class", ClassUtils.isPrimitiveWrapper(Double.class));
+ assertTrue("Float.class", ClassUtils.isPrimitiveWrapper(Float.class));
+
+ // test primitive classes
+ assertFalse("boolean", ClassUtils.isPrimitiveWrapper(Boolean.TYPE));
+ assertFalse("byte", ClassUtils.isPrimitiveWrapper(Byte.TYPE));
+ assertFalse("char", ClassUtils.isPrimitiveWrapper(Character.TYPE));
+ assertFalse("short", ClassUtils.isPrimitiveWrapper(Short.TYPE));
+ assertFalse("int", ClassUtils.isPrimitiveWrapper(Integer.TYPE));
+ assertFalse("long", ClassUtils.isPrimitiveWrapper(Long.TYPE));
+ assertFalse("double", ClassUtils.isPrimitiveWrapper(Double.TYPE));
+ assertFalse("float", ClassUtils.isPrimitiveWrapper(Float.TYPE));
+
+ // others
+ assertFalse("null", ClassUtils.isPrimitiveWrapper(null));
+ assertFalse("Void.class", ClassUtils.isPrimitiveWrapper(Void.class));
+ assertFalse("Void.TYPE", ClassUtils.isPrimitiveWrapper(Void.TYPE));
+ assertFalse("String.class", ClassUtils.isPrimitiveWrapper(String.class));
+ assertFalse("this.getClass()", ClassUtils.isPrimitiveWrapper(this.getClass()));
+ }
+
public void testPrimitiveToWrapper() {
// test primitive classes
|