/** * 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()); }
/** * 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 flatMapOptionalShouldTransformUnderlyingValuesAndUnboxResult() { assertThat( FluentFutureStream.of( CompletableFuture.completedFuture( Stream.of(1, 2, 3))) .flatMapOptional(i -> Optional.of(i + 1) .filter(j -> j % 2 == 0)) .join() .collect(Guavate.toImmutableList())) .containsExactly(2, 4); }