diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index abcb0373de4..5b3c14fc6a0 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -9231,6 +9231,215 @@ 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} + * @since 3.21.0 + */ + 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 occurs 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 int countMatches(final long[] array, final long value) + { + if(array == null){ + return 0; + } + int 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 int countMatches(final float[] array, final float value) + { + if(array == null){ + return 0; + } + int count = 0; + for(float i : array){ + if(Float.compare(i, value) == 0){ + count++; + } + } + return count; + } + + /** + * Counts how many times the specified number or value occurs 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 double[] array, final double value) + { + if(array == null){ + return 0; + } + int count = 0; + for(double i : array){ + if(Double.compare(i, value) == 0) + { + count++; + } + } + return count; + } + + /** + * Counts how many times the specified number or value occurs 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));