This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push:
new 7db3e126d Add ArrayFill.clear(char[]|byte[]) (#1667)
7db3e126d is described below
commit 7db3e126dd480698eb523e72e8dc4e5ed7e7a5c7
Author: Gary Gregory <[email protected]>
AuthorDate: Sun May 24 09:06:06 2026 -0400
Add ArrayFill.clear(char[]|byte[]) (#1667)
Javadoc
---
.../java/org/apache/commons/lang3/ArrayFill.java | 74 ++++++++++++++++------
.../org/apache/commons/lang3/ArrayFillTest.java | 36 +++++++++++
2 files changed, 91 insertions(+), 19 deletions(-)
diff --git a/src/main/java/org/apache/commons/lang3/ArrayFill.java
b/src/main/java/org/apache/commons/lang3/ArrayFill.java
index 32c83a290..7b9617e63 100644
--- a/src/main/java/org/apache/commons/lang3/ArrayFill.java
+++ b/src/main/java/org/apache/commons/lang3/ArrayFill.java
@@ -29,11 +29,47 @@
*/
public final class ArrayFill {
+ /**
+ * Fills and returns the given array, assigning {@code '\0'} to each
element of the array.
+ * <p>
+ * Equivalent to:
+ * </p>
+ * <pre>
+ * ArrayFill.fill(a, '\0'); // and not '0'!
+ * </pre>
+ *
+ * @param a the array to fill (may be null).
+ * @return the given array.
+ * @see Arrays#fill(char[],char)
+ * @see ArrayFill#fill(char[],char)
+ * @see CharUtils#NUL
+ * @since 3.21.0
+ */
+ public static char[] clear(final char[] a) {
+ return fill(a, CharUtils.NUL); // and not '0'!
+ }
+
+ /**
+ * Fills and returns the given array, assigning {@code 0} to each element
of the array.
+ * <pre>
+ * ArrayFill.fill(a, (byte) 0);
+ * </pre>
+ *
+ * @param a the array to fill (may be null).
+ * @return the given array.
+ * @see Arrays#fill(byte[],byte)
+ * @see ArrayFill#fill(byte[],byte)
+ * @since 3.21.0
+ */
+ public static byte[] clear(final byte[] a) {
+ return fill(a, (byte) 0);
+ }
+
/**
* 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.
+ * @param a the array to fill (may be null).
+ * @param val the value to store in all elements of the array.
* @return the given array.
* @see Arrays#fill(boolean[],boolean)
* @since 3.18.0
@@ -48,8 +84,8 @@ public static boolean[] fill(final boolean[] a, final boolean
val) {
/**
* Fills and returns the given array, assigning the given {@code byte}
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.
+ * @param a the array to fill (may be null).
+ * @param val the value to store in all elements of the array.
* @return the given array.
* @see Arrays#fill(byte[],byte)
*/
@@ -63,8 +99,8 @@ public static byte[] fill(final byte[] a, final byte val) {
/**
* Fills and returns the given array, assigning the given {@code char}
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.
+ * @param a the array to fill (may be null).
+ * @param val the value to store in all elements of the array.
* @return the given array.
* @see Arrays#fill(char[],char)
*/
@@ -78,8 +114,8 @@ public static char[] fill(final char[] a, final char val) {
/**
* Fills and returns the given array, assigning the given {@code double}
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.
+ * @param a the array to fill (may be null).
+ * @param val the value to store in all elements of the array.
* @return the given array.
* @see Arrays#fill(double[],double)
*/
@@ -93,8 +129,8 @@ public static double[] fill(final double[] a, final double
val) {
/**
* Fills and returns the given array, assigning the given {@code float}
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.
+ * @param a the array to fill (may be null).
+ * @param val the value to store in all elements of the array.
* @return the given array.
* @see Arrays#fill(float[],float)
*/
@@ -108,8 +144,8 @@ public static float[] fill(final float[] a, final float
val) {
/**
* Fills and returns the given array, assigning the given {@code int}
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.
+ * @param a the array to fill (may be null).
+ * @param val the value to store in all elements of the array.
* @return the given array.
* @see Arrays#fill(int[],int)
*/
@@ -123,8 +159,8 @@ public static int[] fill(final int[] a, final int val) {
/**
* Fills and returns the given array, assigning the given {@code long}
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.
+ * @param a the array to fill (may be null).
+ * @param val the value to store in all elements of the array.
* @return the given array.
* @see Arrays#fill(long[],long)
*/
@@ -138,8 +174,8 @@ public static long[] fill(final long[] a, final long val) {
/**
* Fills and returns the given array, assigning the given {@code short}
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.
+ * @param a the array to fill (may be null).
+ * @param val the value to store in all elements of the array.
* @return the given array.
* @see Arrays#fill(short[],short)
*/
@@ -161,7 +197,7 @@ public static short[] fill(final short[] a, final short
val) {
* </p>
*
* @param <T> type of elements of the array.
- * @param array array to be filled (may be null).
+ * @param array array to fill (may be null).
* @param generator a function accepting an index and producing the
desired value for that position.
* @return the input array.
* @param <E> The kind of thrown exception or error.
@@ -182,8 +218,8 @@ public static <T, E extends Throwable> T[] fill(final T[]
array, final FailableI
* Fills and returns the given array, assigning the given {@code T} value
to each element of the array.
*
* @param <T> the array type.
- * @param a the array to be filled (may be null).
- * @param val the value to be stored in all elements of the array.
+ * @param a the array to fill (may be null).
+ * @param val the value to store in all elements of the array.
* @return the given array.
* @see Arrays#fill(Object[],Object)
*/
diff --git a/src/test/java/org/apache/commons/lang3/ArrayFillTest.java
b/src/test/java/org/apache/commons/lang3/ArrayFillTest.java
index 34cc4b9df..26e738d80 100644
--- a/src/test/java/org/apache/commons/lang3/ArrayFillTest.java
+++ b/src/test/java/org/apache/commons/lang3/ArrayFillTest.java
@@ -30,6 +30,42 @@
*/
class ArrayFillTest extends AbstractLangTest {
+ @Test
+ void testClearByteArray() {
+ final byte[] array = new byte[3];
+ final byte val = 0;
+ final byte[] actual = ArrayFill.clear(array);
+ assertSame(array, actual);
+ for (final byte v : actual) {
+ assertEquals(val, v);
+ }
+ }
+
+ @Test
+ void testClearByteArrayNull() {
+ final byte[] array = null;
+ final byte[] actual = ArrayFill.clear(array);
+ assertSame(array, actual);
+ }
+
+ @Test
+ void testClearCharArray() {
+ final char[] array = new char[3];
+ final char val = 0;
+ final char[] actual = ArrayFill.clear(array);
+ assertSame(array, actual);
+ for (final char v : actual) {
+ assertEquals(val, v);
+ }
+ }
+
+ @Test
+ void testClearCharArrayNull() {
+ final char[] array = null;
+ final char[] actual = ArrayFill.clear(array);
+ assertSame(array, actual);
+ }
+
@Test
void testFillBooleanArray() {
final boolean[] array = new boolean[3];