This is an automated email from the ASF dual-hosted git repository.
ggregory 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 dc6ff3457 Add ArrayFill.fill(T[], FailableIntFunction))
dc6ff3457 is described below
commit dc6ff345793c9c2d2d26919722c3eb79ffbab689
Author: Gary Gregory <[email protected]>
AuthorDate: Mon Mar 24 15:15:50 2025 -0400
Add ArrayFill.fill(T[], FailableIntFunction))
---
src/changes/changes.xml | 1 +
.../java/org/apache/commons/lang3/ArrayFill.java | 29 ++++++++++++++++++++++
.../java/org/apache/commons/lang3/ArrayUtils.java | 6 +----
.../org/apache/commons/lang3/ArrayFillTest.java | 18 ++++++++++++++
4 files changed, 49 insertions(+), 5 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 06bfd8802..e7aace5b2 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -110,6 +110,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary
Gregory">Add SystemUtils.getJavaHomePath().</action>
<action type="add" dev="ggregory" due-to="Gary
Gregory">Add SystemUtils.getUserDirPath().</action>
<action type="add" dev="ggregory" due-to="Gary
Gregory">Add SystemUtils.getUserHomePath().</action>
+ <action type="add" dev="ggregory" due-to="Gary
Gregory">Add ArrayFill.fill(T[], FailableIntFunction)).</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary
Gregory, Dependabot">Bump org.apache.commons:commons-parent from 73 to 81
#1267, #1277, #1283, #1288, #1302.</action>
<action type="update" dev="ggregory" due-to="Gary
Gregory, Dependabot">[site] Bump org.codehaus.mojo:taglist-maven-plugin from
3.1.0 to 3.2.1 #1300.</action>
diff --git a/src/main/java/org/apache/commons/lang3/ArrayFill.java
b/src/main/java/org/apache/commons/lang3/ArrayFill.java
index 2d2acd5a4..cf2a58b33 100644
--- a/src/main/java/org/apache/commons/lang3/ArrayFill.java
+++ b/src/main/java/org/apache/commons/lang3/ArrayFill.java
@@ -18,6 +18,9 @@
package org.apache.commons.lang3;
import java.util.Arrays;
+import java.util.function.IntFunction;
+
+import org.apache.commons.lang3.function.FailableIntFunction;
/**
* Fills and returns arrays in the fluent style.
@@ -131,6 +134,32 @@ public static short[] fill(final short[] a, final short
val) {
return a;
}
+ /**
+ * Fills and returns the given array, using the provided generator
supplier to compute each element. Like
+ * {@link Arrays#setAll(Object[], IntFunction)} with exception support.
+ * <p>
+ * If the generator supplier throws an exception, it is relayed to the
caller and the array is left in an indeterminate
+ * state.
+ * </p>
+ *
+ * @param <T> type of elements of the array.
+ * @param array array to be initialized.
+ * @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.
+ * @throws E Thrown by the given {@code generator}.
+ * @see Arrays#setAll(Object[], IntFunction)
+ * @since 3.18.0
+ */
+ public static <T, E extends Throwable> T[] fill(final T[] array, final
FailableIntFunction<? extends T, E> generator) throws E {
+ if (array != null && generator != null) {
+ for (int i = 0; i < array.length; i++) {
+ array[i] = generator.apply(i);
+ }
+ }
+ return array;
+ }
+
/**
* Fills and returns the given array, assigning the given {@code T} value
to each element of the array.
*
diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java
b/src/main/java/org/apache/commons/lang3/ArrayUtils.java
index 3e1374ad6..f26ed4f2f 100644
--- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java
@@ -4290,11 +4290,7 @@ public static int lastIndexOf(final short[] array, final
short valueToFind, int
*/
private static <T, R, E extends Throwable> R[] map(final T[] array, final
Class<R> componentType, final FailableFunction<? super T, ? extends R, E>
mapper)
throws E {
- final R[] newArray = newInstance(componentType, array.length);
- for (int i = 0; i < newArray.length; i++) {
- newArray[i] = mapper.apply(array[i]);
- }
- return newArray;
+ return ArrayFill.fill(newInstance(componentType, array.length), i ->
mapper.apply(array[i]));
}
private static int max0(final int other) {
diff --git a/src/test/java/org/apache/commons/lang3/ArrayFillTest.java
b/src/test/java/org/apache/commons/lang3/ArrayFillTest.java
index 46f1fe77d..c1deb4384 100644
--- a/src/test/java/org/apache/commons/lang3/ArrayFillTest.java
+++ b/src/test/java/org/apache/commons/lang3/ArrayFillTest.java
@@ -17,9 +17,12 @@
package org.apache.commons.lang3;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
+import org.apache.commons.lang3.function.FailableIntFunction;
import org.junit.jupiter.api.Test;
/**
@@ -178,4 +181,19 @@ public void testFillShortArrayNull() {
final short[] actual = ArrayFill.fill(array, val);
assertSame(array, actual);
}
+
+ @Test
+ public void testFillFunction() throws Exception {
+ final FailableIntFunction<?, Exception> nullIntFunction = null;
+ assertNull(ArrayFill.fill(null, nullIntFunction));
+ assertArrayEquals(null, ArrayFill.fill(null, nullIntFunction));
+ assertArrayEquals(ArrayUtils.EMPTY_BOOLEAN_OBJECT_ARRAY,
ArrayFill.fill(ArrayUtils.EMPTY_BOOLEAN_OBJECT_ARRAY, nullIntFunction));
+ assertArrayEquals(ArrayUtils.EMPTY_OBJECT_ARRAY,
ArrayFill.fill(ArrayUtils.EMPTY_OBJECT_ARRAY, nullIntFunction));
+ final Integer[] array = new Integer[10];
+ final Integer[] array2 = ArrayFill.fill(array, Integer::valueOf);
+ assertSame(array, array2);
+ for (int i = 0; i < array.length; i++) {
+ assertEquals(i, array[i].intValue());
+ }
+ }
}