JAMES-1925 Util for unboxing CompletableFuture streams
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/7c223af7 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/7c223af7 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/7c223af7 Branch: refs/heads/master Commit: 7c223af725dd24ab8d4ab3a6915acfebb9777d19 Parents: be93338 Author: Benoit Tellier <[email protected]> Authored: Tue Feb 14 10:55:39 2017 +0700 Committer: Antoine Duprat <[email protected]> Committed: Wed Feb 15 13:12:37 2017 +0100 ---------------------------------------------------------------------- .../james/util/CompletableFutureUtil.java | 40 ++++++++++++++ .../james/util/CompletableFutureUtilTest.java | 57 ++++++++++++++++++++ 2 files changed, 97 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/7c223af7/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 new file mode 100644 index 0000000..2d114ab --- /dev/null +++ b/server/container/util-java8/src/main/java/org/apache/james/util/CompletableFutureUtil.java @@ -0,0 +1,40 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.util; + +import java.util.concurrent.CompletableFuture; +import java.util.stream.Stream; + +public class CompletableFutureUtil { + + public static <T> CompletableFuture<Stream<T>> allOf(Stream<CompletableFuture<T>> futureStream) { + return futureStream + .map(future -> future.thenApply(Stream::of)) + .reduce((future1, future2) -> + future1.thenCompose( + stream1 -> future2.thenCompose( + stream2 -> { + Stream<T> concatStream = Stream.concat(stream1, stream2); + return CompletableFuture.completedFuture(concatStream); + }))) + .orElse(CompletableFuture.completedFuture(Stream.of())); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/7c223af7/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 new file mode 100644 index 0000000..fa12233 --- /dev/null +++ b/server/container/util-java8/src/test/java/org/apache/james/util/CompletableFutureUtilTest.java @@ -0,0 +1,57 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.util; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.concurrent.CompletableFuture; +import java.util.stream.Stream; + +import org.junit.Test; + +import com.github.steveash.guavate.Guavate; + +public class CompletableFutureUtilTest { + + @Test + public void allOfShouldUnboxEmptyStream() { + assertThat( + CompletableFutureUtil.allOf(Stream.empty()) + .join() + .collect(Guavate.toImmutableList())) + .isEmpty(); + } + + @Test + public void allOfShouldUnboxStream() { + long value1 = 18L; + long value2 = 19L; + long value3 = 20L; + assertThat( + CompletableFutureUtil.allOf( + Stream.of( + CompletableFuture.completedFuture(value1), + CompletableFuture.completedFuture(value2), + CompletableFuture.completedFuture(value3))) + .join() + .collect(Guavate.toImmutableList())) + .containsOnly(value1, value2, value3); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
