Class AsyncFoldLeft
- java.lang.Object
-
- org.cometd.common.AsyncFoldLeft
-
public class AsyncFoldLeft extends java.lang.ObjectProcesses asynchronously a sequence of elements, producing a result that is the reduction of the results produced by the processing of each element.
This class implements the asynchronous version of the following synchronous
forloop, but where the processing of each element may be asynchronous.R result; for (T element : list) { (result, proceed) = operation.apply(result, element); if (!proceed) { break; } }Using this class, the loop above becomes:
R zero; AsyncFoldLeft.run(list, zero, (result, element, loop) -> { CompletableFuture<R> future = processAsync(element); future.whenComplete((r, x) -> { if (x == null) { R reduced = reduce(result, r); if (shouldIterate(r)) { loop.proceed(reduced); } else { loop.leave(reduced); } } else { loop.fail(x); } }) }, Promise.complete((r, x) -> { // Process final result or failure. });
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceAsyncFoldLeft.Loop<R>Controls the iteration over a sequence of elements, allowing to continue the iteration (with a result), break out of the iteration (with a result), or fail the iteration (with an exception).static interfaceAsyncFoldLeft.Operation<T,R>The operation to invoke for each element.
-
Constructor Summary
Constructors Constructor Description AsyncFoldLeft()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T,R>
voidreverseRun(java.util.List<T> list, R zero, AsyncFoldLeft.Operation<T,R> operation, Promise<R> promise)Processes, in reverse order, the given sequence of elements.static <T,R>
voidrun(java.lang.Iterable<T> iterable, R zero, AsyncFoldLeft.Operation<T,R> operation, Promise<R> promise)Processes the given sequence of elements.static <T,R>
voidrun(T[] array, R zero, AsyncFoldLeft.Operation<T,R> operation, Promise<R> promise)Processes the given array of elements.
-
-
-
Method Detail
-
run
public static <T,R> void run(T[] array, R zero, AsyncFoldLeft.Operation<T,R> operation, Promise<R> promise)Processes the given array of elements.
- Type Parameters:
T- the type of elementR- the type of the result- Parameters:
array- the elements to processzero- the initial resultoperation- the operation to invoke for each elementpromise- the promise to notify of the final result- See Also:
run(Iterable, Object, Operation, Promise)
-
run
public static <T,R> void run(java.lang.Iterable<T> iterable, R zero, AsyncFoldLeft.Operation<T,R> operation, Promise<R> promise)Processes the given sequence of elements.
The initial result
zerois returned if the sequence is empty.For each element the
operationfunction is invoked.The sequence should have a "stable" iterator, i.e. it should not be affected if the sequence is modified during the iteration, either concurrently by another thread, or by the same thread in the
operationfunction.- Type Parameters:
T- the type of elementR- the type of the result- Parameters:
iterable- the elements to processzero- the initial resultoperation- the operation to invoke for each elementpromise- the promise to notify of the final result
-
reverseRun
public static <T,R> void reverseRun(java.util.List<T> list, R zero, AsyncFoldLeft.Operation<T,R> operation, Promise<R> promise)Processes, in reverse order, the given sequence of elements.
- Type Parameters:
T- the type of elementR- the type of the result- Parameters:
list- the elements to processzero- the initial resultoperation- the operation to invoke for each elementpromise- the promise to notify of the final result- See Also:
run(Iterable, Object, Operation, Promise)
-
-