Class AsyncFoldLeft


  • public class AsyncFoldLeft
    extends java.lang.Object

    Processes 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 for loop, 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 interface  AsyncFoldLeft.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 interface  AsyncFoldLeft.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>
      void
      run​(java.util.List<T> list, R zero, AsyncFoldLeft.Operation<T,​R> operation, Promise<R> promise)
      Processes the given list of elements.
      static <T,​R>
      void
      run​(T[] array, R zero, AsyncFoldLeft.Operation<T,​R> operation, Promise<R> promise)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • AsyncFoldLeft

        public AsyncFoldLeft()
    • Method Detail

      • run

        public static <T,​R> void run​(java.util.List<T> list,
                                           R zero,
                                           AsyncFoldLeft.Operation<T,​R> operation,
                                           Promise<R> promise)

        Processes the given list of elements.

        The initial result zero is returned if the list is empty.

        For each element the operation function is invoked.

        Type Parameters:
        T - the type of element
        R - the type of the result
        Parameters:
        list - the elements to process
        zero - the initial result
        operation - the operation to invoke for each element
        promise - the promise to notify of the final result