JAMES-2082 Adds Javadoc to FluentFutureStream
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/6b1c3e79 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/6b1c3e79 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/6b1c3e79 Branch: refs/heads/master Commit: 6b1c3e79f4ebe7f1beaef4c3b52d75667810596b Parents: 26ce1f4 Author: benwa <[email protected]> Authored: Sun Jul 9 13:31:57 2017 +0700 Committer: Antoine Duprat <[email protected]> Committed: Mon Jul 10 14:23:58 2017 +0200 ---------------------------------------------------------------------- .../apache/james/util/FluentFutureStream.java | 103 +++++++++++++++---- 1 file changed, 82 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/6b1c3e79/server/container/util-java8/src/main/java/org/apache/james/util/FluentFutureStream.java ---------------------------------------------------------------------- diff --git a/server/container/util-java8/src/main/java/org/apache/james/util/FluentFutureStream.java b/server/container/util-java8/src/main/java/org/apache/james/util/FluentFutureStream.java index fa40616..fbdd1c2 100644 --- a/server/container/util-java8/src/main/java/org/apache/james/util/FluentFutureStream.java +++ b/server/container/util-java8/src/main/java/org/apache/james/util/FluentFutureStream.java @@ -30,24 +30,43 @@ public class FluentFutureStream<T> { private final CompletableFuture<Stream<T>> completableFuture; + /** + * Constructs a FluentFutureStream from a future of Stream. + */ public static <T> FluentFutureStream<T> of(CompletableFuture<Stream<T>> completableFuture) { return new FluentFutureStream<>(completableFuture); } + /** + * Constructs a FluentFutureStream from a Stream of Future + */ + public static <T> FluentFutureStream<T> of(Stream<CompletableFuture<T>> completableFutureStream) { + return new FluentFutureStream<>(CompletableFutureUtil.allOf(completableFutureStream)); + } + + /** + * Constructs a FluentFutureStream from a Stream of Future of Stream. + * + * Underlying streams are flatMapped. + */ public static <T> FluentFutureStream<T> ofNestedStreams(Stream<CompletableFuture<Stream<T>>> completableFuture) { return of(completableFuture) .flatMap(Function.identity()); } + /** + * Constructs a FluentFutureStream from a Stream of Future of Optionals. + * + * Underlying optionals are unboxed. + */ public static <T> FluentFutureStream<T> ofOptionals(Stream<CompletableFuture<Optional<T>>> completableFuture) { return of(completableFuture) .flatMapOptional(Function.identity()); } - public static <T> FluentFutureStream<T> of(Stream<CompletableFuture<T>> completableFutureStream) { - return new FluentFutureStream<>(CompletableFutureUtil.allOf(completableFutureStream)); - } - + /** + * Constructs a FluentFutureStream from the supplied futures. + */ @SafeVarargs public static <T> FluentFutureStream<T> ofFutures(CompletableFuture<T>... completableFutures) { return new FluentFutureStream<>(CompletableFutureUtil.allOfArray(completableFutures)); @@ -57,64 +76,106 @@ public class FluentFutureStream<T> { this.completableFuture = completableFuture; } + /** + * For all values of the underlying stream, an action will be performed. + */ public FluentFutureStream<T> performOnAll(Function<T, CompletableFuture<Void>> action) { return FluentFutureStream.of( CompletableFutureUtil.performOnAll(completableFuture(), action)); } + /** + * Apply a transformation to all values of the underlying stream. + */ public <U> FluentFutureStream<U> map(Function<T, U> function) { return FluentFutureStream.of( CompletableFutureUtil.map(completableFuture(), function)); } - public CompletableFuture<Optional<T>> reduce(BinaryOperator<T> combiner) { - return CompletableFutureUtil.reduce(combiner, completableFuture); + /** + * Apply a transformation to all value of the underlying stream. + * + * As the supplied transformation produces streams, the results will be flatMapped. + */ + public <U> FluentFutureStream<U> flatMap(Function<T, Stream<U>> function) { + return FluentFutureStream.of(completableFuture().thenApply(stream -> + stream.flatMap(function))); } - public CompletableFuture<T> reduce(T emptyAccumulator, BinaryOperator<T> combiner) { - return CompletableFutureUtil.reduce(combiner, completableFuture, emptyAccumulator); + /** + * Apply a transformation to all value of the underlying stream. + * + * As the supplied transformation produces optionals, the results will be unboxed. + */ + public <U> FluentFutureStream<U> flatMapOptional(Function<T, Optional<U>> function) { + return map(function) + .flatMap(OptionalConverter::toStream); } + /** + * Apply a transformation to all value of the underlying stream. + * + * As the supplied transformation produces futures, we need to compose the returned values. + */ public <U> FluentFutureStream<U> thenComposeOnAll(Function<T, CompletableFuture<U>> function) { return FluentFutureStream.of( CompletableFutureUtil.thenComposeOnAll(completableFuture(), function)); } + /** + * Apply a transformation to all value of the underlying stream. + * + * As the supplied transformation produces futures of stream, we need to compose then flatMap the returned values. + */ public <U> FluentFutureStream<U> thenFlatCompose(Function<T, CompletableFuture<Stream<U>>> function) { return FluentFutureStream.of( CompletableFutureUtil.thenComposeOnAll(completableFuture(), function)) .flatMap(Function.identity()); } + /** + * Apply a transformation to all value of the underlying stream. + * + * As the supplied transformation produces futures of optionals, we need to compose then unbox the returned values. + */ public <U> FluentFutureStream<U> thenFlatComposeOnOptional(Function<T, CompletableFuture<Optional<U>>> function) { return FluentFutureStream.of( CompletableFutureUtil.thenComposeOnAll(completableFuture(), function)) .flatMapOptional(Function.identity()); } - public <U> FluentFutureStream<U> flatMap(Function<T, Stream<U>> function) { - return FluentFutureStream.of(completableFuture().thenApply(stream -> - stream.flatMap(function))); + /** + * Filter the values of the underlying stream. + */ + public FluentFutureStream<T> filter(Predicate<T> predicate) { + return FluentFutureStream.of(completableFuture + .thenApply(stream -> stream.filter(predicate))); } - public <U> FluentFutureStream<U> flatMapOptional(Function<T, Optional<U>> function) { - return map(function) - .flatMap(OptionalConverter::toStream); + /** + * Reduces the underlying stream. Reduced value is supplied as a Future of optional, as no empty value is supplied. + */ + public CompletableFuture<Optional<T>> reduce(BinaryOperator<T> combiner) { + return CompletableFutureUtil.reduce(combiner, completableFuture); } - public <U> FluentFutureStream<U> thenCompose(Function<Stream<T>, CompletableFuture<Stream<U>>> function) { - return FluentFutureStream.of(completableFuture().thenCompose(function)); + /** + * educes the underlying stream. Reduced value is supplied as a Future, as an empty value is specified. + */ + public CompletableFuture<T> reduce(T emptyAccumulator, BinaryOperator<T> combiner) { + return CompletableFutureUtil.reduce(combiner, completableFuture, emptyAccumulator); } + /** + * Returns a future of the underlying stream. + */ public CompletableFuture<Stream<T>> completableFuture() { return this.completableFuture; } - public FluentFutureStream<T> filter(Predicate<T> predicate) { - return FluentFutureStream.of(completableFuture - .thenApply(stream -> stream.filter(predicate))); - } - + /** + * Join and returns the underlying stream. + */ public Stream<T> join() { return completableFuture().join(); } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
