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)); }
/** * 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()); }
@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(); }