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 17ba8b9da Make LangCollectors.collect(...) null-safe
17ba8b9da is described below
commit 17ba8b9da6a87791446917250b3d92b6925cbff0
Author: Gary Gregory <[email protected]>
AuthorDate: Mon Dec 2 10:49:51 2024 -0500
Make LangCollectors.collect(...) null-safe
---
src/changes/changes.xml | 1 +
.../org/apache/commons/lang3/stream/LangCollectors.java | 5 +++--
.../apache/commons/lang3/stream/LangCollectorsTest.java | 14 +++++++++++---
3 files changed, 15 insertions(+), 5 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 5e47db24e..a5ab67783 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -68,6 +68,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="fix" dev="ggregory" due-to="Gary
Gregory">Make Failable.run(FailableRunnable) null-safe.</action>
<action type="fix" dev="ggregory" due-to="Gary
Gregory">Make Failable.accept(*) null-safe.</action>
<action type="fix" dev="ggregory" due-to="maxxedev,
Piotr P. Karwasz, Gary Gregory">Improve container detection by mimicking
systemd #1323.</action>
+ <action type="fix" dev="ggregory" due-to="Gary
Gregory">Make LangCollectors.collect(...) null-safe.</action>
<!-- ADD -->
<action type="add" dev="ggregory" due-to="Gary
Gregory">Add Strings and refactor StringUtils.</action>
<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>
diff --git a/src/main/java/org/apache/commons/lang3/stream/LangCollectors.java
b/src/main/java/org/apache/commons/lang3/stream/LangCollectors.java
index 28af06992..ac4e75fee 100644
--- a/src/main/java/org/apache/commons/lang3/stream/LangCollectors.java
+++ b/src/main/java/org/apache/commons/lang3/stream/LangCollectors.java
@@ -100,15 +100,16 @@ public final class LangCollectors {
* @param <R> the type of the result.
* @param <A> the intermediate accumulation type of the {@code
Collector}.
* @param collector the {@code Collector} describing the reduction.
- * @param array The array, assumed to be unmodified during use.
+ * @param array The array, assumed to be unmodified during use, a null
array treated as an empty array.
* @return the result of the reduction
* @see Stream#collect(Collector)
* @see Arrays#stream(Object[])
* @see Collectors
* @since 3.16.0
*/
+ @SafeVarargs
public static <T, R, A> R collect(final Collector<? super T, A, R>
collector, final T... array) {
- return Arrays.stream(array).collect(collector);
+ return Streams.of(array).collect(collector);
}
/**
diff --git
a/src/test/java/org/apache/commons/lang3/stream/LangCollectorsTest.java
b/src/test/java/org/apache/commons/lang3/stream/LangCollectorsTest.java
index 486963d8e..5c39cd65c 100644
--- a/src/test/java/org/apache/commons/lang3/stream/LangCollectorsTest.java
+++ b/src/test/java/org/apache/commons/lang3/stream/LangCollectorsTest.java
@@ -74,7 +74,7 @@ public class LangCollectorsTest {
return LangCollectors.collect(JOINING_4, objects);
}
- private String join4Nul(final Object... objects) {
+ private String join4NullToString(final Object... objects) {
return LangCollectors.collect(JOINING_4_NUL, objects);
}
@@ -127,11 +127,19 @@ public class LangCollectorsTest {
assertEquals("<1-2>", join4(_1L, _2L));
assertEquals("<1-2-3>", join4(_1L, _2L, _3L));
assertEquals("<1-null-3>", join4(_1L, null, _3L));
- assertEquals("<1-NUL-3>", join4Nul(_1L, null, _3L));
+ assertEquals("<1-NUL-3>", join4NullToString(_1L, null, _3L));
assertEquals("<1-2>", join4(new AtomicLong(1), new AtomicLong(2)));
assertEquals("<1-2>", join4(new Fixture(1), new Fixture(2)));
}
+ @Test
+ public void testJoinCollectNullArgs() {
+ assertEquals("", join0((Object[]) null));
+ assertEquals("", join1((Object[]) null));
+ assertEquals("<>", join3((Object[]) null));
+ assertEquals("<>", join4NullToString((Object[]) null));
+ }
+
@Test
public void testJoinCollectStrings0Arg() {
assertEquals("", join0());
@@ -157,7 +165,7 @@ public class LangCollectorsTest {
assertEquals("<1-2>", join4("1", "2"));
assertEquals("<1-2-3>", join4("1", "2", "3"));
assertEquals("<1-null-3>", join4("1", null, "3"));
- assertEquals("<1-NUL-3>", join4Nul("1", null, "3"));
+ assertEquals("<1-NUL-3>", join4NullToString("1", null, "3"));
}
@Test