You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Joerg Schaible (JIRA)" <ji...@apache.org> on 2009/12/23 16:30:29 UTC

[jira] Resolved: (LANG-537) Add ArrayUtils.toArray to create generic arrays

     [ https://issues.apache.org/jira/browse/LANG-537?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Joerg Schaible resolved LANG-537.
---------------------------------

    Resolution: Fixed

{noformat}
commit -m "Add ArrayUtils.toArray (LANG-537)." /home/joehni/src/Commons/proper/lang/src/java/org/apache/commons/lang3/ArrayUtils.java /home/joehni/src/Commons/proper/lang/src/test/org/apache/commons/lang3/ArrayUtilsTest.java
    Sending        /home/joehni/src/Commons/proper/lang/src/java/org/apache/commons/lang3/ArrayUtils.java
    Sending        /home/joehni/src/Commons/proper/lang/src/test/org/apache/commons/lang3/ArrayUtilsTest.java
    Transmitting file data ...
    Committed revision 893547.
{noformat}

Change contains recommendations by Hen and Sebb.

> Add ArrayUtils.toArray to create generic arrays
> -----------------------------------------------
>
>                 Key: LANG-537
>                 URL: https://issues.apache.org/jira/browse/LANG-537
>             Project: Commons Lang
>          Issue Type: New Feature
>          Components: lang.*
>    Affects Versions: 3.0
>            Reporter: Joerg Schaible
>            Assignee: Joerg Schaible
>             Fix For: 3.0
>
>
> {code:java}
> /**
>  * 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[] toArray(int size)
> {
>     return T[size]; // compiler error here
> }
> public static &lt;T&gt; T[] toArray(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>
>  *
>  * @param  <T>   the array's element type
>  * @param  items the items of the array
>  * @return the array
>  * @author J&ouml;rg Schaible
>  * @since  3.0
>  */
> public static <T> T[] toArray(final T... items)
> {
>     return items;
> }
> {code}
> Tests:
> {code:java}
> /**
>  * Tests generic array creation with specified 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 generic type.
>  */
> public void testIndirectArrayCreation()
> {
>     final String[] array = toArray("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 = toArray();
>     assertEquals(0, array.length);
> }
> private static <T> T[] toArray(final T... items)
> {
>     return ArrayUtils.toArray(items);
> }
> {code}

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.