commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Henri Yandell (JIRA)" <j...@apache.org>
Subject [jira] Commented: (LANG-537) Add ArrayUtils.toArray to create generic arrays
Date Sat, 14 Nov 2009 09:14:39 GMT

    [ https://issues.apache.org/jira/browse/LANG-537?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12777845#action_12777845
] 

Henri Yandell commented on LANG-537:
------------------------------------

What does ArrayUtils.toArray(a,b,c,d) have over new String[] { a,b,c,d } ?

> 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
>    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.


Mime
View raw message