Search in sources :

Example 1 with StageWithResults

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()));
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) EagerStreamWrapper(com.oath.cyclops.internal.react.stream.EagerStreamWrapper) StageWithResults(com.oath.cyclops.react.StageWithResults) FilteredExecutionPathException(com.oath.cyclops.internal.react.exceptions.FilteredExecutionPathException) SimpleReactFailedStageException(com.oath.cyclops.react.SimpleReactFailedStageException) CompletionException(java.util.concurrent.CompletionException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

FilteredExecutionPathException (com.oath.cyclops.internal.react.exceptions.FilteredExecutionPathException)1 EagerStreamWrapper (com.oath.cyclops.internal.react.stream.EagerStreamWrapper)1 SimpleReactFailedStageException (com.oath.cyclops.react.SimpleReactFailedStageException)1 StageWithResults (com.oath.cyclops.react.StageWithResults)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionException (java.util.concurrent.CompletionException)1 ExecutionException (java.util.concurrent.ExecutionException)1