You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/06/13 14:39:50 UTC
[commons-lang] branch master updated: [LANG-1550] Optimize
ArrayUtils::isArrayIndexValid method. (#551)
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push:
new 874cc14 [LANG-1550] Optimize ArrayUtils::isArrayIndexValid method. (#551)
874cc14 is described below
commit 874cc143284feb8a469e3d41e05543663cffeafb
Author: Edgar Asatryan <17...@users.noreply.github.com>
AuthorDate: Sat Jun 13 18:39:41 2020 +0400
[LANG-1550] Optimize ArrayUtils::isArrayIndexValid method. (#551)
* [LANG-1550] Optimize ArrayUtils::isArrayIndexValid method.
---
src/main/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 87ee055..ee1b215 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.
+ *
+ * <pre>
+ * ArrayUtils.isArrayIndexValid(null, 0) = false
+ * ArrayUtils.isArrayIndexValid([], 0) = false
+ * ArrayUtils.isArrayIndexValid(["a"], 0) = true
+ * </pre>
+ *
* @param <T> 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 <T> 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;
}
/**