Package de.esoco.coroutine.step
Class Delay<T>
- java.lang.Object
-
- org.obrel.core.RelatedObject
-
- de.esoco.coroutine.CoroutineStep<T,T>
-
- de.esoco.coroutine.step.Delay<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 Delay<T> extends CoroutineStep<T,T>
A suspendingCoroutinestep that performs delayed executions.
-
-
Constructor Summary
Constructors Constructor Description Delay(java.util.function.Function<Continuation<?>,de.esoco.lib.datatype.Pair<java.lang.Long,java.util.concurrent.TimeUnit>> getDuration)Creates a new instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Texecute(T input, Continuation<?> continuation)This method must be implemented by subclasses to provide the actual functionality of this step.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.static <T> Delay<T>sleep(long milliseconds)Suspends the coroutine execution for a certain duration in milliseconds.static <T> Delay<T>sleep(long duration, java.util.concurrent.TimeUnit timeUnit)Suspends the coroutine execution for a certain duration.static <T> Delay<T>sleep(java.util.function.Function<Continuation<?>,de.esoco.lib.datatype.Pair<java.lang.Long,java.util.concurrent.TimeUnit>> getDuration)Suspends the coroutine execution for a duration stored in a certain state relation.-
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
-
Delay
public Delay(java.util.function.Function<Continuation<?>,de.esoco.lib.datatype.Pair<java.lang.Long,java.util.concurrent.TimeUnit>> getDuration)
Creates a new instance.- Parameters:
getDuration- A function that determines the duration to sleep from the continuation
-
-
Method Detail
-
sleep
public static <T> Delay<T> sleep(java.util.function.Function<Continuation<?>,de.esoco.lib.datatype.Pair<java.lang.Long,java.util.concurrent.TimeUnit>> getDuration)
Suspends the coroutine execution for a duration stored in a certain state relation. The lookup of the duration value follows the rules defined byContinuation.getState(RelationType, Object).- Parameters:
getDuration- A function that determines the duration to sleep from the continuation- Returns:
- A new step instance
-
sleep
public static <T> Delay<T> sleep(long milliseconds)
Suspends the coroutine execution for a certain duration in milliseconds.- Parameters:
milliseconds- The milliseconds to sleep- See Also:
sleep(long, TimeUnit)
-
sleep
public static <T> Delay<T> sleep(long duration, java.util.concurrent.TimeUnit timeUnit)
Suspends the coroutine execution for a certain duration.- Parameters:
duration- The duration to sleeptimeUnit- The time unit of the duration- Returns:
- A new step instance
-
execute
public T execute(T input, Continuation<?> continuation)
Description copied from class:CoroutineStepThis 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
-
runAsync
public void runAsync(java.util.concurrent.CompletableFuture<T> previousExecution, CoroutineStep<T,?> nextStep, Continuation<?> continuation)
Description copied from class:CoroutineStepRuns 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
-
-