/** * Filter the values of the underlying stream. */ public FluentFutureStream<T> filter(Predicate<T> predicate) { return FluentFutureStream.of(completableFuture .thenApply(stream -> stream.filter(predicate))); }
/** * Filter the values of the underlying stream. */ public FluentFutureStream<T> filter(Predicate<T> predicate) { return FluentFutureStream.of(completableFuture .thenApply(stream -> stream.filter(predicate))); }
public static <T> FluentFutureStream<T> unboxStream(FluentFutureStream<Stream<T>> streams) { return FluentFutureStream.of( streams.completableFuture() .thenApply(StreamUtils::flatten)); }
/** * 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 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))); }
@Override public CompletableFuture<I> save(T t) { return FluentFutureStream.of( encoder.encode(t) .map(this::saveEntry)) .completableFuture() .thenApply(pairStream -> pairStream.collect(ImmutableMap.toImmutableMap(Pair::getKey, Pair::getValue))) .thenApply(idFactory::generate); }
private CompletableFuture<Stream<MessageResult>> rowToMessages(FetchType fetchType, Collection<ComposedMessageIdWithMetaData> ids) { return FluentFutureStream.of( ids.stream() .map(id -> retrieveRow(id, fetchType) .thenCompose((ResultSet resultSet) -> message(resultSet, id, fetchType)))) .completableFuture(); }
/** * sort all elements of the stream by the provided {@code Comparator}. */ public FluentFutureStream<T> sorted(Comparator<T> comparator) { return FluentFutureStream.of( CompletableFutureUtil.sorted(completableFuture(), comparator)); }
/** * 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)); }
private FlagsUpdateStageResult retryUpdatesStage(CassandraId mailboxId, FlagsUpdateCalculator flagsUpdateCalculator, List<MessageUid> failed) { Stream<ComposedMessageIdWithMetaData> idsFailed = FluentFutureStream.of( failed.stream().map(uid -> messageIdDAO.retrieve(mailboxId, uid)), FluentFutureStream::unboxOptional) .join(); return runUpdateStage(mailboxId, idsFailed, flagsUpdateCalculator); }
private CompletableFuture<FlagsUpdateStageResult> updateIndexesForUpdatesResult(CassandraId mailboxId, FlagsUpdateStageResult result) { return FluentFutureStream.of( result.getSucceeded().stream() .map(Throwing .function((UpdatedFlags updatedFlags) -> indexTableHandler.updateIndexOnFlagsUpdate(mailboxId, updatedFlags)) .fallbackTo(failedindex -> { LOGGER.error("Could not update flag indexes for mailboxId {} UID {}. This will lead to inconsistencies across Cassandra tables", mailboxId, failedindex.getUid()); return CompletableFuture.completedFuture(null); }))) .completableFuture() .thenApply(any -> result); }
private List<Mailbox> toMailboxes(MailboxPath path, CompletableFuture<Stream<CassandraIdAndPath>> listUserMailboxes) { Pattern regex = Pattern.compile(constructEscapedRegexForMailboxNameMatching(path)); return FluentFutureStream.of(listUserMailboxes) .filter(idAndPath -> regex.matcher(idAndPath.getMailboxPath().getName()).matches()) .map(this::retrieveMailbox, FluentFutureStream::unboxFutureOptional) .join() .collect(Guavate.toImmutableList()); }
@Test void reduceShouldGatherAllValuesOfTheUnderlyingStreamWithAnEmptyValue() { assertThat( FluentFutureStream.of( CompletableFuture.completedFuture( Stream.of(1, 2, 3))) .reduce(0, (a, b) -> a + b) .join()) .isEqualTo(6); }
@Test public void collectShouldReturnTheCollectionOfData() { assertThat( FluentFutureStream.of( Stream.of( CompletableFuture.completedFuture(1), CompletableFuture.completedFuture(2), CompletableFuture.completedFuture(3))) .collect(Guavate.toImmutableList()) .join()) .containsExactly(1, 2, 3); }
@Test void completableFutureShouldReturnAFutureOfTheUnderLayingStream() { assertThat( FluentFutureStream.of( CompletableFuture.completedFuture( Stream.of(1, 2, 3))) .completableFuture() .join() .collect(Guavate.toImmutableList())) .containsExactly(1, 2, 3); }
@Test public void completableFutureShouldReturnAFutureOfTheUnderLayingStream() { assertThat( FluentFutureStream.of( CompletableFuture.completedFuture( Stream.of(1, 2, 3))) .completableFuture() .join() .collect(Guavate.toImmutableList())) .containsExactly(1, 2, 3); }
@Test void ofShouldConstructAFluentFutureStreamWhenProvidedAFutureOfStream() { assertThat( FluentFutureStream.of( CompletableFuture.completedFuture( Stream.of(1, 2, 3))) .join() .collect(Guavate.toImmutableList())) .containsExactly(1, 2, 3); }
@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); }
@Test public void reduceShouldGatherAllValuesOfTheUnderlyingStream() { assertThat( FluentFutureStream.of( CompletableFuture.completedFuture( Stream.of(1, 2, 3))) .reduce((a, b) -> a + b) .join()) .contains(6); }
@Test public void thenComposeOnAllShouldTransformUnderlyingValuesAndComposeFutures() { assertThat( FluentFutureStream.of( CompletableFuture.completedFuture( Stream.of(1, 2, 3))) .thenComposeOnAll(i -> CompletableFuture.completedFuture(i + 1)) .join() .collect(Guavate.toImmutableList())) .containsExactly(2, 3, 4); }