From 2521d9619fe1f052ced8ea1107851ac98a1b7488 Mon Sep 17 00:00:00 2001 From: MarkDacek Date: Sun, 8 Jul 2018 16:15:54 -0400 Subject: [PATCH] refactored to Generics and added isArrayIndexValid --- .../org/apache/commons/lang3/ArrayUtils.java | 36 +++++++++++++------ .../apache/commons/lang3/ArrayUtilsTest.java | 15 ++++++++ 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index a51726d60..c7f73fe75 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -8675,26 +8675,26 @@ public class ArrayUtils { /** * Gets an element from the array if the array is non-null and appropriately long, otherwise returns null - * - * @param array the array holding the desired object - * @param index the index of the object in the array - * @return The Object in the array at the index, or null if it is ill-formatted + * @param 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 Object get(Object[] array, int index){ + public static 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 array the array holding the desired object - * @param index the index of the object in the array + * @param 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 object in the array at the specified index, or the given Object if it is ill-formatted + * @return The element in the array at the specified index, or the given argument if it is ill-formatted * @since 3.8 */ - public static Object get(Object[] array, int index, Object defaultReturn){ + public static T get(T[] array, int index, T defaultReturn){ if(getLength(array) == 0 || array.length <= index){ return defaultReturn; } @@ -8705,4 +8705,20 @@ public class ArrayUtils { return array[index]; } + + /** + * Gets an element from the array if the array is non-null and appropriately long, otherwise returns the specified value + * @param 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 Whether the given index is safely-accessible in the given array + * @since 3.8 + */ + public static boolean isArrayIndexValid(T[] array, int index){ + if(getLength(array) == 0 || array.length <= index){ + return false; + } + + return index >= 0; + } } diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java index 25028d0a3..984184cbb 100644 --- a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java @@ -5127,4 +5127,19 @@ public class ArrayUtilsTest { //negative index assertEquals("Hello World", ArrayUtils.get(array, -1)); } + + @Test + public void testIsArrayIndexValid(){ + assertFalse(ArrayUtils.isArrayIndexValid(null, 0)); + String[] array = new String[1]; + + //too big + assertFalse(ArrayUtils.isArrayIndexValid(array, 1)); + + //negative index + assertFalse(ArrayUtils.isArrayIndexValid(array, -1)); + + //good to go + assertTrue(ArrayUtils.isArrayIndexValid(array, 0)); + } }