commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From joe...@apache.org
Subject svn commit: r893547 - in /commons/proper/lang/trunk/src: java/org/apache/commons/lang3/ArrayUtils.java test/org/apache/commons/lang3/ArrayUtilsTest.java
Date Wed, 23 Dec 2009 15:27:38 GMT
Author: joehni
Date: Wed Dec 23 15:27:38 2009
New Revision: 893547

URL: http://svn.apache.org/viewvc?rev=893547&view=rev
Log:
Add ArrayUtils.toArray (LANG-537).

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

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang3/ArrayUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang3/ArrayUtils.java?rev=893547&r1=893546&r2=893547&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang3/ArrayUtils.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang3/ArrayUtils.java Wed Dec 23
15:27:38 2009
@@ -247,6 +247,51 @@
         return map;
     }
 
+    // Generic array
+    //-----------------------------------------------------------------------
+    /**
+     * Create a type-safe generic array.
+     *
+     * <p>Arrays are covariant i.e. they cannot be created from a generic type:</p>
+     *
+     * <pre>
+    public static &lt;T&gt; T[] createAnArray(int size)
+    {
+        return T[size]; // compiler error here
+    }
+    public static &lt;T&gt; T[] createAnArray(int size)
+    {
+        return (T[])Object[size]; // ClassCastException at runtime
+    }
+     * </pre>
+     *
+     * <p>Therefore new arrays of generic types can be created with this method, e.g.
an arrays
+     * of Strings:</p>
+     *
+     * <pre>
+    String[] array = ArrayUtils.toArray("1", "2");
+    String[] emptyArray = ArrayUtils.&lt;String&gt;toArray();
+     * </pre>
+     * 
+     * The method is typically used in scenarios, where the caller itself uses generic types
+     * that have to be combined into an array.
+     * 
+     * Note, this method makes only sense to provide arguments of the same type so that the
+     * compiler can deduce the type of the array itself. While it is possible to select the
+     * type explicitly like in <code>Number[] array = ArrayUtils.<Number>toArray(new
+     * Integer(42), new Double(Math.PI))</code>, there is no real advantage to <code>new
+     * Number[] {new Integer(42), new Double(Math.PI)}</code> anymore.
+     *
+     * @param  <T>   the array's element type
+     * @param  items the items of the array
+     * @return the array
+     * @since  3.0
+     */
+    public static <T> T[] toArray(final T... items)
+    {
+        return items;
+    }
+    
     // Clone
     //-----------------------------------------------------------------------
     /**

Modified: commons/proper/lang/trunk/src/test/org/apache/commons/lang3/ArrayUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang3/ArrayUtilsTest.java?rev=893547&r1=893546&r2=893547&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang3/ArrayUtilsTest.java (original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang3/ArrayUtilsTest.java Wed Dec
23 15:27:38 2009
@@ -133,6 +133,72 @@
     }
     
     //-----------------------------------------------------------------------
+    /**
+     * Tests generic array creation with parameters of same type.
+     */
+    public void testArrayCreation()
+    {
+        final String[] array = ArrayUtils.toArray("foo", "bar");
+        assertEquals(2, array.length);
+        assertEquals("foo", array[0]);
+        assertEquals("bar", array[1]);
+    }
+
+    /**
+     * Tests generic array creation with general return type.
+     */
+    public void testArrayCreationWithGeneralReturnType()
+    {
+        final Object obj = ArrayUtils.toArray("foo", "bar");
+        assertTrue(obj instanceof String[]);
+    }
+
+    /**
+     * Tests generic array creation with parameters of common base type.
+     */
+    public void testArrayCreationWithDifferentTypes()
+    {
+        final Number[] array = ArrayUtils.<Number>toArray(Integer.valueOf(42), Double.valueOf(Math.PI));
+        assertEquals(2, array.length);
+        assertEquals(Integer.valueOf(42), array[0]);
+        assertEquals(Double.valueOf(Math.PI), array[1]);
+    }
+
+    /**
+     * Tests generic array creation with generic type.
+     */
+    public void testIndirectArrayCreation()
+    {
+        final String[] array = toArrayPropagatingType("foo", "bar");
+        assertEquals(2, array.length);
+        assertEquals("foo", array[0]);
+        assertEquals("bar", array[1]);
+    }
+
+    /**
+     * Tests generic empty array creation with generic type.
+     */
+    public void testEmptyArrayCreation()
+    {
+        final String[] array = ArrayUtils.<String>toArray();
+        assertEquals(0, array.length);
+    }
+
+    /**
+     * Tests indirect generic empty array creation with generic type.
+     */
+    public void testIndirectEmptyArrayCreation()
+    {
+        final String[] array = ArrayUtilsTest.<String>toArrayPropagatingType();
+        assertEquals(0, array.length);
+    }
+
+    private static <T> T[] toArrayPropagatingType(final T... items)
+    {
+        return ArrayUtils.toArray(items);
+    }
+    
+    //-----------------------------------------------------------------------
     public void testToMap() {
         Map<?, ?> map = ArrayUtils.toMap(new String[][] {{"foo", "bar"}, {"hello",
"world"}});
         



Mime
View raw message