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 647366307 Add Streams.nonNull(T), non-varargs variant new 0a98b934d Merge branch 'master' of https://gitbox.apache.org/repos/asf/commons-lang.git 647366307 is described below commit 64736630706adf9e2cbb482fbfcbdc81a9dc02f7 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Fri Mar 29 14:12:41 2024 -0400 Add Streams.nonNull(T), non-varargs variant --- src/changes/changes.xml | 2 ++ .../java/org/apache/commons/lang3/stream/Streams.java | 18 +++++++++++++++++- .../org/apache/commons/lang3/stream/StreamsTest.java | 8 ++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 9fa424dc6..321a755a8 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -54,6 +54,8 @@ The <action> type attribute can be add,update,fix,remove. <action issue="LANG-1724" type="add" dev="ggregory" due-to="Gary Gregory">Add ReflectionDiffBuilder.builder().</action> <action issue="LANG-1702" type="add" dev="ggregory" due-to="Elliotte Rusty Harold">Add test in TypeUtilsTest #1151.</action> <action issue="LANG-1724" type="add" dev="ggregory" due-to="Gary Gregory">Add Streams.failableStream(T), non-varargs variant.</action> + <action issue="LANG-1724" type="add" dev="ggregory" due-to="Gary Gregory">Add Streams.nonNull(T), non-varargs variant.</action> + <!-- FIX --> <action type="fix" dev="ggregory" due-to="Miklós Karakó, Gary Gregory">Improve Javadoc in ExceptionUtils #1136.</action> <action type="fix" dev="ggregory" due-to="Saiharshith Karuneegar Ramesh, Gary Gregory">Fixed two non-deterministic tests in EnumUtilsTest.java #1131.</action> diff --git a/src/main/java/org/apache/commons/lang3/stream/Streams.java b/src/main/java/org/apache/commons/lang3/stream/Streams.java index d5399c542..3d5807828 100644 --- a/src/main/java/org/apache/commons/lang3/stream/Streams.java +++ b/src/main/java/org/apache/commons/lang3/stream/Streams.java @@ -588,7 +588,7 @@ public class Streams { * @since 3.15.0 */ public static <T> FailableStream<T> failableStream(final T value) { - return failableStream(value == null ? Stream.empty() : Stream.of(value)); + return failableStream(streamOf(value)); } /** @@ -640,6 +640,18 @@ public class Streams { return of(collection).filter(Objects::nonNull); } + /** + * Streams the non-null element. + * + * @param <E> the type of elements in the collection. + * @param array the element to stream or null. + * @return A non-null stream that filters out a null element. + * @since 3.15.0 + */ + public static <E> Stream<E> nonNull(final E array) { + return nonNull(streamOf(array)); + } + /** * Streams the non-null elements of an array. * @@ -828,6 +840,10 @@ public class Streams { return failableStream(stream); } + private static <T> Stream<T> streamOf(final T value) { + return value == null ? Stream.empty() : Stream.of(value); + } + /** * Returns a {@link Collector} that accumulates the input elements into a new array. * diff --git a/src/test/java/org/apache/commons/lang3/stream/StreamsTest.java b/src/test/java/org/apache/commons/lang3/stream/StreamsTest.java index e232dc7dc..435b02ee7 100644 --- a/src/test/java/org/apache/commons/lang3/stream/StreamsTest.java +++ b/src/test/java/org/apache/commons/lang3/stream/StreamsTest.java @@ -203,6 +203,14 @@ public class StreamsTest extends AbstractLangTest { assertEquals(0, Streams.of(input).collect(Collectors.toList()).size()); } + @Test + public void testNonNull() { + assertEquals(0, Streams.nonNull().collect(Collectors.toList()).size()); + assertEquals(1, Streams.nonNull("A").collect(Collectors.toList()).size()); + assertEquals(1, Streams.nonNull("A", null).collect(Collectors.toList()).size()); + assertEquals(1, Streams.nonNull(null, "A").collect(Collectors.toList()).size()); + } + @Test public void testSimpleStreamFilter() { final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");