Skip to content

Commit c477140

Browse files
committed
LANG-1816: ArrayUtils contains/indexOf/indexesOf with tolerance fail to
match NaN values #1589
1 parent 24e0b28 commit c477140

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ The <action> type attribute can be add,update,fix,remove.
110110
<action type="fix" dev="ggregory" due-to="Ivan Ponomarev">Fix Javadoc in DoubleRange.of(Double, Double) to reflect actual exception type thrown #1581.</action>
111111
<action issue="LANG-1452" type="fix" dev="ggregory" due-to="Gary Gregory, Aleksey Novozhilov, Sara">RecursiveToStringStyle and MultilineRecursiveToStringStyle shouldn't recurse into a java.math.BigDecimal #1584.</action>
112112
<action issue="LANG-1814" type="fix" dev="ggregory" due-to="Ivan Ponomarev">ArrayUtils.subarray(..) may overflow index arithmetic and violate contract for extreme index values.</action>
113+
<action issue="LANG-1816" type="fix" dev="ggregory" due-to="Ivan Ponomarev, Gary Gregory">ArrayUtils contains/indexOf/indexesOf with tolerance fail to match NaN values #1589.</action>
113114
<!-- ADD -->
114115
<action type="add" dev="ggregory" due-to="Gary Gregory">Add JavaVersion.JAVA_27.</action>
115116
<action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemUtils.IS_JAVA_27.</action>

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2877,24 +2877,6 @@ public static int indexOf(final double[] array, final double valueToFind, final
28772877
return INDEX_NOT_FOUND;
28782878
}
28792879

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-
28982880
/**
28992881
* Finds the index of the given value in the array.
29002882
* <p>
@@ -3106,6 +3088,24 @@ public static int indexOf(final short[] array, final short valueToFind, final in
31063088
return INDEX_NOT_FOUND;
31073089
}
31083090

3091+
/**
3092+
* Finds the index of the NaN value in a double array.
3093+
* @param array the array to search for NaN, may be {@code null}.
3094+
* @param startIndex the index to start searching.
3095+
* @return the index of the NaN value within the array, {@link #INDEX_NOT_FOUND} ({@code -1}) if not found or {@code null} array input.
3096+
*/
3097+
private static int indexOfNaN(final double[] array, final int startIndex) {
3098+
if (isEmpty(array)) {
3099+
return INDEX_NOT_FOUND;
3100+
}
3101+
for (int i = max0(startIndex); i < array.length; i++) {
3102+
if (Double.isNaN(array[i])) {
3103+
return i;
3104+
}
3105+
}
3106+
return INDEX_NOT_FOUND;
3107+
}
3108+
31093109
/**
31103110
* Inserts elements into an array at the given index (starting from zero).
31113111
*

0 commit comments

Comments
 (0)