use of com.oath.cyclops.internal.react.stream.EagerStreamWrapper 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()));
}
use of com.oath.cyclops.internal.react.stream.EagerStreamWrapper in project cyclops by aol.
the class SimpleReactStream method reverse.
/**
* Reversed, operating on the underlying futures.
* <pre>
* {@code
*
* // (3, 2, 1) EagerFutureStream.of(1, 2, 3).reverse()
* }</pre>
*
* @return
*/
default SimpleReactStream<U> reverse() {
final EagerStreamWrapper lastActive = getLastActive();
final ListIterator<CompletableFuture> it = lastActive.list().listIterator();
final List<CompletableFuture> result = new ArrayList<>();
while (it.hasPrevious()) result.add(it.previous());
final EagerStreamWrapper limited = lastActive.withList(result);
return this.withLastActive(limited);
}
use of com.oath.cyclops.internal.react.stream.EagerStreamWrapper in project cyclops by aol.
the class SimpleReactStream method skip.
/**
* In contast to EagerFutureStream#skip skipFutures will skip the first n entries
* of the underlying Stream of Futures.
* <pre>
* {@code
* EagerFutureStream.of(()>loadSlow(),()>loadMedium(),()>loadFast())
* .skip(2)
* }
*
* //[loadFast]
* </pre>
*
* @param n
* @return
*/
@Override
default SimpleReactStream<U> skip(final long n) {
final EagerStreamWrapper lastActive = getLastActive();
final EagerStreamWrapper limited = lastActive.withList(lastActive.stream().skip(n).collect(Collectors.toList()));
return this.withLastActive(limited);
}
use of com.oath.cyclops.internal.react.stream.EagerStreamWrapper in project cyclops by aol.
the class SimpleReactStream method anyOf.
/**
* React to the completion of any of the events in the previous stage. Will not work reliably with Streams
* where filter has been applied in earlier stages. (As Filter completes the Stream for events that are filtered out, they
* potentially shortcircuit the completion of the stage).
*
* @param fn Function to applyHKT when any of the previous events complete
* @return Next stage in the stream
*/
default <R> SimpleReactStream<R> anyOf(final Function<? super U, ? extends R> fn) {
final CompletableFuture[] array = lastActiveArray(getLastActive());
final CompletableFuture cf = CompletableFuture.anyOf(array);
final CompletableFuture onSuccess = cf.thenApplyAsync(fn, getTaskExecutor());
return (SimpleReactStream<R>) withLastActive(new EagerStreamWrapper(onSuccess, getErrorHandler()));
}
use of com.oath.cyclops.internal.react.stream.EagerStreamWrapper in project cyclops by aol.
the class SimpleReactStream method limit.
/**
* Perform a limit operation on the underlying Stream of Futures
* In contrast to EagerFutureStream#limit this removes entries basaed on their
* skip position
*
* <pre>
* {@code
* EagerFutureStream.of(()>loadSlow(),()>loadMedium(),()>loadFast())
* .limitFutures(2)
* }
*
* //[loadSlow, loadMedium]
* </pre>
*
* @param maxSize The size of the subsequent Stream
* @return limited Stream
*/
default SimpleReactStream<U> limit(final long maxSize) {
final EagerStreamWrapper lastActive = getLastActive();
final EagerStreamWrapper limited = lastActive.withList(lastActive.stream().limit(maxSize).collect(Collectors.toList()));
return this.withLastActive(limited);
}
Aggregations