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