commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sebbASF <...@git.apache.org>
Subject [GitHub] commons-lang pull request #336: LANG-1402: Null/index safe get methods for A...
Date Sun, 08 Jul 2018 23:00:12 GMT
Github user sebbASF commented on a diff in the pull request:

    https://github.com/apache/commons-lang/pull/336#discussion_r200857070
  
    --- Diff: src/main/java/org/apache/commons/lang3/ArrayUtils.java ---
    @@ -8672,4 +8672,53 @@ public static void shuffle(final double[] array, final Random random)
{
                 swap(array, i - 1, random.nextInt(i), 1);
             }
         }
    +
    +    /**
    +     * Gets an element from the array if the array is non-null and appropriately long,
otherwise returns null
    +     * @param <T> the component type of the array
    +     * @param array   the array holding the desired element
    +     * @param index  the index of the element in the array
    +     * @return The element in the array at the index, or null if it is ill-formatted
    +     * @since 3.8
    +     */
    +    public static <T> T get(T[] array, int index){
    +        return get(array, index, null);
    +    }
    +
    +    /**
    +     * Gets an element from the array if the array is non-null and appropriately long,
otherwise returns the specified value
    +     * @param <T> the component type of the array
    +     * @param array   the array holding the desired element
    +     * @param index  the index of the element in the array
    +     * @param defaultReturn the object to be returned if the array is null or shorter
than the index
    +     * @return The element in the array at the specified index, or the given argument
if it is ill-formatted
    +     * @since 3.8
    +     */
    +    public static <T> T get(T[] array, int index, T defaultReturn){
    +        if(getLength(array) == 0 || array.length <= index){
    +            return defaultReturn;
    +        }
    +
    +        if(index < 0 ){
    +            index = 0;
    +        }
    +
    +        return array[index];
    +    }
    +
    +    /**
    +     * Gets an element from the array if the array is non-null and appropriately long,
otherwise returns the specified value
    --- End diff --
    
    Does not agree with code


---

Mime
View raw message