Repository: commons-lang Updated Branches: refs/heads/master a36c903d4 -> 1deca6672
LANG-1402: added get methods to ArrayUtils Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/9379d0d3 Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/9379d0d3 Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/9379d0d3 Branch: refs/heads/master Commit: 9379d0d36acdf78455e81518b3b1476c7691f056 Parents: c241b09 Author: MarkDacek <mark.da...@richmond.edu> Authored: Sat Jul 7 12:45:15 2018 -0400 Committer: MarkDacek <mark.da...@richmond.edu> Committed: Sat Jul 7 13:04:19 2018 -0400 ---------------------------------------------------------------------- .../org/apache/commons/lang3/ArrayUtils.java | 33 ++++++++++++++++++++ .../apache/commons/lang3/ArrayUtilsTest.java | 16 ++++++++++ 2 files changed, 49 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-lang/blob/9379d0d3/src/main/java/org/apache/commons/lang3/ArrayUtils.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index daf9596..a51726d 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -8672,4 +8672,37 @@ public class ArrayUtils { 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 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 + * @since 3.8 + */ + public static Object get(Object[] 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 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 + * @since 3.8 + */ + public static Object get(Object[] array, int index, Object defaultReturn){ + if(getLength(array) == 0 || array.length <= index){ + return defaultReturn; + } + + if(index < 0 ){ + index = 0; + } + + return array[index]; + } } http://git-wip-us.apache.org/repos/asf/commons-lang/blob/9379d0d3/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java index a853642..25028d0 100644 --- a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java @@ -5111,4 +5111,20 @@ public class ArrayUtilsTest { assertTrue("Element " + element + " not found", ArrayUtils.contains(array1, element)); } } + + @Test + public void testGet(){ + assertNull(ArrayUtils.get(null, 0)); + String[] array = new String[1]; + assertNull(ArrayUtils.get(array, 1)); + array[0] = "Hello World"; + //test with happy path + assertNotNull(ArrayUtils.get(array, 0)); + + //test with default getter + assertEquals("Test", ArrayUtils.get(array, 10, "Test")); + + //negative index + assertEquals("Hello World", ArrayUtils.get(array, -1)); + } }