Skip to content

Commit 833ac0c

Browse files
tushartushar
authored andcommitted
Fix countMatches for double[] using Double.compare for proper IEEE 754 handling
1 parent 65d49d9 commit 833ac0c

File tree

1 file changed

+13
-57
lines changed

1 file changed

+13
-57
lines changed

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

Lines changed: 13 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -9240,6 +9240,7 @@ public static short[] toPrimitive(final Short[] array, final short valueForNull)
92409240
* @param array the array to search, may be {@code null}
92419241
* @param value the value to count.
92429242
* @return the number of matches, may be {@code 0} if array is {@code null}
9243+
* @since 3.21.0
92439244
*/
92449245
public static int countMatches(final boolean[] array, final boolean value)
92459246
{
@@ -9302,7 +9303,7 @@ public static int countMatches(final char[] array, final char value)
93029303
}
93039304

93049305
/**
9305-
* Counts how many times the specified number or value occures in the given array.
9306+
* Counts how many times the specified number or value occurs in the given array.
93069307
* <p>
93079308
* A {@code null} array returns {@code 0}.
93089309
* </p>
@@ -9356,12 +9357,12 @@ public static int countMatches(final int[] array, final int value)
93569357
* @param value the value to count.
93579358
* @return the number of matches, may be {@code 0} if array is {@code null}
93589359
*/
9359-
public static long countMatches(final long[] array, final long value)
9360+
public static int countMatches(final long[] array, final long value)
93609361
{
93619362
if(array == null){
93629363
return 0;
93639364
}
9364-
long count = 0;
9365+
int count = 0;
93659366
for(long i : array){
93669367
if(i == value){
93679368
count++;
@@ -9379,91 +9380,46 @@ public static long countMatches(final long[] array, final long value)
93799380
* @param value the value to count.
93809381
* @return the number of matches, may be {@code 0} if array is {@code null}
93819382
*/
9382-
public static long countMatches(final float[] array, final float value)
9383-
{
9384-
if(array == null){
9385-
return 0;
9386-
}
9387-
long count = 0;
9388-
for(float i : array){
9389-
if(i == value){
9390-
count++;
9391-
}
9392-
}
9393-
return count;
9394-
}
9395-
9396-
/**
9397-
* Counts how many times the specified number or value occures in the given array.
9398-
* <p>
9399-
* A {@code null} array returns {@code 0}.
9400-
* </p>
9401-
* @param array the array to search, may be {@code null}
9402-
* @param value the value to count.
9403-
* @return the number of matches, may be {@code 0} if array is {@code null}
9404-
*/
9405-
public static long countMatches(final double[] array, final double value)
9406-
{
9407-
if(array == null){
9408-
return 0;
9409-
}
9410-
long count = 0;
9411-
for(double i : array){
9412-
if(i == value){
9413-
count++;
9414-
}
9415-
}
9416-
return count;
9417-
}
9418-
9419-
/**
9420-
* Counts how many times the specified number or value occures in the given array.
9421-
* <p>
9422-
* A {@code null} array returns {@code 0}.
9423-
* </p>
9424-
* @param array the array to search, may be {@code null}
9425-
* @param value the value to count.
9426-
* @return the number of matches, may be {@code 0} if array is {@code null}
9427-
*/
9428-
public static int countMatches(final byte[] array, final byte value)
9383+
public static int countMatches(final float[] array, final float value)
94299384
{
94309385
if(array == null){
94319386
return 0;
94329387
}
94339388
int count = 0;
9434-
for(byte i : array){
9435-
if(i == value){
9389+
for(float i : array){
9390+
if(Float.compare(i, value) == 0){
94369391
count++;
94379392
}
94389393
}
94399394
return count;
94409395
}
94419396

94429397
/**
9443-
* Counts how many times the specified number or value occures in the given array.
9398+
* Counts how many times the specified number or value occurs in the given array.
94449399
* <p>
94459400
* A {@code null} array returns {@code 0}.
94469401
* </p>
94479402
* @param array the array to search, may be {@code null}
94489403
* @param value the value to count.
94499404
* @return the number of matches, may be {@code 0} if array is {@code null}
94509405
*/
9451-
public static int countMatches(final char[] array, final char value)
9406+
public static int countMatches(final double[] array, final double value)
94529407
{
94539408
if(array == null){
94549409
return 0;
94559410
}
94569411
int count = 0;
9457-
for(char i : array){
9458-
if(i == value){
9412+
for(double i : array){
9413+
if(Double.compare(i, value) == 0)
9414+
{
94599415
count++;
94609416
}
94619417
}
94629418
return count;
94639419
}
94649420

94659421
/**
9466-
* Counts how many times the specified number or value occures in the given array.
9422+
* Counts how many times the specified number or value occurs in the given array.
94679423
* <p>
94689424
* A {@code null} array returns {@code 0}.
94699425
* </p>

0 commit comments

Comments
 (0)