[ https://issues.apache.org/jira/browse/LANG-537?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12794074#action_12794074 ]
Joerg Schaible edited comment on LANG-537 at 12/23/09 3:30 PM: --------------------------------------------------------------- {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 Stephen. was (Author: joehni): {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 <T> T[] toArray(int size) > { > return T[size]; // compiler error here > } > public static <T> 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.<String>toArray(); > * </pre> > * > * @param <T> the array's element type > * @param items the items of the array > * @return the array > * @author Jö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.