Add and use null-safe Streams.of(T[]).

This commit is contained in:
Gary Gregory 2021-07-01 14:46:15 -04:00
parent 11391a424a
commit 5c6de5a7a0
4 changed files with 23 additions and 2 deletions

View File

@ -59,6 +59,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary Gregory">Add and use ClassUtils.getComponentType(Class&gt;T[]>).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add and use ObjectUtils.getClass(T).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add and use ArrayUtils.newInstance(Class&gt;T>, int).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add and use null-safe Streams.of(T...).</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump spotbugs-maven-plugin from 4.2.0 to 4.2.3 #735.</action>
<action type="update" dev="ggregory" due-to="Dependabot, XenoAmess">Bump Bump actions/cache from v2.1.4 to v2.1.6 #742, #752, #764.</action>

View File

@ -18,9 +18,9 @@ package org.apache.commons.lang3;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
import org.apache.commons.lang3.arch.Processor;
import org.apache.commons.lang3.stream.Streams;
/**
* An utility class for the os.arch System Property. The class defines methods for
@ -101,7 +101,7 @@ public class ArchUtils {
* @throws IllegalStateException If the key already exists.
*/
private static void addProcessors(final Processor processor, final String... keys) {
Stream.of(keys).forEach(e -> addProcessor(e, processor));
Streams.of(keys).forEach(e -> addProcessor(e, processor));
}
/**

View File

@ -416,6 +416,19 @@ public class Streams {
}
}
/**
* Null-safe version of {@link Stream#of(Object[])}.
*
* @param <T> the type of stream elements.
* @param values the elements of the new stream, may be {@code null}.
* @return the new stream on {@code values} or {@link Stream#empty()}.
* @since 3.13.0
*/
@SafeVarargs // Creating a stream from an array is safe
public static <T> Stream<T> of(T... values) {
return values == null ? Stream.empty() : Stream.of(values);
}
/**
* Converts the given {@link Collection} into a {@link FailableStream}. This is basically a simplified, reduced
* version of the {@link Stream} class, with the same underlying element stream, except that failable objects, like

View File

@ -135,6 +135,13 @@ public class StreamsTest {
}));
}
@Test
public void testOf() {
assertEquals(0, Streams.of((Object[]) null).count());
assertEquals(1, Streams.of("foo").count());
assertEquals(2, Streams.of("foo", "bar").count());
}
@Test
public void testSimpleStreamFilter() {
final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");