Add Streams.of(Iterable<E>)

This commit is contained in:
Gary Gregory 2022-07-16 14:44:06 -04:00
parent 152b1777fd
commit 9be39c4a7a
3 changed files with 24 additions and 0 deletions

View File

@ -158,6 +158,7 @@ The <action> type attribute can be add,update,fix,remove.
<action issue="LANG-1627" type="add" dev="ggregory" due-to="Alberto Scotto, Avijit Chakraborty, Steve Bosman, Bruno P. Kinoshita, Gary Gregory">Add ArrayUtils.oneHot().</action> <action issue="LANG-1627" type="add" dev="ggregory" due-to="Alberto Scotto, Avijit Chakraborty, Steve Bosman, Bruno P. Kinoshita, Gary Gregory">Add ArrayUtils.oneHot().</action>
<action issue="LANG-1662" type="add" dev="ggregory" due-to="Daniel Augusto Veronezi Salvador, Gary Gregory, Bruno P. Kinoshita">Let ReflectionToStringBuilder only reflect given field names #849.</action> <action issue="LANG-1662" type="add" dev="ggregory" due-to="Daniel Augusto Veronezi Salvador, Gary Gregory, Bruno P. Kinoshita">Let ReflectionToStringBuilder only reflect given field names #849.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Streams.of(Enumeration&lt;E&gt;).</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add Streams.of(Enumeration&lt;E&gt;).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Streams.of(Iterable&lt;E&gt;).</action>
<!-- UPDATE --> <!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Dependabot, XenoAmess, Gary Gregory">Bump actions/cache from 2.1.4 to 3.0.5 #742, #752, #764, #833, #867.</action> <action type="update" dev="ggregory" due-to="Dependabot, XenoAmess, Gary Gregory">Bump actions/cache from 2.1.4 to 3.0.5 #742, #752, #764, #833, #867.</action>
<action type="update" dev="ggregory" due-to="Dependabot">Bump actions/checkout from 2 to 3 #819, #825, #859.</action> <action type="update" dev="ggregory" due-to="Dependabot">Bump actions/checkout from 2 to 3 #819, #825, #859.</action>

View File

@ -641,6 +641,18 @@ public class Streams {
return StreamSupport.stream(new EnumerationSpliterator<>(Long.MAX_VALUE, Spliterator.ORDERED, enumeration), false); return StreamSupport.stream(new EnumerationSpliterator<>(Long.MAX_VALUE, Spliterator.ORDERED, enumeration), false);
} }
/**
* Creates a stream on the given Iterable.
*
* @param <E> the type of elements in the Iterable.
* @param iterable the Iterable to stream or null.
* @return a new Stream or {@link Stream#empty()} if the Iterable is null.
* @since 3.13.0
*/
public static <E> Stream<E> of(final Iterable<E> iterable) {
return iterable == null ? Stream.empty() : StreamSupport.stream(iterable.spliterator(), false);
}
/** /**
* Null-safe version of {@link Stream#of(Object[])}. * Null-safe version of {@link Stream#of(Object[])}.
* *

View File

@ -188,6 +188,17 @@ public class StreamsTest extends AbstractLangTest {
assertEquals(2, collect.size()); assertEquals(2, collect.size());
} }
@Test
public void testOfIterableNotNull() {
assertEquals(2, Streams.of((Iterable<String>) Arrays.asList("A", "B")).collect(Collectors.toList()).size());
}
@Test
public void testOfIterableNull() {
final Iterable<String> input = null;
assertEquals(0, Streams.of(input).collect(Collectors.toList()).size());
}
@Test @Test
public void testSimpleStreamFilter() { public void testSimpleStreamFilter() {
final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6"); final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");