commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sebb (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (LANG-1050) Change nullToEmpty methods to generics
Date Thu, 16 Oct 2014 23:43:35 GMT

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

Sebb commented on LANG-1050:
----------------------------

I just noticed - the Javadoc currently states that:

{quote}
As a memory optimizing technique an empty array passed in will be overridden with the empty
public static references in this class.

Parameters:
array the array to check for null or empty
Returns:
the same array, public static empty array if null or empty input
{quote}

This means that it is not possible to convert the existing methods without breaking the contract.

So I think it's only possible to add the new method to deal with arbitrary array types.
This could not return "the public static array".
Also it could be simplified as follows:

{code}
    public static <T> T[] nullToEmpty(final T[] array, final Class<T[]> type)
{
        if(array == null) { // no need to check length, we only want to convert a null array
            return type.cast(java.lang.reflect.Array.newInstance(type.getComponentType(),
0));
        }
        return array;
    }
{code}

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

Mime
View raw message