You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2019/03/01 08:46:49 UTC

[james-project] 01/04: MAILBOX-381 Add a Iterators::toFlux method

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

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 461d2eee647c187efec39704df8db4acd7cc9c0c
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Wed Feb 27 13:49:45 2019 +0700

    MAILBOX-381 Add a Iterators::toFlux method
---
 .../org/apache/james/util/streams/Iterators.java     |  6 ++++++
 .../org/apache/james/util/streams/IteratorsTest.java | 20 ++++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/server/container/util/src/main/java/org/apache/james/util/streams/Iterators.java b/server/container/util/src/main/java/org/apache/james/util/streams/Iterators.java
index ba3a06b..bb46f6c 100644
--- a/server/container/util/src/main/java/org/apache/james/util/streams/Iterators.java
+++ b/server/container/util/src/main/java/org/apache/james/util/streams/Iterators.java
@@ -23,10 +23,16 @@ import java.util.Iterator;
 import java.util.stream.Stream;
 import java.util.stream.StreamSupport;
 
+import reactor.core.publisher.Flux;
+
 public class Iterators {
 
     public static <T> Stream<T> toStream(Iterator<T> iterator) {
         Iterable<T> iterable = () -> iterator;
         return StreamSupport.stream(iterable.spliterator(), false);
     }
+
+    public static <T> Flux<T> toFlux(Iterator<T> iterator) {
+        return Flux.fromStream(toStream(iterator));
+    }
 }
diff --git a/server/container/util/src/test/java/org/apache/james/util/streams/IteratorsTest.java b/server/container/util/src/test/java/org/apache/james/util/streams/IteratorsTest.java
index e596ef1..bb01f00 100644
--- a/server/container/util/src/test/java/org/apache/james/util/streams/IteratorsTest.java
+++ b/server/container/util/src/test/java/org/apache/james/util/streams/IteratorsTest.java
@@ -29,6 +29,8 @@ import org.junit.jupiter.api.Test;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.UnmodifiableIterator;
 
+import reactor.core.publisher.Flux;
+
 public class IteratorsTest {
 
     @Test
@@ -55,4 +57,22 @@ public class IteratorsTest {
         assertThat(actual.collect(toList())).containsExactly("a", "b", "c");
     }
 
+    @Test
+    void toFluxShouldReturnEmptyStreamWhenEmptyIterator() {
+        UnmodifiableIterator<String> emptyIterator = ImmutableList.<String>of().iterator();
+
+        Flux<String> actual = Iterators.toFlux(emptyIterator);
+
+        assertThat(actual.count().block()).isEqualTo(0);
+    }
+
+    @Test
+    void toFluxShouldReturnSameContent() {
+        UnmodifiableIterator<String> iterator = ImmutableList.of("a", "b", "c").iterator();
+
+        Flux<String> actual = Iterators.toFlux(iterator);
+
+        assertThat(actual.collect(toList()).block()).containsExactly("a", "b", "c");
+    }
+
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org