Class Iteration<T,R,I extends java.lang.Iterable<T>,C extends java.util.Collection<R>>
- java.lang.Object
-
- org.obrel.core.RelatedObject
-
- de.esoco.coroutine.CoroutineStep<I,C>
-
- de.esoco.coroutine.step.Iteration<T,R,I,C>
-
- 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 anIterableinput 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), andcollectEachInto(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 anIterableinput value, processes each element with another coroutine step, and collects the resulting values into a target collection.protected Cexecute(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 anIterableinput value and processes each element with another coroutine step.voidrunAsync(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 aCompletableFutureand proceeds to the next step afterwards.-
Methods inherited from class de.esoco.coroutine.CoroutineStep
runBlocking, terminateCoroutine, toString
-
Methods inherited from class org.obrel.core.RelatedObject
deleteRelation, get, getRelation, getRelations, notifyRelationListeners, readRelations, relationsEqual, relationsHashCode, relationsString, set, set, transform, writeRelations
-
-
-
-
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 collectionrProcessingStep- The step to be applied to each value returned by the iterator
-
-
Method Detail
-
collectEach
public 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.- See Also:
collectEachInto(Supplier, CoroutineStep)
-
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 anIterableinput 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 inrProcessingStep- 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 anIterableinput 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 valuescollectEachInto(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 aCompletableFutureand 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
Suspensionby invokingContinuation.suspend(CoroutineStep, CoroutineStep)on the next step. If the condition that caused the suspension resolves the coroutine execution can be resumed by callingSuspension.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).
-
execute
protected C execute(I input, Continuation<?> continuation)
This method must be implemented by subclasses to provide the actual functionality of this step.
-
-