Class Iteration<T,​R,​I extends java.lang.Iterable<T>,​C extends java.util.Collection<R>>

  • All Implemented Interfaces:
    de.esoco.lib.property.Fluent<CoroutineStep<I,​C>>, org.obrel.core.FluentRelatable<CoroutineStep<I,​C>>, org.obrel.core.Relatable

    public class Iteration<T,​R,​I extends java.lang.Iterable<T>,​C extends java.util.Collection<R>>
    extends CoroutineStep<I,​C>
    A step that implements suspendable iteration over an Iterable input value. Each value returned by the iterator will be processed with a separate execution of a certain coroutine step (which may be a subroutine).

    The static factory methods forEach(CoroutineStep), collectEach(CoroutineStep), and collectEachInto(Supplier, CoroutineStep) create instances that either discard or collect the results of applying the iteration step.

    • Constructor Summary

      Constructors 
      Constructor Description
      Iteration​(java.util.function.Supplier<C> fCollectionFactory, CoroutineStep<T,​R> rProcessingStep)
      Creates a new instance.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static <T,​R,​I extends java.lang.Iterable<T>>
      CoroutineStep<I,​java.util.List<R>>
      collectEach​(CoroutineStep<T,​R> rProcessingStep)
      Collects all processed elements into a list.
      static <T,​R,​I extends java.lang.Iterable<T>,​C extends java.util.Collection<R>>
      CoroutineStep<I,​C>
      collectEachInto​(java.util.function.Supplier<C> fCollectionFactory, CoroutineStep<T,​R> rProcessingStep)
      Iterates over the elements in an Iterable input value, processes each element with another coroutine step, and collects the resulting values into a target collection.
      protected C execute​(I input, Continuation<?> continuation)
      This method must be implemented by subclasses to provide the actual functionality of this step.
      static <T,​I extends java.lang.Iterable<T>>
      CoroutineStep<I,​java.lang.Void>
      forEach​(CoroutineStep<T,​?> rProcessingStep)
      Iterates over the elements in an Iterable input value and processes each element with another coroutine step.
      void runAsync​(java.util.concurrent.CompletableFuture<I> previousExecution, CoroutineStep<C,​?> nextStep, Continuation<?> continuation)
      Runs this execution step asynchronously as a continuation of a previous code execution in a CompletableFuture and proceeds to the next step afterwards.
      • Methods inherited from class org.obrel.core.RelatedObject

        deleteRelation, get, getRelation, getRelations, notifyRelationListeners, readRelations, relationsEqual, relationsHashCode, relationsString, set, set, transform, writeRelations
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface de.esoco.lib.property.Fluent

        _with
      • Methods inherited from interface org.obrel.core.FluentRelatable

        with, with, with
      • Methods inherited from interface org.obrel.core.Relatable

        deleteRelation, deleteRelation, deleteRelations, get, getAll, getOption, getRelation, getRelationCount, getRelations, getRelations, hasFlag, hasRelation, hasRelations, init, set, set, set, set, set, setOption, streamRelations, transform
    • Constructor Detail

      • Iteration

        public Iteration​(java.util.function.Supplier<C> fCollectionFactory,
                         CoroutineStep<T,​R> rProcessingStep)
        Creates a new instance.
        Parameters:
        fCollectionFactory - A supplier that returns a collection of the target type to store the processed values in or NULL for no collection
        rProcessingStep - The step to be applied to each value returned by the iterator
    • Method Detail

      • collectEachInto

        public static <T,​R,​I extends java.lang.Iterable<T>,​C extends java.util.Collection<R>> CoroutineStep<I,​C> collectEachInto​(java.util.function.Supplier<C> fCollectionFactory,
                                                                                                                                                         CoroutineStep<T,​R> rProcessingStep)
        Iterates over the elements in an Iterable input value, processes each element with another coroutine step, and collects the resulting values into a target collection. If invoked asynchronously each iteration will be invoked as a separate suspension, but values are still processed sequentially. After the iteration has completed the coroutine continues with the next step with the collected values as it's input.
        Parameters:
        fCollectionFactory - A supplier that returns a collection of the target type to store the processed values in
        rProcessingStep - The step to process each value
        Returns:
        A new step instance
      • forEach

        public static <T,​I extends java.lang.Iterable<T>> CoroutineStep<I,​java.lang.Void> forEach​(CoroutineStep<T,​?> rProcessingStep)
        Iterates over the elements in an Iterable input value and processes each element with another coroutine step. The processed values will be discarded, the returned step will always have a result of NULL. To collect the processed values collectEachInto(Supplier, CoroutineStep) can be used instead.
        Parameters:
        rProcessingStep - The step to process each value
        Returns:
        A new step instance
      • runAsync

        public void runAsync​(java.util.concurrent.CompletableFuture<I> previousExecution,
                             CoroutineStep<C,​?> nextStep,
                             Continuation<?> continuation)
        Runs this execution step asynchronously as a continuation of a previous code execution in a CompletableFuture and proceeds to the next step afterwards.

        Subclasses that need to suspend the invocation of the next step until some condition is met (e.g. sending or receiving data has finished) need to override this method and create a Suspension by invoking Continuation.suspend(CoroutineStep, CoroutineStep) on the next step. If the condition that caused the suspension resolves the coroutine execution can be resumed by calling Suspension.resume(Object).

        Subclasses that override this method also need to handle errors by terminating any further execution (i.e. not resuming a suspension if such exists) and forwarding the causing exception to Continuation.fail(Throwable).

        Overrides:
        runAsync in class CoroutineStep<I extends java.lang.Iterable<T>,​C extends java.util.Collection<R>>
        Parameters:
        previousExecution - The future of the previous code execution
        nextStep - The next step to execute or NULL for none
        continuation - The continuation of the execution
      • execute

        protected C execute​(I input,
                            Continuation<?> continuation)
        This method must be implemented by subclasses to provide the actual functionality of this step.
        Specified by:
        execute in class CoroutineStep<I extends java.lang.Iterable<T>,​C extends java.util.Collection<R>>
        Parameters:
        input - The input value
        continuation - The continuation of the execution
        Returns:
        The result of the execution