commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r594396 - in /commons/proper/lang/trunk/src: java/org/apache/commons/lang/ArrayUtils.java test/org/apache/commons/lang/ArrayUtilsTest.java
Date Tue, 13 Nov 2007 02:38:50 GMT
Author: bayard
Date: Mon Nov 12 18:38:48 2007
New Revision: 594396

URL: http://svn.apache.org/viewvc?rev=594396&view=rev
Log:
Applying Dave Meikle's patch to LANG-333; adding a toClass(Object[]);Class[] method to ArrayUtils

Modified:
    commons/proper/lang/trunk/src/java/org/apache/commons/lang/ArrayUtils.java
    commons/proper/lang/trunk/src/test/org/apache/commons/lang/ArrayUtilsTest.java

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/ArrayUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/ArrayUtils.java?rev=594396&r1=594395&r2=594396&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/ArrayUtils.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/ArrayUtils.java Mon Nov 12
18:38:48 2007
@@ -4413,5 +4413,27 @@
         
         return result;
     }
-    
+
+    /**
+     * <p>Converts an array of <code>Object</code> in to an Array of <code>Class</code>
objects.</p>
+     *
+     * <p>This method returns <code>null</code> for a <code>null</code>
input array.</p>
+     *
+     * @param array an <code>Object</code> array
+     * @return a <code>Class</code> array, <code>null</code> if null
array input
+     */
+    public static Class[] toClass(Object[] array)
+    {
+        if (array == null) {
+            return null;
+        } else if (array.length == 0) {
+            return EMPTY_CLASS_ARRAY;
+        }
+        Class[] classes = new Class[array.length];
+        for (int i = 0; i < array.length; i++) {
+            classes[i] = array[i].getClass();
+        }
+        return classes;
+    }
+
 }

Modified: commons/proper/lang/trunk/src/test/org/apache/commons/lang/ArrayUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/ArrayUtilsTest.java?rev=594396&r1=594395&r2=594396&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang/ArrayUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang/ArrayUtilsTest.java Mon Nov
12 18:38:48 2007
@@ -2420,6 +2420,24 @@
                 new double[] { Double.MIN_VALUE, Double.MAX_VALUE, 9999999 })));
     }
 
+    public void testToClass_object() {
+        assertEquals(null, ArrayUtils.toClass(null));
+
+        assertSame(
+            ArrayUtils.EMPTY_CLASS_ARRAY,
+            ArrayUtils.toClass(new Class[0]));
+
+        Object[] array = new Object[3];
+        array[0] = new String("Test");
+        array[1] = new Integer(1);
+        array[2] = new Double(99);
+
+        Class[] results = ArrayUtils.toClass(array);
+        assertEquals("String", results[0].getSimpleName());
+        assertEquals("Integer", results[1].getSimpleName());
+        assertEquals("Double", results[2].getSimpleName());
+    }
+
     //-----------------------------------------------------------------------
     /**
      * Test for {@link ArrayUtils#isEmpty(java.lang.Object[])}.



Mime
View raw message