Skip to content

Commit 9d5ad94

Browse files
kommalapatiravitejaRaviteja Kommalapati
andauthored
Add fill method for boolean arrays in ArrayFill utility class (#1386)
- Completes the fill methods for all primitive array types Co-authored-by: Raviteja Kommalapati <[email protected]@example.com>
1 parent 73f9923 commit 9d5ad94

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ public static byte[] fill(final byte[] a, final byte val) {
4444
return a;
4545
}
4646

47+
/**
48+
* Fills and returns the given array, assigning the given {@code boolean} value to each element of the array.
49+
*
50+
* @param a the array to be filled (may be null).
51+
* @param val the value to be stored in all elements of the array.
52+
* @return the given array.
53+
* @see Arrays#fill(boolean[],boolean)
54+
*/
55+
public static boolean[] fill(final boolean[] a, final boolean val) {
56+
if (a != null) {
57+
Arrays.fill(a, val);
58+
}
59+
return a;
60+
}
61+
4762
/**
4863
* Fills and returns the given array, assigning the given {@code char} value to each element of the array.
4964
*

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,25 @@ public void testFillByteArrayNull() {
4949
assertSame(array, actual);
5050
}
5151

52+
@Test
53+
public void testFillBooleanArray() {
54+
final boolean[] array = new boolean[3];
55+
final boolean val = true;
56+
final boolean[] actual = ArrayFill.fill(array, val);
57+
assertSame(array, actual);
58+
for (final boolean v : actual) {
59+
assertEquals(val, v);
60+
}
61+
}
62+
63+
@Test
64+
public void testFillBooleanArrayNull() {
65+
final boolean[] array = null;
66+
final boolean val = true;
67+
final boolean[] actual = ArrayFill.fill(array, val);
68+
assertSame(array, actual);
69+
}
70+
5271
@Test
5372
public void testFillCharArray() {
5473
final char[] array = new char[3];

0 commit comments

Comments
 (0)