From 874cc143284feb8a469e3d41e05543663cffeafb Mon Sep 17 00:00:00 2001 From: Edgar Asatryan <17509127+nstdio@users.noreply.github.com> Date: Sat, 13 Jun 2020 18:39:41 +0400 Subject: [PATCH] [LANG-1550] Optimize ArrayUtils::isArrayIndexValid method. (#551) * [LANG-1550] Optimize ArrayUtils::isArrayIndexValid method. --- .../java/org/apache/commons/lang3/ArrayUtils.java | 13 ++++++++----- 1 file changed, 8 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 87ee055cf..ee1b2157c 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -3111,6 +3111,13 @@ public static int indexOf(final int[] array, final int valueToFind) { /** * Returns whether a given array can safely be accessed at the given index. + * + *
+     * ArrayUtils.isArrayIndexValid(null, 0)       = false
+     * ArrayUtils.isArrayIndexValid([], 0)         = false
+     * ArrayUtils.isArrayIndexValid(["a"], 0)      = true
+     * 
+ * * @param the component type of the array * @param array the array to inspect, may be null * @param index the index of the array to be inspected @@ -3118,11 +3125,7 @@ public static int indexOf(final int[] array, final int valueToFind) { * @since 3.8 */ public static boolean isArrayIndexValid(final T[] array, final int index) { - if (getLength(array) == 0 || array.length <= index) { - return false; - } - - return index >= 0; + return index >= 0 && getLength(array) > index; } /**