You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ch...@apache.org on 2018/08/09 19:54:08 UTC

[02/10] [lang] refactored to Generics and added isArrayIndexValid

refactored to Generics and added isArrayIndexValid


Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/2521d961
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/2521d961
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/2521d961

Branch: refs/heads/master
Commit: 2521d9619fe1f052ced8ea1107851ac98a1b7488
Parents: 9379d0d
Author: MarkDacek <ma...@richmond.edu>
Authored: Sun Jul 8 16:15:54 2018 -0400
Committer: MarkDacek <ma...@richmond.edu>
Committed: Sun Jul 8 16:15:54 2018 -0400

----------------------------------------------------------------------
 .../org/apache/commons/lang3/ArrayUtils.java    | 36 ++++++++++++++------
 .../apache/commons/lang3/ArrayUtilsTest.java    | 15 ++++++++
 2 files changed, 41 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/2521d961/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 a51726d..c7f73fe 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 <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 Object get(Object[] array, int index){
+    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 array   the array holding the desired object
-     * @param index  the index of the object in the array
+     * @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 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> 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 <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 Whether the given index is safely-accessible in the given array
+     * @since 3.8
+     */
+    public static <T> boolean isArrayIndexValid(T[] array, int index){
+        if(getLength(array) == 0 || array.length <= index){
+            return false;
+        }
+
+        return index >= 0;
+    }
 }

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/2521d961/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 25028d0..984184c 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));
+    }
 }