Class Loop<T>
- java.lang.Object
-
- org.obrel.core.RelatedObject
-
- de.esoco.coroutine.CoroutineStep<T,T>
-
- de.esoco.coroutine.step.Loop<T>
-
- All Implemented Interfaces:
de.esoco.lib.property.Fluent<CoroutineStep<T,T>>,org.obrel.core.FluentRelatable<CoroutineStep<T,T>>,org.obrel.core.Relatable
public class Loop<T> extends CoroutineStep<T,T>
ACoroutinestep that loops over another step (which may be a subroutine) as long as a predicate yields TRUE for the input value and/or the current continuation. The looped step get's the loop input value as it's input on the first iteration and must return a value of the same type which will then be used to test the condition before the next loop run.If more complex conditions need to be checked the loop condition can check relations that have been set in the
Continuationby the looped step.
-
-
Constructor Summary
Constructors Constructor Description Loop(java.util.function.BiPredicate<? super T,Continuation<?>> fCondition, CoroutineStep<T,T> rLoopedStep)Creates a new instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected Texecute(T input, Continuation<?> continuation)This method must be implemented by subclasses to provide the actual functionality of this step.static <T> Loop<T>loopWhile(java.util.function.BiPredicate<? super T,Continuation<?>> pCondition, CoroutineStep<T,T> rLoopedStep)Repeatedly executes a certain step as long as the given condition is TRUE for the current value and the continuation of the execution.static <T> Loop<T>loopWhile(java.util.function.Predicate<T> pCondition, CoroutineStep<T,T> rLoopedStep)Repeatedly executes a certain step as long as the given condition is TRUE for the current value.voidrunAsync(java.util.concurrent.CompletableFuture<T> previousExecution, CoroutineStep<T,?> 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
-
Loop
public Loop(java.util.function.BiPredicate<? super T,Continuation<?>> fCondition, CoroutineStep<T,T> rLoopedStep)
Creates a new instance.- Parameters:
fCondition- The condition to check for TRUE to continue loopingrLoopedStep- The step to execute in the loop
-
-
Method Detail
-
loopWhile
public static <T> Loop<T> loopWhile(java.util.function.BiPredicate<? super T,Continuation<?>> pCondition, CoroutineStep<T,T> rLoopedStep)
Repeatedly executes a certain step as long as the given condition is TRUE for the current value and the continuation of the execution. The current value is initialized from the loop input value and updated by the looped step on each iteration.- Parameters:
pCondition- The condition to check for TRUE to continue loopingrLoopedStep- The step to execute in the loop- Returns:
- A new step instance
-
loopWhile
public static <T> Loop<T> loopWhile(java.util.function.Predicate<T> pCondition, CoroutineStep<T,T> rLoopedStep)
Repeatedly executes a certain step as long as the given condition is TRUE for the current value. The current value is initialized from the loop input value and updated by the looped step on each iteration.If more complex conditions need to be checked the method
loopWhile(BiPredicate, CoroutineStep)can be used to check relations that have been set in theContinuationby the looped step.- Parameters:
pCondition- The condition to check for TRUE to continue loopingrLoopedStep- The step to execute in the loop- Returns:
- A new step instance
-
runAsync
public void runAsync(java.util.concurrent.CompletableFuture<T> previousExecution, CoroutineStep<T,?> 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).- Overrides:
runAsyncin classCoroutineStep<T,T>- Parameters:
previousExecution- The future of the previous code executionnextStep- The next step to execute or NULL for nonecontinuation- The continuation of the execution
-
execute
protected T execute(T input, Continuation<?> continuation)
This method must be implemented by subclasses to provide the actual functionality of this step.- Specified by:
executein classCoroutineStep<T,T>- Parameters:
input- The input valuecontinuation- The continuation of the execution- Returns:
- The result of the execution
-
-