use of com.oath.cyclops.react.StageWithResults in project cyclops by aol.
the class SimpleReactStream method allOf.
/**
* @param collector
* to perform aggregation / reduction operation on the results
* from active stage (e.g. to Collect into a List or String)
* @param fn
* Function that receives the results of all currently active
* tasks as input
* @return A new builder object that can be used to define the next stage in
* the dataflow
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
default <R1, R2> SimpleReactStream<R2> allOf(final Collector<? super U, ?, R1> collector, final Function<? super R1, ? extends R2> fn) {
final CompletableFuture[] array = lastActiveArray(getLastActive());
final CompletableFuture cf = CompletableFuture.allOf(array);
final Function<Exception, R2> f = (final Exception e) -> {
BlockingStreamHelper.capture(e, getErrorHandler());
return BlockingStreamHelper.block(this, Collectors.toList(), new EagerStreamWrapper(Stream.of(array), getErrorHandler()));
};
final CompletableFuture onFail = cf.exceptionally(f);
final CompletableFuture onSuccess = onFail.thenApplyAsync((result) -> {
return new StageWithResults(getTaskExecutor(), null, result).submit(() -> fn.apply(BlockingStreamHelper.aggregateResultsCompletable(collector, Stream.of(array).collect(Collectors.toList()), getErrorHandler())));
}, getTaskExecutor());
return (SimpleReactStream<R2>) withLastActive(new EagerStreamWrapper(onSuccess, getErrorHandler()));
}
Aggregations