Package de.esoco.coroutine
Class Suspension<T>
- java.lang.Object
-
- de.esoco.coroutine.Suspension<T>
-
- Direct Known Subclasses:
Selection
public class Suspension<T> extends java.lang.Object
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedSuspension(CoroutineStep<?,T> suspendingStep, CoroutineStep<T,?> resumeStep, Continuation<?> continuation)Creates a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcancel()Cancels this suspension.Continuation<?>continuation()Returns the continuation of the suspended coroutine.voidfail(java.lang.Throwable error)Cancels this suspension because of an error.voidifNotCancelled(java.lang.Runnable code)Executes code only if this suspension has not (yet) been cancel.booleanisCancelled()Checks if the this suspension has been cancelled.voidonCancel(de.esoco.lib.expression.monad.Option<java.lang.Runnable> cancelHandler)Sets an optional handler to be invoked if this suspension is cancelled.voidresume()Resumes the execution of the suspended coroutine with the input value provided to the constructor.voidresume(T value)Resumes the execution of the suspended coroutine with the given value.CoroutineStep<?,T>suspendingStep()Returns the suspending step.java.lang.StringtoString()Tvalue()Returns the value of this suspension.Suspension<T>withValue(T value)Sets the suspension value and returns this instance so that it can be used as an updated argument to method calls.
-
-
-
Constructor Detail
-
Suspension
protected Suspension(CoroutineStep<?,T> suspendingStep, CoroutineStep<T,?> resumeStep, Continuation<?> continuation)
Creates a new instance. The input value for the resume step is not provided here because it is typically not available upon suspension because it will only become available when the suspension is resumed (e.g. when receiving data). To resume execution with an explicit input value the methodresume(Object)can be used. If the resume should occur at a different time than the availability of the input value a suspension can be updated by callingwithValue(Object). In that caseresume()can be used later to resume the execution.- Parameters:
suspendingStep- The step that initiated the suspensionresumeStep- The step to resume the execution withcontinuation- The continuation of the execution
-
-
Method Detail
-
cancel
public void cancel()
Cancels this suspension. This willcancelthe continuation. Tries to resume a cancelled suspension will be ignored. If a cancel handler has been registered withonCancel(Option)it will be invoked.
-
continuation
public final Continuation<?> continuation()
Returns the continuation of the suspended coroutine.- Returns:
- The continuation
-
fail
public void fail(java.lang.Throwable error)
Cancels this suspension because of an error. This willfailthe continuation. Tries to resume a failed suspension will be ignored.- Parameters:
error- The error exception
-
ifNotCancelled
public void ifNotCancelled(java.lang.Runnable code)
Executes code only if this suspension has not (yet) been cancel. The given code will be executed with a lock on the cancelation state to prevent race conditions if other threads try to cancel a suspension while it is resumed.- Parameters:
code- The code to execute only if this suspension is not cancelled
-
isCancelled
public final boolean isCancelled()
Checks if the this suspension has been cancelled.- Returns:
- TRUE if the suspension has been cancelled
-
onCancel
public void onCancel(de.esoco.lib.expression.monad.Option<java.lang.Runnable> cancelHandler)
Sets an optional handler to be invoked if this suspension is cancelled. Only one handler at a time is supported, setting a new one will replace any previously registered handler.- Parameters:
cancelHandler- The optional cancel handler
-
resume
public final void resume()
Resumes the execution of the suspended coroutine with the input value provided to the constructor.- See Also:
resume(Object)
-
resume
public void resume(T value)
Resumes the execution of the suspended coroutine with the given value.- Parameters:
value- The input value to the resumed step
-
suspendingStep
public final CoroutineStep<?,T> suspendingStep()
Returns the suspending step.- Returns:
- The suspending step
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
value
public final T value()
Returns the value of this suspension. The value will be used as the input of the resumed step.- Returns:
- The suspension value
-
withValue
public Suspension<T> withValue(T value)
Sets the suspension value and returns this instance so that it can be used as an updated argument to method calls.- Parameters:
value- The new value- Returns:
- This instance
-
-