[
https://issues.apache.org/jira/browse/LANG-1050?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14174832#comment-14174832
]
Joerg Schaible edited comment on LANG-1050 at 10/17/14 7:56 AM:
----------------------------------------------------------------
Well, our unit test works for this:
{code}
public void testEmptyArrayCreation()
{
final String[] array = ArrayUtils.<String>toArray();
assertEquals(0, array.length);
}
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);
}
{code}
Therefore I'd extected that it works when replaceing the Object[] version with
(ignoring BC for now):
{code}
public static <T> T[] nullToEmpty(final T... array)
{
if (isEmpty(array)) {
return ArrayUtils.<T>toArray();
}
return array;
}
{code}
However, you're right, I assumed wrongly, that the type is propagated. The unit
test above simply works, because null is never expected as the array itself and
we get an array with a null element for this:
{code}
return ArrayUtils.toArray(array);
{code}
Since there seems to be no proper solution, I withdraw my -1.
was (Author: joehni):
Well, our unit test works for this:
{code}
public void testEmptyArrayCreation()
{
final String[] array = ArrayUtils.<String>toArray();
assertEquals(0, array.length);
}
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);
}
{code}
Therefore I'd extected that it works when replaceing the Object[] version with
(ignoring BC for now):
{code}
public static <T> T[] nullToEmpty(final T... array)
{
if (isEmpty(array)) {
return ArrayUtils.<>toArray();
}
return array;
}
{code}
However, you're right, I assumed wrongly, that the type is propagated. The unit
test above simply works, because null is never expected as the array itself and
we get an array with a null element for this:
{code}
return ArrayUtils.toArray(array);
{code}
Since there seems to be no proper solution, I withdraw my -1.
> Change nullToEmpty methods to generics
> --------------------------------------
>
> Key: LANG-1050
> URL: https://issues.apache.org/jira/browse/LANG-1050
> Project: Commons Lang
> Issue Type: Improvement
> Components: lang.*
> Reporter: James Sawle
> Fix For: Review Patch
>
>
> Currently there are multiple Object based methods which could be replaced by
> a single generic method.
> - public static Long[] nullToEmpty(final Long[] array)
> - public static Integer[] nullToEmpty(final Integer[] array)
> - public static Short[] nullToEmpty(final Short[] array)
> - public static Character[] nullToEmpty(final Character[] array)
> - public static Byte[] nullToEmpty(final Byte[] array)
> - public static Double[] nullToEmpty(final Double[] array)
> - public static Float[] nullToEmpty(final Float[] array)
> - public static Boolean[] nullToEmpty(final Boolean[] array)
> Recommendation, replace all of these with a single method that would also
> allow a defensive programming style when not using wrapped primitives.
> - public static <T> T[] nullToEmpty(final T[] array)
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)