Skip to content

Commit 17ba8b9

Browse files
committed
Make LangCollectors.collect(...) null-safe
1 parent 50a4eea commit 17ba8b9

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ The <action> type attribute can be add,update,fix,remove.
6868
<action type="fix" dev="ggregory" due-to="Gary Gregory">Make Failable.run(FailableRunnable) null-safe.</action>
6969
<action type="fix" dev="ggregory" due-to="Gary Gregory">Make Failable.accept(*) null-safe.</action>
7070
<action type="fix" dev="ggregory" due-to="maxxedev, Piotr P. Karwasz, Gary Gregory">Improve container detection by mimicking systemd #1323.</action>
71+
<action type="fix" dev="ggregory" due-to="Gary Gregory">Make LangCollectors.collect(...) null-safe.</action>
7172
<!-- ADD -->
7273
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Strings and refactor StringUtils.</action>
7374
<action issue="LANG-1747" type="add" dev="ggregory" due-to="Oliver B. Fischer, Gary Gregory">Add StopWatch.run([Failable]Runnable) and get([Failable]Supplier).</action>

src/main/java/org/apache/commons/lang3/stream/LangCollectors.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,16 @@ public Supplier<A> supplier() {
100100
* @param <R> the type of the result.
101101
* @param <A> the intermediate accumulation type of the {@code Collector}.
102102
* @param collector the {@code Collector} describing the reduction.
103-
* @param array The array, assumed to be unmodified during use.
103+
* @param array The array, assumed to be unmodified during use, a null array treated as an empty array.
104104
* @return the result of the reduction
105105
* @see Stream#collect(Collector)
106106
* @see Arrays#stream(Object[])
107107
* @see Collectors
108108
* @since 3.16.0
109109
*/
110+
@SafeVarargs
110111
public static <T, R, A> R collect(final Collector<? super T, A, R> collector, final T... array) {
111-
return Arrays.stream(array).collect(collector);
112+
return Streams.of(array).collect(collector);
112113
}
113114

114115
/**

src/test/java/org/apache/commons/lang3/stream/LangCollectorsTest.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private String join4(final Object... objects) {
7474
return LangCollectors.collect(JOINING_4, objects);
7575
}
7676

77-
private String join4Nul(final Object... objects) {
77+
private String join4NullToString(final Object... objects) {
7878
return LangCollectors.collect(JOINING_4_NUL, objects);
7979
}
8080

@@ -127,11 +127,19 @@ public void testJoinCollectNonStrings4Args() {
127127
assertEquals("<1-2>", join4(_1L, _2L));
128128
assertEquals("<1-2-3>", join4(_1L, _2L, _3L));
129129
assertEquals("<1-null-3>", join4(_1L, null, _3L));
130-
assertEquals("<1-NUL-3>", join4Nul(_1L, null, _3L));
130+
assertEquals("<1-NUL-3>", join4NullToString(_1L, null, _3L));
131131
assertEquals("<1-2>", join4(new AtomicLong(1), new AtomicLong(2)));
132132
assertEquals("<1-2>", join4(new Fixture(1), new Fixture(2)));
133133
}
134134

135+
@Test
136+
public void testJoinCollectNullArgs() {
137+
assertEquals("", join0((Object[]) null));
138+
assertEquals("", join1((Object[]) null));
139+
assertEquals("<>", join3((Object[]) null));
140+
assertEquals("<>", join4NullToString((Object[]) null));
141+
}
142+
135143
@Test
136144
public void testJoinCollectStrings0Arg() {
137145
assertEquals("", join0());
@@ -157,7 +165,7 @@ public void testJoinCollectStrings4Args() {
157165
assertEquals("<1-2>", join4("1", "2"));
158166
assertEquals("<1-2-3>", join4("1", "2", "3"));
159167
assertEquals("<1-null-3>", join4("1", null, "3"));
160-
assertEquals("<1-NUL-3>", join4Nul("1", null, "3"));
168+
assertEquals("<1-NUL-3>", join4NullToString("1", null, "3"));
161169
}
162170

163171
@Test

0 commit comments

Comments
 (0)