[
https://issues.apache.org/jira/browse/LANG-1050?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14173495#comment-14173495
]
Duncan Jones commented on LANG-1050:
------------------------------------
To avoid {{@SuppressWarning}} statements, the method should look like this:
{code:java}
public static <T> T[] nullToEmpty(final T[] array, final Class<T[]> type) {
if (isEmpty(array)) {
return
type.cast(java.lang.reflect.Array.newInstance(type.getComponentType(), 0));
}
return array;
}
{code}
The usage of such a method would be:
{code:java}
Foo[] x = ArrayUtils.nullToEmpty(null, Foo[].class);
{code}
It's still a bit ugly, but we probably should be offering a generic method
here, since we've taken the time to implement a whole pile of other methods. I
agree with James that we could then call this new method from all the existing
methods. I also agree that the other methods should be using {{isEmpty(array)}}
- I'll make that change separately if we don't end up implementing this method.
> 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
>
> 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)