From 65d49d9d7855368b2b9d92236b01aad034a4ca67 Mon Sep 17 00:00:00 2001 From: tushar Date: Fri, 26 Dec 2025 21:55:52 +0530 Subject: [PATCH 1/2] Add countMatches methods to ArrayUtils for all array types with tests --- .../org/apache/commons/lang3/ArrayUtils.java | 253 ++++++++++++++++++ .../apache/commons/lang3/ArrayUtilsTest.java | 81 ++++++ 2 files changed, 334 insertions(+) diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index abcb0373de4..2db2a262ee0 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -9231,6 +9231,259 @@ public static short[] toPrimitive(final Short[] array, final short valueForNull) } return result; } + + /** + * Counts how many times the specified number or value occures in the given array. + *

+ * A {@code null} array returns {@code 0}. + *

+ * @param array the array to search, may be {@code null} + * @param value the value to count. + * @return the number of matches, may be {@code 0} if array is {@code null} + */ + public static int countMatches(final boolean[] array, final boolean value) + { + if(array == null){ + return 0; + } + int count = 0; + for(boolean i : array){ + if(i == value){ + count++; + } + } + return count; + } + + /** + * Counts how many times the specified number or value occures in the given array. + *

+ * A {@code null} array returns {@code 0}. + *

+ * @param array the array to search, may be {@code null} + * @param value the value to count. + * @return the number of matches, may be {@code 0} if array is {@code null} + */ + public static int countMatches(final byte[] array, final byte value) + { + if(array == null){ + return 0; + } + int count = 0; + for(byte i : array){ + if(i == value){ + count++; + } + } + return count; + } + + /** + * Counts how many times the specified number or value occures in the given array. + *

+ * A {@code null} array returns {@code 0}. + *

+ * @param array the array to search, may be {@code null} + * @param value the value to count. + * @return the number of matches, may be {@code 0} if array is {@code null} + */ + public static int countMatches(final char[] array, final char value) + { + if(array == null){ + return 0; + } + int count = 0; + for(char i : array){ + if(i == value){ + count++; + } + } + return count; + } + + /** + * Counts how many times the specified number or value occures in the given array. + *

+ * A {@code null} array returns {@code 0}. + *

+ * @param array the array to search, may be {@code null} + * @param value the value to count. + * @return the number of matches, may be {@code 0} if array is {@code null} + */ + public static int countMatches(final short[] array, final short value) + { + if(array == null){ + return 0; + } + int count = 0; + for(short i : array){ + if(i == value){ + count++; + } + } + return count; + } + + /** + * Counts how many times the specified number or value occures in the given array. + *

+ * A {@code null} array returns {@code 0}. + *

+ * @param array the array to search, may be {@code null} + * @param value the value to count. + * @return the number of matches, may be {@code 0} if array is {@code null} + */ + public static int countMatches(final int[] array, final int value) + { + if(array == null){ + return 0; + } + int count = 0; + for(int i : array){ + if(i == value){ + count++; + } + } + return count; + } + + /** + * Counts how many times the specified number or value occures in the given array. + *

+ * A {@code null} array returns {@code 0}. + *

+ * @param array the array to search, may be {@code null} + * @param value the value to count. + * @return the number of matches, may be {@code 0} if array is {@code null} + */ + public static long countMatches(final long[] array, final long value) + { + if(array == null){ + return 0; + } + long count = 0; + for(long i : array){ + if(i == value){ + count++; + } + } + return count; + } + + /** + * Counts how many times the specified number or value occures in the given array. + *

+ * A {@code null} array returns {@code 0}. + *

+ * @param array the array to search, may be {@code null} + * @param value the value to count. + * @return the number of matches, may be {@code 0} if array is {@code null} + */ + public static long countMatches(final float[] array, final float value) + { + if(array == null){ + return 0; + } + long count = 0; + for(float i : array){ + if(i == value){ + count++; + } + } + return count; + } + + /** + * Counts how many times the specified number or value occures in the given array. + *

+ * A {@code null} array returns {@code 0}. + *

+ * @param array the array to search, may be {@code null} + * @param value the value to count. + * @return the number of matches, may be {@code 0} if array is {@code null} + */ + public static long countMatches(final double[] array, final double value) + { + if(array == null){ + return 0; + } + long count = 0; + for(double i : array){ + if(i == value){ + count++; + } + } + return count; + } + + /** + * Counts how many times the specified number or value occures in the given array. + *

+ * A {@code null} array returns {@code 0}. + *

+ * @param array the array to search, may be {@code null} + * @param value the value to count. + * @return the number of matches, may be {@code 0} if array is {@code null} + */ + public static int countMatches(final byte[] array, final byte value) + { + if(array == null){ + return 0; + } + int count = 0; + for(byte i : array){ + if(i == value){ + count++; + } + } + return count; + } + + /** + * Counts how many times the specified number or value occures in the given array. + *

+ * A {@code null} array returns {@code 0}. + *

+ * @param array the array to search, may be {@code null} + * @param value the value to count. + * @return the number of matches, may be {@code 0} if array is {@code null} + */ + public static int countMatches(final char[] array, final char value) + { + if(array == null){ + return 0; + } + int count = 0; + for(char i : array){ + if(i == value){ + count++; + } + } + return count; + } + + /** + * Counts how many times the specified number or value occures in the given array. + *

+ * A {@code null} array returns {@code 0}. + *

+ * @param array the array to search, may be {@code null} + * @param value the value to count. + * @return the number of matches, may be {@code 0} if array is {@code null} + */ + public static int countMatches(final T[] array, final T value) + { + if(array == null){ + return 0; + } + int count = 0; + for(T element : array){ + if(value == null ? element == null : element.equals(value)){ + count++; + } + } + return count; + } /** * Outputs an array as a String, treating {@code null} as an empty array. diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java index edd4af9b13f..15ac3175d1e 100644 --- a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java @@ -6830,6 +6830,87 @@ void testToPrimitiveArrayViaObjectArray() { assertArrayEquals(new String[] { "a" }, (String[]) ArrayUtils.toPrimitive(new String[] { "a" })); } + @Test + void countMatchesBooleanArray() + { + /* boolean array tests */ + assertEquals(0, ArrayUtils.countMatches((boolean[])null, true)); + assertEquals(2, ArrayUtils.countMatches(new boolean[]{true, false, true}, true)); + assertEquals(0, ArrayUtils.countMatches(new boolean[]{true,true,true}, false)); + } + + @Test + void countMatchesByteArray() + { + /* byte array tests */ + assertEquals(0, ArrayUtils.countMatches((byte[])null, (byte)1)); + assertEquals(2, ArrayUtils.countMatches(new byte[]{1,2,2,4,5}, (byte)2)); + assertEquals(0, ArrayUtils.countMatches(new byte[]{1,3,2,4,6,5}, (byte)7)); + } + + @Test + void countMatchesCharArray() + { + /* char array tests */ + assertEquals(0, ArrayUtils.countMatches((char[])null, 'a')); + assertEquals(3, ArrayUtils.countMatches(new char[]{'a','e','e','u','e'}, 'e')); + assertEquals(0, ArrayUtils.countMatches(new char[]{'a','e','e','u','e'}, 'i')); + } + + @Test + void countMatchesShortArray() + { + /* short array tests */ + assertEquals(0, ArrayUtils.countMatches((short[])null, (short)5)); + assertEquals(3, ArrayUtils.countMatches(new short[]{1,5,5,2,5,7}, (short)5)); + assertEquals(0, ArrayUtils.countMatches(new short[]{1,2,2,2,3,7}, (short)5)); + } + + @Test + void countMatchesIntArray() + { + /* int array tests */ + assertEquals(0, ArrayUtils.countMatches((int[])null, 5)); + assertEquals(3, ArrayUtils.countMatches(new int[]{1,5,5,2,5,7}, 5)); + assertEquals(0, ArrayUtils.countMatches(new int[]{1,2,2,2,3,7}, 5)); + } + + @Test + void countMatchesLongArray() + { + /* long array tests */ + assertEquals(0, ArrayUtils.countMatches((long[])null, 5L)); + assertEquals(3, ArrayUtils.countMatches(new long[]{1L,5L,5L,2L,5L,7L}, 5L)); + assertEquals(0, ArrayUtils.countMatches(new long[]{1L,2L,3L,7L}, 5L)); + } + + @Test + void countMatchesFloatArray() + { + /* float array tests */ + assertEquals(0, ArrayUtils.countMatches((float[])null, 5f)); + assertEquals(3, ArrayUtils.countMatches(new float[]{1f,5f,5f,2f,5f,7f}, 5f)); + assertEquals(0, ArrayUtils.countMatches(new float[]{1f,2f,2f,2f,3f,7f}, 5f)); + } + + @Test + void countMatchesDoubleArray() + { + /* double array tests */ + assertEquals(0, ArrayUtils.countMatches((double[])null, 5d)); + assertEquals(2, ArrayUtils.countMatches(new double[]{1d,5.7d,5d,2d,5d,7d}, 5d)); + assertEquals(0, ArrayUtils.countMatches(new double[]{1d,2d,2d,3d,7d}, 5)); + } + + @Test + void countMatchesObjectArray() + { + /* object array tests */ + assertEquals(0, ArrayUtils.countMatches((String[])null, "a")); + assertEquals(2, ArrayUtils.countMatches(new String[]{"a","e","i","e"}, "e")); + assertEquals(0, ArrayUtils.countMatches(new String[]{"a","e","i","e","u"}, "x")); + } + @Test void testToString() { assertEquals("{}", ArrayUtils.toString(null)); From 833ac0cdbeafb52e31b8787092a2df020aab794d Mon Sep 17 00:00:00 2001 From: tushar Date: Fri, 26 Dec 2025 22:27:37 +0530 Subject: [PATCH 2/2] Fix countMatches for double[] using Double.compare for proper IEEE 754 handling --- .../org/apache/commons/lang3/ArrayUtils.java | 70 ++++--------------- 1 file changed, 13 insertions(+), 57 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index 2db2a262ee0..5b3c14fc6a0 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -9240,6 +9240,7 @@ public static short[] toPrimitive(final Short[] array, final short valueForNull) * @param array the array to search, may be {@code null} * @param value the value to count. * @return the number of matches, may be {@code 0} if array is {@code null} + * @since 3.21.0 */ public static int countMatches(final boolean[] array, final boolean value) { @@ -9302,7 +9303,7 @@ public static int countMatches(final char[] array, final char value) } /** - * Counts how many times the specified number or value occures in the given array. + * Counts how many times the specified number or value occurs in the given array. *

* A {@code null} array returns {@code 0}. *

@@ -9356,12 +9357,12 @@ public static int countMatches(final int[] array, final int value) * @param value the value to count. * @return the number of matches, may be {@code 0} if array is {@code null} */ - public static long countMatches(final long[] array, final long value) + public static int countMatches(final long[] array, final long value) { if(array == null){ return 0; } - long count = 0; + int count = 0; for(long i : array){ if(i == value){ count++; @@ -9379,60 +9380,14 @@ public static long countMatches(final long[] array, final long value) * @param value the value to count. * @return the number of matches, may be {@code 0} if array is {@code null} */ - public static long countMatches(final float[] array, final float value) - { - if(array == null){ - return 0; - } - long count = 0; - for(float i : array){ - if(i == value){ - count++; - } - } - return count; - } - - /** - * Counts how many times the specified number or value occures in the given array. - *

- * A {@code null} array returns {@code 0}. - *

- * @param array the array to search, may be {@code null} - * @param value the value to count. - * @return the number of matches, may be {@code 0} if array is {@code null} - */ - public static long countMatches(final double[] array, final double value) - { - if(array == null){ - return 0; - } - long count = 0; - for(double i : array){ - if(i == value){ - count++; - } - } - return count; - } - - /** - * Counts how many times the specified number or value occures in the given array. - *

- * A {@code null} array returns {@code 0}. - *

- * @param array the array to search, may be {@code null} - * @param value the value to count. - * @return the number of matches, may be {@code 0} if array is {@code null} - */ - public static int countMatches(final byte[] array, final byte value) + public static int countMatches(final float[] array, final float value) { if(array == null){ return 0; } int count = 0; - for(byte i : array){ - if(i == value){ + for(float i : array){ + if(Float.compare(i, value) == 0){ count++; } } @@ -9440,7 +9395,7 @@ public static int countMatches(final byte[] array, final byte value) } /** - * Counts how many times the specified number or value occures in the given array. + * Counts how many times the specified number or value occurs in the given array. *

* A {@code null} array returns {@code 0}. *

@@ -9448,14 +9403,15 @@ public static int countMatches(final byte[] array, final byte value) * @param value the value to count. * @return the number of matches, may be {@code 0} if array is {@code null} */ - public static int countMatches(final char[] array, final char value) + public static int countMatches(final double[] array, final double value) { if(array == null){ return 0; } int count = 0; - for(char i : array){ - if(i == value){ + for(double i : array){ + if(Double.compare(i, value) == 0) + { count++; } } @@ -9463,7 +9419,7 @@ public static int countMatches(final char[] array, final char value) } /** - * Counts how many times the specified number or value occures in the given array. + * Counts how many times the specified number or value occurs in the given array. *

* A {@code null} array returns {@code 0}. *