Skip to content

Add fill method for boolean arrays in ArrayFill utility class #1386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2025
Merged
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
15 changes: 15 additions & 0 deletions src/main/java/org/apache/commons/lang3/ArrayFill.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ public static byte[] fill(final byte[] a, final byte val) {
return a;
}

/**
* Fills and returns the given array, assigning the given {@code boolean} value to each element of the array.
*
* @param a the array to be filled (may be null).
* @param val the value to be stored in all elements of the array.
* @return the given array.
* @see Arrays#fill(boolean[],boolean)
*/
public static boolean[] fill(final boolean[] a, final boolean val) {
if (a != null) {
Arrays.fill(a, val);
}
return a;
}

/**
* Fills and returns the given array, assigning the given {@code char} value to each element of the array.
*
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/org/apache/commons/lang3/ArrayFillTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,25 @@ public void testFillByteArrayNull() {
assertSame(array, actual);
}

@Test
public void testFillBooleanArray() {
final boolean[] array = new boolean[3];
final boolean val = true;
final boolean[] actual = ArrayFill.fill(array, val);
assertSame(array, actual);
for (final boolean v : actual) {
assertEquals(val, v);
}
}

@Test
public void testFillBooleanArrayNull() {
final boolean[] array = null;
final boolean val = true;
final boolean[] actual = ArrayFill.fill(array, val);
assertSame(array, actual);
}

@Test
public void testFillCharArray() {
final char[] array = new char[3];
Expand Down
Loading