You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/07/16 18:44:12 UTC

[commons-lang] branch master updated (e0818e338 -> 9be39c4a7)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


    from e0818e338 Type safety
     new 152b1777f Rename new method
     new 9be39c4a7 Add Streams.of(Iterable<E>)

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/changes/changes.xml                            |  1 +
 .../org/apache/commons/lang3/stream/Streams.java   | 42 ++++++++++++-------
 .../apache/commons/lang3/stream/StreamsTest.java   | 47 +++++++++++++---------
 3 files changed, 57 insertions(+), 33 deletions(-)


[commons-lang] 01/02: Rename new method

Posted by gg...@apache.org.
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

commit 152b1777fddc5127e77f9b14637d6abae18eaf05
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jul 16 11:14:44 2022 -0400

    Rename new method
---
 .../org/apache/commons/lang3/stream/Streams.java   | 30 +++++++++---------
 .../apache/commons/lang3/stream/StreamsTest.java   | 36 +++++++++++-----------
 2 files changed, 33 insertions(+), 33 deletions(-)

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 720405129..eee85087e 100644
--- a/src/main/java/org/apache/commons/lang3/stream/Streams.java
+++ b/src/main/java/org/apache/commons/lang3/stream/Streams.java
@@ -530,7 +530,7 @@ public class Streams {
      * @since 3.13.0
      */
     public static <T> FailableStream<T> failableStream(final Collection<T> stream) {
-        return failableStream(toStream(stream));
+        return failableStream(of(stream));
     }
 
     /**
@@ -578,7 +578,7 @@ public class Streams {
     }
 
     private static <E> Stream<E> filter(final Collection<E> collection, final Predicate<? super E> predicate) {
-        return toStream(collection).filter(predicate);
+        return of(collection).filter(predicate);
     }
 
     /**
@@ -597,7 +597,7 @@ public class Streams {
      * @since 3.13.0
      */
     public static <E> Stream<E> instancesOf(final Class<? super E> clazz, final Collection<? super E> collection) {
-        return instancesOf(clazz, toStream(collection));
+        return instancesOf(clazz, of(collection));
     }
 
     @SuppressWarnings("unchecked") // After the isInstance check, we still need to type-cast.
@@ -617,6 +617,18 @@ public class Streams {
         return filter(collection, Objects::nonNull);
     }
 
+    /**
+     * Delegates to {@link Collection#stream()} or returns {@link Stream#empty()} if the collection is null.
+     *
+     * @param <E> the type of elements in the collection.
+     * @param collection the collection to stream or null.
+     * @return {@link Collection#stream()} or {@link Stream#empty()} if the collection is null.
+     * @since 3.13.0
+     */
+    public static <E> Stream<E> of(final Collection<E> collection) {
+        return collection == null ? Stream.empty() : collection.stream();
+    }
+
     /**
      * Streams the elements of the given enumeration in order.
      *
@@ -742,16 +754,4 @@ public class Streams {
     public static <T extends Object> Collector<T, ?, T[]> toArray(final Class<T> pElementType) {
         return new ArrayCollector<>(pElementType);
     }
-
-    /**
-     * Delegates to {@link Collection#stream()} or returns {@link Stream#empty()} if the collection is null.
-     *
-     * @param <E> the type of elements in the collection.
-     * @param collection the collection to stream or null.
-     * @return {@link Collection#stream()} or {@link Stream#empty()} if the collection is null.
-     * @since 3.13.0
-     */
-    public static <E> Stream<E> toStream(final Collection<E> collection) {
-        return collection == null ? Stream.empty() : collection.stream();
-    }
 }
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 99b34204b..3db2ec794 100644
--- a/src/test/java/org/apache/commons/lang3/stream/StreamsTest.java
+++ b/src/test/java/org/apache/commons/lang3/stream/StreamsTest.java
@@ -133,13 +133,6 @@ public class StreamsTest extends AbstractLangTest {
             }));
     }
 
-    @Test
-    public void testNullSafeStreamNotNull() {
-        assertEquals(2, Streams.nonNull(Arrays.asList("A", "B")).collect(Collectors.toList()).size());
-        assertEquals(2, Streams.nonNull(Arrays.asList(null, "A", null, "B", null)).collect(Collectors.toList()).size());
-        assertEquals(0, Streams.nonNull(Arrays.asList(null, null)).collect(Collectors.toList()).size());
-    }
-
     @Test
     public void testInstanceOfStream() {
         assertEquals(2, Streams.instancesOf(String.class, Arrays.asList("A", "B")).collect(Collectors.toList()).size());
@@ -150,6 +143,13 @@ public class StreamsTest extends AbstractLangTest {
         assertEquals(2, Streams.instancesOf(String.class, objects).collect(Collectors.toList()).size());
     }
 
+    @Test
+    public void testNullSafeStreamNotNull() {
+        assertEquals(2, Streams.nonNull(Arrays.asList("A", "B")).collect(Collectors.toList()).size());
+        assertEquals(2, Streams.nonNull(Arrays.asList(null, "A", null, "B", null)).collect(Collectors.toList()).size());
+        assertEquals(0, Streams.nonNull(Arrays.asList(null, null)).collect(Collectors.toList()).size());
+    }
+
     @Test
     public void testNullSafeStreamNull() {
         final List<String> input = null;
@@ -163,6 +163,17 @@ public class StreamsTest extends AbstractLangTest {
         assertEquals(2, Streams.of("foo", "bar").count());
     }
 
+    @Test
+    public void testOfCollectionNotNull() {
+        assertEquals(2, Streams.of(Arrays.asList("A", "B")).collect(Collectors.toList()).size());
+    }
+
+    @Test
+    public void testOfCollectionNull() {
+        final List<String> input = null;
+        assertEquals(0, Streams.of(input).collect(Collectors.toList()).size());
+    }
+
     @Test
     public void testOfEnumeration() {
         final Hashtable<String, Integer> table = new Hashtable<>();
@@ -235,15 +246,4 @@ public class StreamsTest extends AbstractLangTest {
         assertEquals("1", array[2]);
     }
 
-    @Test
-    public void testToStreamNotNull() {
-        assertEquals(2, Streams.toStream(Arrays.asList("A", "B")).collect(Collectors.toList()).size());
-    }
-
-    @Test
-    public void testToStreamNull() {
-        final List<String> input = null;
-        assertEquals(0, Streams.toStream(input).collect(Collectors.toList()).size());
-    }
-
 }


[commons-lang] 02/02: Add Streams.of(Iterable)

Posted by gg...@apache.org.
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

commit 9be39c4a7aa7a9a0e3e9cbf29c85884623a9fa4f
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jul 16 14:44:06 2022 -0400

    Add Streams.of(Iterable<E>)
---
 src/changes/changes.xml                                      |  1 +
 src/main/java/org/apache/commons/lang3/stream/Streams.java   | 12 ++++++++++++
 .../java/org/apache/commons/lang3/stream/StreamsTest.java    | 11 +++++++++++
 3 files changed, 24 insertions(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index e5e32ada0..f16b4ad6b 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -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-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(Iterable&lt;E&gt;).</action>
     <!-- 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">Bump actions/checkout from 2 to 3 #819, #825, #859.</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 eee85087e..ee576c723 100644
--- a/src/main/java/org/apache/commons/lang3/stream/Streams.java
+++ b/src/main/java/org/apache/commons/lang3/stream/Streams.java
@@ -641,6 +641,18 @@ public class Streams {
         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[])}.
      *
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 3db2ec794..073e9cc71 100644
--- a/src/test/java/org/apache/commons/lang3/stream/StreamsTest.java
+++ b/src/test/java/org/apache/commons/lang3/stream/StreamsTest.java
@@ -188,6 +188,17 @@ public class StreamsTest extends AbstractLangTest {
         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
     public void testSimpleStreamFilter() {
         final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");