james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adup...@apache.org
Subject [5/7] james-project git commit: JAMES-1948 add helper to CompletableFutureUtil
Date Thu, 02 Mar 2017 13:44:47 GMT
JAMES-1948 add helper to CompletableFutureUtil


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/01d21eb4
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/01d21eb4
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/01d21eb4

Branch: refs/heads/master
Commit: 01d21eb4213ddf738de465316a2cb3c613cc9c81
Parents: 33a5846
Author: Luc DUZAN <lduzan@linagora.com>
Authored: Fri Feb 24 19:06:38 2017 +0100
Committer: Luc DUZAN <lduzan@linagora.com>
Committed: Thu Mar 2 12:31:25 2017 +0100

----------------------------------------------------------------------
 .../james/util/CompletableFutureUtil.java       | 26 +++++++
 .../james/util/CompletableFutureUtilTest.java   | 79 +++++++++++++++++++-
 2 files changed, 104 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/01d21eb4/server/container/util-java8/src/main/java/org/apache/james/util/CompletableFutureUtil.java
----------------------------------------------------------------------
diff --git a/server/container/util-java8/src/main/java/org/apache/james/util/CompletableFutureUtil.java
b/server/container/util-java8/src/main/java/org/apache/james/util/CompletableFutureUtil.java
index b014592..ba4f30a 100644
--- a/server/container/util-java8/src/main/java/org/apache/james/util/CompletableFutureUtil.java
+++ b/server/container/util-java8/src/main/java/org/apache/james/util/CompletableFutureUtil.java
@@ -20,6 +20,8 @@
 package org.apache.james.util;
 
 import java.util.concurrent.CompletableFuture;
+import java.util.function.Function;
+import java.util.function.Supplier;
 import java.util.stream.Stream;
 
 public class CompletableFutureUtil {
@@ -38,4 +40,28 @@ public class CompletableFutureUtil {
             .orElse(CompletableFuture.completedFuture(Stream.of()));
     }
 
+    public static <T> CompletableFuture<Stream<T>> performOnAll(CompletableFuture<Stream<T>>
futurStream, Function<T, CompletableFuture<Void>> action) {
+        return thenComposeOnAll(futurStream, value ->
+            keepValue(() ->
+                action.apply(value),
+                value));
+    }
+
+    public static <T, U> CompletableFuture<Stream<U>> thenComposeOnAll(CompletableFuture<Stream<T>>
futurStream, Function<T, CompletableFuture<U>> action) {
+        return futurStream
+            .thenCompose(stream ->
+                CompletableFutureUtil.allOf(
+                    stream.map(action::apply)));
+    }
+
+    public static <T, U> CompletableFuture<Stream<U>> map(CompletableFuture<Stream<T>>
futurStream, Function<T, U> action) {
+        return futurStream
+            .thenApply(stream ->
+                stream.map(value ->
+                    action.apply(value)));
+    }
+
+    public static <T> CompletableFuture<T> keepValue(Supplier<CompletableFuture<Void>>
supplier, T value) {
+        return supplier.get().thenApply(any -> value);
+    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/01d21eb4/server/container/util-java8/src/test/java/org/apache/james/util/CompletableFutureUtilTest.java
----------------------------------------------------------------------
diff --git a/server/container/util-java8/src/test/java/org/apache/james/util/CompletableFutureUtilTest.java
b/server/container/util-java8/src/test/java/org/apache/james/util/CompletableFutureUtilTest.java
index 55cef98..31eb3f4 100644
--- a/server/container/util-java8/src/test/java/org/apache/james/util/CompletableFutureUtilTest.java
+++ b/server/container/util-java8/src/test/java/org/apache/james/util/CompletableFutureUtilTest.java
@@ -22,6 +22,8 @@ package org.apache.james.util;
 import static org.assertj.core.api.Assertions.assertThat;
 
 import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Supplier;
 import java.util.stream.IntStream;
 import java.util.stream.Stream;
 
@@ -56,7 +58,6 @@ public class CompletableFutureUtilTest {
             .containsOnly(value1, value2, value3);
     }
 
-
     @Test
     public void allOfShouldPreserveOrder() {
         long value1 = 18L;
@@ -95,4 +96,80 @@ public class CompletableFutureUtilTest {
                 .map(CompletableFuture::completedFuture))
             .join();
     }
+
+    @Test
+    public void mapShouldMapOnStreamInsideACompletableFuturOfStream() {
+        CompletableFuture<Stream<Integer>> futurOfInteger = CompletableFuture.completedFuture(Stream.of(1,
2, 3));
+
+        assertThat(
+            CompletableFutureUtil.map(futurOfInteger, integer ->
+                integer * 2)
+                .join()
+                .collect(Guavate.toImmutableList()))
+            .containsExactly(2, 4, 6);
+    }
+
+    @Test
+    public void mapShouldReturnEmptyStreamWhenGivenAnEmptyStream() {
+        CompletableFuture<Stream<Integer>> futurOfInteger = CompletableFuture.completedFuture(Stream.of());
+
+        assertThat(
+            CompletableFutureUtil.map(futurOfInteger, integer ->
+                integer * 2)
+                .join()
+                .collect(Guavate.toImmutableList()))
+            .isEmpty();
+    }
+
+    @Test
+    public void thenComposeOnAllShouldMapOnStreamInsideACompletableFuturOfStreamAndTransformTheResultingStreamOfCompletableFutureIntoACompletableOfStreamAndFlatIt()
{
+        CompletableFuture<Stream<Integer>> futurOfInteger = CompletableFuture.completedFuture(Stream.of(1,
2, 3));
+
+        assertThat(
+            CompletableFutureUtil.thenComposeOnAll(futurOfInteger, integer ->
+                CompletableFuture.completedFuture(integer * 2))
+                .join()
+                .collect(Guavate.toImmutableList()))
+            .containsExactly(2, 4, 6);
+    }
+
+    @Test
+    public void thenComposeOnAllOnEmptyStreamShouldReturnAnEmptyStream() {
+        CompletableFuture<Stream<Integer>> futurOfInteger = CompletableFuture.completedFuture(Stream.of());
+
+        assertThat(
+            CompletableFutureUtil.thenComposeOnAll(futurOfInteger, integer ->
+                CompletableFuture.completedFuture(integer * 2))
+                .join()
+                .collect(Guavate.toImmutableList()))
+            .isEmpty();
+    }
+
+    @Test
+    public void keepValueShouldCompleteWhenTheGivenCompletableFutureEnd() {
+        final AtomicInteger numOfFutureExecution = new AtomicInteger(0);
+
+        Supplier<CompletableFuture<Void>> future = () ->
+            CompletableFuture.runAsync(numOfFutureExecution::incrementAndGet);
+
+        assertThat(
+            CompletableFutureUtil.keepValue(future, 42)
+                .join())
+            .isEqualTo(42);
+
+        assertThat(
+            numOfFutureExecution.get())
+            .isEqualTo(1);
+    }
+
+    @Test
+    public void keepValueShouldReturnNullWithNullValue() {
+        Supplier<CompletableFuture<Void>> future = () ->
+            CompletableFuture.completedFuture(null);
+
+        assertThat(
+            CompletableFutureUtil.keepValue(future, null)
+                .join())
+            .isNull();
+    }
 }


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


Mime
View raw message