/** * Join and returns the underlying stream. */ public Stream<T> join() { return completableFuture().join(); } }
/** * Join and returns the underlying stream. */ public Stream<T> join() { return completableFuture().join(); } }
/** * Join and returns the underlying stream. */ public Stream<T> join() { return completableFuture().join(); } }
public static <T> FluentFutureStream<T> unboxStream(FluentFutureStream<Stream<T>> streams) { return FluentFutureStream.of( streams.completableFuture() .thenApply(StreamUtils::flatten)); }
public static <T> FluentFutureStream<T> unboxStream(FluentFutureStream<Stream<T>> streams) { return FluentFutureStream.of( streams.completableFuture() .thenApply(StreamUtils::flatten)); }
/** * 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<T> read(I blobIds) { CompletableFuture<Stream<Pair<BlobType, byte[]>>> binaries = FluentFutureStream.of(blobIds.asMap() .entrySet() .stream() .map(entry -> blobStore.readBytes(entry.getValue()) .thenApply(bytes -> Pair.of(entry.getKey(), bytes)))) .completableFuture(); return binaries.thenApply(decoder::decode); } }
@Override public CompletableFuture<T> read(I blobIds) { CompletableFuture<Stream<Pair<BlobType, byte[]>>> binaries = FluentFutureStream.of(blobIds.asMap() .entrySet() .stream() .map(entry -> blobStore.readBytes(entry.getValue()) .thenApply(bytes -> Pair.of(entry.getKey(), bytes)))) .completableFuture(); return binaries.thenApply(decoder::decode); } }
@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); }
@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(); }
/** * 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)); }
/** * 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)); }
/** * 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)); }
/** * 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()); }
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 CompletableFuture<Stream<SimpleMailboxMessage>> expungeUidChunk(CassandraId mailboxId, Collection<MessageUid> uidChunk) { return FluentFutureStream.of( uidChunk.stream().map(uid -> retrieveComposedId(mailboxId, uid)), FluentFutureStream::unboxOptional) .performOnAll(this::deleteUsingMailboxId) .map(idWithMetadata -> FluentFutureStream.of( messageDAO.retrieveMessages(ImmutableList.of(idWithMetadata), FetchType.Metadata, Limit.unlimited())), FluentFutureStream::unboxFluentFuture) .filter(CassandraMessageDAO.MessageResult::isFound) .map(CassandraMessageDAO.MessageResult::message) .map(pair -> pair.getKey().toMailboxMessage(ImmutableList.of())) .completableFuture(); }
@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); }