[LANG-1550] Optimize ArrayUtils::isArrayIndexValid method. (#551)

* [LANG-1550] Optimize ArrayUtils::isArrayIndexValid method.
This commit is contained in:
Edgar Asatryan 2020-06-13 18:39:41 +04:00 committed by GitHub
parent 96a8969c13
commit 874cc14328
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 5 deletions

View File

@ -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. * 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 <T> the component type of the array
* @param array the array to inspect, may be null * @param array the array to inspect, may be null
* @param index the index of the array to be inspected * @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 * @since 3.8
*/ */
public static <T> boolean isArrayIndexValid(final T[] array, final int index) { public static <T> boolean isArrayIndexValid(final T[] array, final int index) {
if (getLength(array) == 0 || array.length <= index) { return index >= 0 && getLength(array) > index;
return false;
}
return index >= 0;
} }
/** /**