Skip to content

Commit 122def3

Browse files
committed
fix LANG-1816
1 parent cebde03 commit 122def3

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/main/java/org/apache/commons/lang3/ArrayUtils.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2830,13 +2830,14 @@ public static int indexOf(final double[] array, final double valueToFind, final
28302830
* @return the index of the value within the array, {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input.
28312831
*/
28322832
public static int indexOf(final double[] array, final double valueToFind, final int startIndex) {
2833+
if (Double.isNaN(valueToFind)) {
2834+
return indexOfNaN(array, startIndex);
2835+
}
28332836
if (isEmpty(array)) {
28342837
return INDEX_NOT_FOUND;
28352838
}
2836-
final boolean searchNaN = Double.isNaN(valueToFind);
28372839
for (int i = max0(startIndex); i < array.length; i++) {
2838-
final double element = array[i];
2839-
if (valueToFind == element || searchNaN && Double.isNaN(element)) {
2840+
if (valueToFind == array[i]) {
28402841
return i;
28412842
}
28422843
}
@@ -2860,6 +2861,9 @@ public static int indexOf(final double[] array, final double valueToFind, final
28602861
* @return the index of the value within the array, {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input.
28612862
*/
28622863
public static int indexOf(final double[] array, final double valueToFind, final int startIndex, final double tolerance) {
2864+
if (Double.isNaN(valueToFind)) {
2865+
return indexOfNaN(array, startIndex);
2866+
}
28632867
if (isEmpty(array)) {
28642868
return INDEX_NOT_FOUND;
28652869
}
@@ -2873,6 +2877,24 @@ public static int indexOf(final double[] array, final double valueToFind, final
28732877
return INDEX_NOT_FOUND;
28742878
}
28752879

2880+
/**
2881+
* Finds the index of the NaN value in a double array.
2882+
* @param array the array to search for NaN, may be {@code null}.
2883+
* @param startIndex the index to start searching.
2884+
* @return the index of the NaN value within the array, {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input.
2885+
*/
2886+
private static int indexOfNaN(final double[] array, final int startIndex) {
2887+
if (isEmpty(array)) {
2888+
return INDEX_NOT_FOUND;
2889+
}
2890+
for (int i = max0(startIndex); i < array.length; i++) {
2891+
if (Double.isNaN(array[i])) {
2892+
return i;
2893+
}
2894+
}
2895+
return INDEX_NOT_FOUND;
2896+
}
2897+
28762898
/**
28772899
* Finds the index of the given value in the array.
28782900
* <p>

src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,10 @@ void testContainsDoubleNaN() {
362362
assertTrue(ArrayUtils.contains(a, Double.POSITIVE_INFINITY));
363363
assertTrue(ArrayUtils.contains(a, Double.NEGATIVE_INFINITY));
364364
assertTrue(ArrayUtils.contains(a, Double.NaN));
365+
366+
assertTrue(ArrayUtils.contains(a, Double.POSITIVE_INFINITY, 0.1));
367+
assertTrue(ArrayUtils.contains(a, Double.NEGATIVE_INFINITY, 0.1));
368+
assertTrue(ArrayUtils.contains(a, Double.NaN, 0.1));
365369
}
366370

367371
@Test
@@ -1135,17 +1139,21 @@ void testIndexOfDouble() {
11351139
void testIndexOfDoubleNaN() {
11361140
final double[] array = { Double.NEGATIVE_INFINITY, Double.NaN, Double.POSITIVE_INFINITY, Double.NaN };
11371141
assertEquals(0, ArrayUtils.indexOf(array, Double.NEGATIVE_INFINITY));
1142+
assertEquals(0, ArrayUtils.indexOf(array, Double.NEGATIVE_INFINITY, (double) 0));
11381143
assertEquals(1, ArrayUtils.indexOf(array, Double.NaN));
1144+
assertEquals(1, ArrayUtils.indexOf(array, Double.NaN, (double) 0));
11391145
assertEquals(2, ArrayUtils.indexOf(array, Double.POSITIVE_INFINITY));
1140-
1146+
assertEquals(2, ArrayUtils.indexOf(array, Double.POSITIVE_INFINITY, (double) 0));
11411147
}
11421148

11431149
@Test
11441150
void testIndexOfDoubleTolerance() {
11451151
double[] array = null;
11461152
assertEquals(-1, ArrayUtils.indexOf(array, (double) 0, (double) 0));
1153+
assertEquals(-1, ArrayUtils.indexOf(array, Double.NaN, (double) 0));
11471154
array = new double[0];
11481155
assertEquals(-1, ArrayUtils.indexOf(array, (double) 0, (double) 0));
1156+
assertEquals(-1, ArrayUtils.indexOf(array, Double.NaN, (double) 0));
11491157
array = new double[]{0, 1, 2, 3, 0};
11501158
assertEquals(0, ArrayUtils.indexOf(array, 0, 0.3));
11511159
assertEquals(2, ArrayUtils.indexOf(array, 2.2, 0.35));

0 commit comments

Comments
 (0)