From 9379d0d36acdf78455e81518b3b1476c7691f056 Mon Sep 17 00:00:00 2001 From: MarkDacek Date: Sat, 7 Jul 2018 12:45:15 -0400 Subject: [PATCH 1/8] LANG-1402: added get methods to ArrayUtils --- .../org/apache/commons/lang3/ArrayUtils.java | 33 +++++++++++++++++++ .../apache/commons/lang3/ArrayUtilsTest.java | 16 +++++++++ 2 files changed, 49 insertions(+) diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index daf9596b9cd..a51726d6058 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 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 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]; + } } diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java index a853642e19b..25028d0a328 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 void testShuffleDouble() { 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)); + } } From 2521d9619fe1f052ced8ea1107851ac98a1b7488 Mon Sep 17 00:00:00 2001 From: MarkDacek Date: Sun, 8 Jul 2018 16:15:54 -0400 Subject: [PATCH 2/8] 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 a51726d6058..c7f73fe75a4 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 static void shuffle(final double[] array, final Random random) { /** * 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 static Object get(Object[] array, int index, Object defaultReturn){ 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 25028d0a328..984184cbbfd 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 void testGet(){ //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)); + } } From 625fbccaa9c42d5efbe4993e9701d8db0b53f5e9 Mon Sep 17 00:00:00 2001 From: MarkDacek Date: Sun, 8 Jul 2018 19:06:48 -0400 Subject: [PATCH 3/8] LANG-1402: Fixed comment --- src/main/java/org/apache/commons/lang3/ArrayUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index c7f73fe75a4..8b7e319e100 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -8707,7 +8707,7 @@ public static T get(T[] array, int index, T defaultReturn){ } /** - * Gets an element from the array if the array is non-null and appropriately long, otherwise returns the specified value + * Returns whether a given array can safely be accessed at the given index. * @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 From 7721302ae5d70d2986d74ae7e7df648bf849997d Mon Sep 17 00:00:00 2001 From: MarkDacek Date: Sun, 8 Jul 2018 19:11:19 -0400 Subject: [PATCH 4/8] LANG-1402: refactored to return default value on negative index --- src/main/java/org/apache/commons/lang3/ArrayUtils.java | 2 +- src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index 8b7e319e100..7a5f950e681 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -8700,7 +8700,7 @@ public static T get(T[] array, int index, T defaultReturn){ } if(index < 0 ){ - index = 0; + return defaultReturn; } return array[index]; diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java index 984184cbbfd..0121473cc27 100644 --- a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java @@ -5125,7 +5125,7 @@ public void testGet(){ assertEquals("Test", ArrayUtils.get(array, 10, "Test")); //negative index - assertEquals("Hello World", ArrayUtils.get(array, -1)); + assertEquals("Default", ArrayUtils.get(array, -1, "Default")); } @Test From 96f3ab841096b6a480b4cae99bb05aebce1ff92d Mon Sep 17 00:00:00 2001 From: MarkDacek Date: Sun, 8 Jul 2018 19:20:48 -0400 Subject: [PATCH 5/8] LANG-1402: comment proofread --- src/main/java/org/apache/commons/lang3/ArrayUtils.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index 7a5f950e681..3d5c6b02083 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -8675,7 +8675,7 @@ public static void shuffle(final double[] array, final Random random) { /** * Gets an element from the array if the array is non-null and appropriately long, otherwise returns null - * @param the component type of the array + * @param the component type of the array, may be null * @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 @@ -8688,10 +8688,10 @@ public static T get(T[] array, int 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 array the array holding the desired element, may be null * @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 + * @return The element in the array at the specified index, or the given argument if it the array is not sufficiently long for the index. May return null if the array contains null * @since 3.8 */ public static T get(T[] array, int index, T defaultReturn){ @@ -8709,8 +8709,8 @@ public static T get(T[] array, int index, T defaultReturn){ /** * Returns whether a given array can safely be accessed at the given index. * @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 array the array to inspect + * @param index the index of the array to be inspected * @return Whether the given index is safely-accessible in the given array * @since 3.8 */ From ec2ec774925cb845f85a82f85c32d0019de31f01 Mon Sep 17 00:00:00 2001 From: MarkDacek Date: Sun, 8 Jul 2018 19:35:13 -0400 Subject: [PATCH 6/8] LANG-1402: more comment proofreading --- src/main/java/org/apache/commons/lang3/ArrayUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index 3d5c6b02083..9ed6b684f03 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -8678,7 +8678,7 @@ public static void shuffle(final double[] array, final Random random) { * @param the component type of the array, may be null * @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 + * @return The element in the array at the index, or null if the array is not sufficiently long for the index. May return null if the array contains null * @since 3.8 */ public static T get(T[] array, int index){ From 2cad60b6c25c87a6a59d3d315ec7d72c552fbc58 Mon Sep 17 00:00:00 2001 From: MarkDacek Date: Sat, 14 Jul 2018 15:42:14 -0400 Subject: [PATCH 7/8] removed ArrayUtils.get --- .../org/apache/commons/lang3/ArrayUtils.java | 33 ------------------- .../apache/commons/lang3/ArrayUtilsTest.java | 16 --------- 2 files changed, 49 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index 9ed6b684f03..f6b1131c821 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -8673,39 +8673,6 @@ public static void shuffle(final double[] array, final Random random) { } } - /** - * Gets an element from the array if the array is non-null and appropriately long, otherwise returns null - * @param the component type of the array, may be null - * @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 the array is not sufficiently long for the index. May return null if the array contains null - * @since 3.8 - */ - 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 the component type of the array - * @param array the array holding the desired element, may be null - * @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 the array is not sufficiently long for the index. May return null if the array contains null - * @since 3.8 - */ - public static T get(T[] array, int index, T defaultReturn){ - if(getLength(array) == 0 || array.length <= index){ - return defaultReturn; - } - - if(index < 0 ){ - return defaultReturn; - } - - return array[index]; - } - /** * Returns whether a given array can safely be accessed at the given index. * @param the component type of the array diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java index 0121473cc27..335b9a4a5cc 100644 --- a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java @@ -5112,22 +5112,6 @@ public void testShuffleDouble() { } } - @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("Default", ArrayUtils.get(array, -1, "Default")); - } - @Test public void testIsArrayIndexValid(){ assertFalse(ArrayUtils.isArrayIndexValid(null, 0)); From 3dbc94481291a99ecf28d47b3f338df99d96df40 Mon Sep 17 00:00:00 2001 From: MarkDacek Date: Wed, 8 Aug 2018 21:47:43 -0400 Subject: [PATCH 8/8] clarified Javadoc --- src/main/java/org/apache/commons/lang3/ArrayUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index f6b1131c821..4ff2553b908 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -8676,7 +8676,7 @@ public static void shuffle(final double[] array, final Random random) { /** * Returns whether a given array can safely be accessed at the given index. * @param the component type of the array - * @param array the array to inspect + * @param array the array to inspect, may be null * @param index the index of the array to be inspected * @return Whether the given index is safely-accessible in the given array * @since 3.8