Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
209 changes: 209 additions & 0 deletions src/main/java/org/apache/commons/lang3/ArrayUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
* A {@code null} array returns {@code 0}.
* </p>
* @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.
* <p>
* A {@code null} array returns {@code 0}.
* </p>
* @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.
* <p>
* A {@code null} array returns {@code 0}.
* </p>
* @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.
* <p>
* A {@code null} array returns {@code 0}.
* </p>
* @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.
* <p>
* A {@code null} array returns {@code 0}.
* </p>
* @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.
* <p>
* A {@code null} array returns {@code 0}.
* </p>
* @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.
* <p>
* A {@code null} array returns {@code 0}.
* </p>
* @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.
* <p>
* A {@code null} array returns {@code 0}.
* </p>
* @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.
* <p>
* A {@code null} array returns {@code 0}.
* </p>
* @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 <T> 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.
Expand Down
81 changes: 81 additions & 0 deletions src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down