commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r583668 - in /commons/proper/lang/trunk/src: java/org/apache/commons/lang/ClassUtils.java test/org/apache/commons/lang/ClassUtilsTest.java
Date Thu, 11 Oct 2007 01:51:50 GMT
Author: ggregory
Date: Wed Oct 10 18:51:41 2007
New Revision: 583668

URL: http://svn.apache.org/viewvc?rev=583668&view=rev
Log:
[LANG-351] Extension to ClassUtils: Obtain the primitive class from a wrapper. Using @since
2.4.

Modified:
    commons/proper/lang/trunk/src/java/org/apache/commons/lang/ClassUtils.java
    commons/proper/lang/trunk/src/test/org/apache/commons/lang/ClassUtilsTest.java

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/ClassUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/ClassUtils.java?rev=583668&r1=583667&r2=583668&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/ClassUtils.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/ClassUtils.java Wed Oct 10
18:51:41 2007
@@ -24,6 +24,7 @@
 import java.util.List;
 import java.util.Map;
 
+
 /**
  * <p>Operates on classes without using reflection.</p>
  *
@@ -76,6 +77,20 @@
     }
 
     /**
+     * Maps wrapper <code>Class</code>es to their corresponding primitive types.
+     */
+    private static Map wrapperPrimitiveMap = new HashMap();
+    static {
+        for (Iterator it = primitiveWrapperMap.keySet().iterator(); it.hasNext();) {
+            Class primitiveClass = (Class) it.next();
+            Class wrapperClass = (Class) primitiveWrapperMap.get(primitiveClass);
+            if (!primitiveClass.equals(wrapperClass)) {
+                wrapperPrimitiveMap.put(wrapperClass, primitiveClass);
+            }
+        }
+    }
+
+    /**
      * Maps a primitive class name to its corresponding abbreviation used in array class
names.
      */
     private static Map abbreviationMap = new HashMap();
@@ -501,6 +516,56 @@
         Class[] convertedClasses = new Class[classes.length];
         for (int i=0; i < classes.length; i++) {
             convertedClasses[i] = primitiveToWrapper( classes[i] );
+        }
+        return convertedClasses;
+    }
+
+    /**
+     * <p>Converts the specified wrapper class to its corresponding primitive
+     * class.</p>
+     *
+     * <p>This method is the counter part of <code>primitiveToWrapper()</code>.
+     * If the passed in class is a wrapper class for a primitive type, this
+     * primitive type will be returned (e.g. <code>Integer.TYPE</code> for
+     * <code>Integer.class</code>). For other classes, or if the parameter is
+     * <b>null</b>, the return value is <b>null</b>.</p>
+     *
+     * @param cls the class to convert, may be <b>null</b>
+     * @return the corresponding primitive type if <code>cls</code> is a
+     * wrapper class, <b>null</b> otherwise
+     * @see #primitiveToWrapper(Class)
+     * @since 2.4
+     */
+    public static Class wrapperToPrimitive(Class cls) {
+        return (Class) wrapperPrimitiveMap.get(cls);
+    }
+
+    /**
+     * <p>Converts the specified array of wrapper Class objects to an array of
+     * its corresponding primitive Class objects.</p>
+     *
+     * <p>This method invokes <code>wrapperToPrimitive()</code> for each
element
+     * of the passed in array.</p>
+     *
+     * @param classes  the class array to convert, may be null or empty
+     * @return an array which contains for each given class, the primitive class or
+     * <b>null</b> if the original class is not a wrapper class. <code>null</code>
if null input.
+     * Empty array if an empty array passed in.
+     * @see #wrapperToPrimitive(Class)
+     * @since 2.4
+     */
+    public static Class[] wrappersToPrimitives(Class[] classes) {        
+        if (classes == null) {
+            return null;
+        }
+
+        if (classes.length == 0) {
+            return classes;
+        }
+
+        Class[] convertedClasses = new Class[classes.length];
+        for (int i=0; i < classes.length; i++) {
+            convertedClasses[i] = wrapperToPrimitive( classes[i] );
         }
         return convertedClasses;
     }

Modified: commons/proper/lang/trunk/src/test/org/apache/commons/lang/ClassUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/ClassUtilsTest.java?rev=583668&r1=583667&r2=583668&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang/ClassUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang/ClassUtilsTest.java Wed Oct
10 18:51:41 2007
@@ -31,6 +31,8 @@
 import junit.framework.TestSuite;
 import junit.textui.TestRunner;
 
+
+
 /**
  * Unit tests {@link org.apache.commons.lang.ClassUtils}.
  *
@@ -393,6 +395,55 @@
         };
         // This used to return the exact same array, but no longer does.
         assertNotSame("unmodified", noPrimitives, ClassUtils.primitivesToWrappers(noPrimitives));
+    }
+
+    public void testWrapperToPrimitive() {
+        // an array with classes to convert
+        final Class[] primitives = {
+                Boolean.TYPE, Byte.TYPE, Character.TYPE, Short.TYPE,
+                Integer.TYPE, Long.TYPE, Float.TYPE, Double.TYPE
+        };
+        for (int i = 0; i < primitives.length; i++) {
+            Class wrapperCls = ClassUtils.primitiveToWrapper(primitives[i]);
+            assertFalse("Still primitive", wrapperCls.isPrimitive());
+            assertEquals(wrapperCls + " -> " + primitives[i], primitives[i],
+                    ClassUtils.wrapperToPrimitive(wrapperCls));
+        }
+    }
+
+    public void testWrapperToPrimitiveNoWrapper() {
+        assertNull("Wrong result for non wrapper class", ClassUtils.wrapperToPrimitive(String.class));
+    }
+
+    public void testWrapperToPrimitiveNull() {
+        assertNull("Wrong result for null class", ClassUtils.wrapperToPrimitive(null));
+    }
+
+    public void testWrappersToPrimitives() {
+        // an array with classes to test
+        final Class[] classes = {
+                Boolean.class, Byte.class, Character.class, Short.class,
+                Integer.class, Long.class, Float.class, Double.class,
+                String.class, ClassUtils.class, null
+        };
+
+        Class[] primitives = ClassUtils.wrappersToPrimitives(classes);
+        // now test the result
+        assertEquals("Wrong length of result array", classes.length, primitives.length);
+        for (int i = 0; i < classes.length; i++) {
+            Class expectedPrimitive = ClassUtils.wrapperToPrimitive(classes[i]);
+            assertEquals(classes[i] + " -> " + expectedPrimitive, expectedPrimitive,
+                    primitives[i]);
+        }
+    }
+
+    public void testWrappersToPrimitivesNull() {
+        assertNull("Wrong result for null input", ClassUtils.wrappersToPrimitives(null));
+    }
+
+    public void testWrappersToPrimitivesEmpty() {
+        Class[] empty = new Class[0];
+        assertEquals("Wrong result for empty input", empty, ClassUtils.wrappersToPrimitives(empty));
     }
 
     public void testGetClassClassNotFound() throws Exception {



Mime
View raw message