Skip to content

Commit 65d49d9

Browse files
tushartushar
authored andcommitted
Add countMatches methods to ArrayUtils for all array types with tests
1 parent 6a4979b commit 65d49d9

File tree

2 files changed

+334
-0
lines changed

2 files changed

+334
-0
lines changed

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

Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9231,6 +9231,259 @@ public static short[] toPrimitive(final Short[] array, final short valueForNull)
92319231
}
92329232
return result;
92339233
}
9234+
9235+
/**
9236+
* Counts how many times the specified number or value occures in the given array.
9237+
* <p>
9238+
* A {@code null} array returns {@code 0}.
9239+
* </p>
9240+
* @param array the array to search, may be {@code null}
9241+
* @param value the value to count.
9242+
* @return the number of matches, may be {@code 0} if array is {@code null}
9243+
*/
9244+
public static int countMatches(final boolean[] array, final boolean value)
9245+
{
9246+
if(array == null){
9247+
return 0;
9248+
}
9249+
int count = 0;
9250+
for(boolean i : array){
9251+
if(i == value){
9252+
count++;
9253+
}
9254+
}
9255+
return count;
9256+
}
9257+
9258+
/**
9259+
* Counts how many times the specified number or value occures in the given array.
9260+
* <p>
9261+
* A {@code null} array returns {@code 0}.
9262+
* </p>
9263+
* @param array the array to search, may be {@code null}
9264+
* @param value the value to count.
9265+
* @return the number of matches, may be {@code 0} if array is {@code null}
9266+
*/
9267+
public static int countMatches(final byte[] array, final byte value)
9268+
{
9269+
if(array == null){
9270+
return 0;
9271+
}
9272+
int count = 0;
9273+
for(byte i : array){
9274+
if(i == value){
9275+
count++;
9276+
}
9277+
}
9278+
return count;
9279+
}
9280+
9281+
/**
9282+
* Counts how many times the specified number or value occures in the given array.
9283+
* <p>
9284+
* A {@code null} array returns {@code 0}.
9285+
* </p>
9286+
* @param array the array to search, may be {@code null}
9287+
* @param value the value to count.
9288+
* @return the number of matches, may be {@code 0} if array is {@code null}
9289+
*/
9290+
public static int countMatches(final char[] array, final char value)
9291+
{
9292+
if(array == null){
9293+
return 0;
9294+
}
9295+
int count = 0;
9296+
for(char i : array){
9297+
if(i == value){
9298+
count++;
9299+
}
9300+
}
9301+
return count;
9302+
}
9303+
9304+
/**
9305+
* Counts how many times the specified number or value occures in the given array.
9306+
* <p>
9307+
* A {@code null} array returns {@code 0}.
9308+
* </p>
9309+
* @param array the array to search, may be {@code null}
9310+
* @param value the value to count.
9311+
* @return the number of matches, may be {@code 0} if array is {@code null}
9312+
*/
9313+
public static int countMatches(final short[] array, final short value)
9314+
{
9315+
if(array == null){
9316+
return 0;
9317+
}
9318+
int count = 0;
9319+
for(short i : array){
9320+
if(i == value){
9321+
count++;
9322+
}
9323+
}
9324+
return count;
9325+
}
9326+
9327+
/**
9328+
* Counts how many times the specified number or value occures in the given array.
9329+
* <p>
9330+
* A {@code null} array returns {@code 0}.
9331+
* </p>
9332+
* @param array the array to search, may be {@code null}
9333+
* @param value the value to count.
9334+
* @return the number of matches, may be {@code 0} if array is {@code null}
9335+
*/
9336+
public static int countMatches(final int[] array, final int value)
9337+
{
9338+
if(array == null){
9339+
return 0;
9340+
}
9341+
int count = 0;
9342+
for(int i : array){
9343+
if(i == value){
9344+
count++;
9345+
}
9346+
}
9347+
return count;
9348+
}
9349+
9350+
/**
9351+
* Counts how many times the specified number or value occures in the given array.
9352+
* <p>
9353+
* A {@code null} array returns {@code 0}.
9354+
* </p>
9355+
* @param array the array to search, may be {@code null}
9356+
* @param value the value to count.
9357+
* @return the number of matches, may be {@code 0} if array is {@code null}
9358+
*/
9359+
public static long countMatches(final long[] array, final long value)
9360+
{
9361+
if(array == null){
9362+
return 0;
9363+
}
9364+
long count = 0;
9365+
for(long i : array){
9366+
if(i == value){
9367+
count++;
9368+
}
9369+
}
9370+
return count;
9371+
}
9372+
9373+
/**
9374+
* Counts how many times the specified number or value occures in the given array.
9375+
* <p>
9376+
* A {@code null} array returns {@code 0}.
9377+
* </p>
9378+
* @param array the array to search, may be {@code null}
9379+
* @param value the value to count.
9380+
* @return the number of matches, may be {@code 0} if array is {@code null}
9381+
*/
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)
9429+
{
9430+
if(array == null){
9431+
return 0;
9432+
}
9433+
int count = 0;
9434+
for(byte i : array){
9435+
if(i == value){
9436+
count++;
9437+
}
9438+
}
9439+
return count;
9440+
}
9441+
9442+
/**
9443+
* Counts how many times the specified number or value occures in the given array.
9444+
* <p>
9445+
* A {@code null} array returns {@code 0}.
9446+
* </p>
9447+
* @param array the array to search, may be {@code null}
9448+
* @param value the value to count.
9449+
* @return the number of matches, may be {@code 0} if array is {@code null}
9450+
*/
9451+
public static int countMatches(final char[] array, final char value)
9452+
{
9453+
if(array == null){
9454+
return 0;
9455+
}
9456+
int count = 0;
9457+
for(char i : array){
9458+
if(i == value){
9459+
count++;
9460+
}
9461+
}
9462+
return count;
9463+
}
9464+
9465+
/**
9466+
* Counts how many times the specified number or value occures in the given array.
9467+
* <p>
9468+
* A {@code null} array returns {@code 0}.
9469+
* </p>
9470+
* @param array the array to search, may be {@code null}
9471+
* @param value the value to count.
9472+
* @return the number of matches, may be {@code 0} if array is {@code null}
9473+
*/
9474+
public static <T> int countMatches(final T[] array, final T value)
9475+
{
9476+
if(array == null){
9477+
return 0;
9478+
}
9479+
int count = 0;
9480+
for(T element : array){
9481+
if(value == null ? element == null : element.equals(value)){
9482+
count++;
9483+
}
9484+
}
9485+
return count;
9486+
}
92349487

92359488
/**
92369489
* Outputs an array as a String, treating {@code null} as an empty array.

src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6830,6 +6830,87 @@ void testToPrimitiveArrayViaObjectArray() {
68306830
assertArrayEquals(new String[] { "a" }, (String[]) ArrayUtils.toPrimitive(new String[] { "a" }));
68316831
}
68326832

6833+
@Test
6834+
void countMatchesBooleanArray()
6835+
{
6836+
/* boolean array tests */
6837+
assertEquals(0, ArrayUtils.countMatches((boolean[])null, true));
6838+
assertEquals(2, ArrayUtils.countMatches(new boolean[]{true, false, true}, true));
6839+
assertEquals(0, ArrayUtils.countMatches(new boolean[]{true,true,true}, false));
6840+
}
6841+
6842+
@Test
6843+
void countMatchesByteArray()
6844+
{
6845+
/* byte array tests */
6846+
assertEquals(0, ArrayUtils.countMatches((byte[])null, (byte)1));
6847+
assertEquals(2, ArrayUtils.countMatches(new byte[]{1,2,2,4,5}, (byte)2));
6848+
assertEquals(0, ArrayUtils.countMatches(new byte[]{1,3,2,4,6,5}, (byte)7));
6849+
}
6850+
6851+
@Test
6852+
void countMatchesCharArray()
6853+
{
6854+
/* char array tests */
6855+
assertEquals(0, ArrayUtils.countMatches((char[])null, 'a'));
6856+
assertEquals(3, ArrayUtils.countMatches(new char[]{'a','e','e','u','e'}, 'e'));
6857+
assertEquals(0, ArrayUtils.countMatches(new char[]{'a','e','e','u','e'}, 'i'));
6858+
}
6859+
6860+
@Test
6861+
void countMatchesShortArray()
6862+
{
6863+
/* short array tests */
6864+
assertEquals(0, ArrayUtils.countMatches((short[])null, (short)5));
6865+
assertEquals(3, ArrayUtils.countMatches(new short[]{1,5,5,2,5,7}, (short)5));
6866+
assertEquals(0, ArrayUtils.countMatches(new short[]{1,2,2,2,3,7}, (short)5));
6867+
}
6868+
6869+
@Test
6870+
void countMatchesIntArray()
6871+
{
6872+
/* int array tests */
6873+
assertEquals(0, ArrayUtils.countMatches((int[])null, 5));
6874+
assertEquals(3, ArrayUtils.countMatches(new int[]{1,5,5,2,5,7}, 5));
6875+
assertEquals(0, ArrayUtils.countMatches(new int[]{1,2,2,2,3,7}, 5));
6876+
}
6877+
6878+
@Test
6879+
void countMatchesLongArray()
6880+
{
6881+
/* long array tests */
6882+
assertEquals(0, ArrayUtils.countMatches((long[])null, 5L));
6883+
assertEquals(3, ArrayUtils.countMatches(new long[]{1L,5L,5L,2L,5L,7L}, 5L));
6884+
assertEquals(0, ArrayUtils.countMatches(new long[]{1L,2L,3L,7L}, 5L));
6885+
}
6886+
6887+
@Test
6888+
void countMatchesFloatArray()
6889+
{
6890+
/* float array tests */
6891+
assertEquals(0, ArrayUtils.countMatches((float[])null, 5f));
6892+
assertEquals(3, ArrayUtils.countMatches(new float[]{1f,5f,5f,2f,5f,7f}, 5f));
6893+
assertEquals(0, ArrayUtils.countMatches(new float[]{1f,2f,2f,2f,3f,7f}, 5f));
6894+
}
6895+
6896+
@Test
6897+
void countMatchesDoubleArray()
6898+
{
6899+
/* double array tests */
6900+
assertEquals(0, ArrayUtils.countMatches((double[])null, 5d));
6901+
assertEquals(2, ArrayUtils.countMatches(new double[]{1d,5.7d,5d,2d,5d,7d}, 5d));
6902+
assertEquals(0, ArrayUtils.countMatches(new double[]{1d,2d,2d,3d,7d}, 5));
6903+
}
6904+
6905+
@Test
6906+
void countMatchesObjectArray()
6907+
{
6908+
/* object array tests */
6909+
assertEquals(0, ArrayUtils.countMatches((String[])null, "a"));
6910+
assertEquals(2, ArrayUtils.countMatches(new String[]{"a","e","i","e"}, "e"));
6911+
assertEquals(0, ArrayUtils.countMatches(new String[]{"a","e","i","e","u"}, "x"));
6912+
}
6913+
68336914
@Test
68346915
void testToString() {
68356916
assertEquals("{}", ArrayUtils.toString(null));

0 commit comments

Comments
 (0)