Skip to content

Commit dc6ff34

Browse files
committed
Add ArrayFill.fill(T[], FailableIntFunction))
1 parent 248ec2e commit dc6ff34

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

Diff for: src/changes/changes.xml

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ The <action> type attribute can be add,update,fix,remove.
110110
<action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemUtils.getJavaHomePath().</action>
111111
<action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemUtils.getUserDirPath().</action>
112112
<action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemUtils.getUserHomePath().</action>
113+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add ArrayFill.fill(T[], FailableIntFunction)).</action>
113114
<!-- UPDATE -->
114115
<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>
115116
<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 for: src/main/java/org/apache/commons/lang3/ArrayFill.java

+29
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
package org.apache.commons.lang3;
1919

2020
import java.util.Arrays;
21+
import java.util.function.IntFunction;
22+
23+
import org.apache.commons.lang3.function.FailableIntFunction;
2124

2225
/**
2326
* Fills and returns arrays in the fluent style.
@@ -131,6 +134,32 @@ public static short[] fill(final short[] a, final short val) {
131134
return a;
132135
}
133136

137+
/**
138+
* Fills and returns the given array, using the provided generator supplier to compute each element. Like
139+
* {@link Arrays#setAll(Object[], IntFunction)} with exception support.
140+
* <p>
141+
* If the generator supplier throws an exception, it is relayed to the caller and the array is left in an indeterminate
142+
* state.
143+
* </p>
144+
*
145+
* @param <T> type of elements of the array.
146+
* @param array array to be initialized.
147+
* @param generator a function accepting an index and producing the desired value for that position.
148+
* @return the input array
149+
* @param <E> The kind of thrown exception or error.
150+
* @throws E Thrown by the given {@code generator}.
151+
* @see Arrays#setAll(Object[], IntFunction)
152+
* @since 3.18.0
153+
*/
154+
public static <T, E extends Throwable> T[] fill(final T[] array, final FailableIntFunction<? extends T, E> generator) throws E {
155+
if (array != null && generator != null) {
156+
for (int i = 0; i < array.length; i++) {
157+
array[i] = generator.apply(i);
158+
}
159+
}
160+
return array;
161+
}
162+
134163
/**
135164
* Fills and returns the given array, assigning the given {@code T} value to each element of the array.
136165
*

Diff for: src/main/java/org/apache/commons/lang3/ArrayUtils.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -4290,11 +4290,7 @@ public static int lastIndexOf(final short[] array, final short valueToFind, int
42904290
*/
42914291
private static <T, R, E extends Throwable> R[] map(final T[] array, final Class<R> componentType, final FailableFunction<? super T, ? extends R, E> mapper)
42924292
throws E {
4293-
final R[] newArray = newInstance(componentType, array.length);
4294-
for (int i = 0; i < newArray.length; i++) {
4295-
newArray[i] = mapper.apply(array[i]);
4296-
}
4297-
return newArray;
4293+
return ArrayFill.fill(newInstance(componentType, array.length), i -> mapper.apply(array[i]));
42984294
}
42994295

43004296
private static int max0(final int other) {

Diff for: src/test/java/org/apache/commons/lang3/ArrayFillTest.java

+18
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717

1818
package org.apache.commons.lang3;
1919

20+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2021
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertNull;
2123
import static org.junit.jupiter.api.Assertions.assertSame;
2224

25+
import org.apache.commons.lang3.function.FailableIntFunction;
2326
import org.junit.jupiter.api.Test;
2427

2528
/**
@@ -178,4 +181,19 @@ public void testFillShortArrayNull() {
178181
final short[] actual = ArrayFill.fill(array, val);
179182
assertSame(array, actual);
180183
}
184+
185+
@Test
186+
public void testFillFunction() throws Exception {
187+
final FailableIntFunction<?, Exception> nullIntFunction = null;
188+
assertNull(ArrayFill.fill(null, nullIntFunction));
189+
assertArrayEquals(null, ArrayFill.fill(null, nullIntFunction));
190+
assertArrayEquals(ArrayUtils.EMPTY_BOOLEAN_OBJECT_ARRAY, ArrayFill.fill(ArrayUtils.EMPTY_BOOLEAN_OBJECT_ARRAY, nullIntFunction));
191+
assertArrayEquals(ArrayUtils.EMPTY_OBJECT_ARRAY, ArrayFill.fill(ArrayUtils.EMPTY_OBJECT_ARRAY, nullIntFunction));
192+
final Integer[] array = new Integer[10];
193+
final Integer[] array2 = ArrayFill.fill(array, Integer::valueOf);
194+
assertSame(array, array2);
195+
for (int i = 0; i < array.length; i++) {
196+
assertEquals(i, array[i].intValue());
197+
}
198+
}
181199
}

0 commit comments

Comments
 (0)