public class Continuation<T>
extends org.obrel.core.RelatedObject
implements java.util.concurrent.Executor
Coroutine.then(CoroutineStep)
gives the code of a step
access to the current continuation it is running in.
This class also implements the Future
interface and can therefore
be used like any other Java future. The only limitation is that due to the
cooperative concurrency of coroutines it is not possible to immediately
interrupt a coroutine execution. Therefore the boolean parameter of the
method cancel()
is ignored.
If a continuation has been cancelled all blocking Future
methods
will throw a CancellationException
after the wait lock is
removed.
Constructor and Description |
---|
Continuation(CoroutineScope rScope,
Coroutine<?,T> rCoroutine)
Creates a new instance for the execution of the given
Coroutine
in a certain scope. |
Modifier and Type | Method and Description |
---|---|
void |
await()
Awaits the completion of the coroutine execution.
|
boolean |
await(long nTimeout,
java.util.concurrent.TimeUnit eUnit)
Awaits the completion of this continuation but only until a timeout is
reached.
|
void |
cancel()
Cancels the execution of the associated
Coroutine at the next
suspension point. |
CoroutineContext |
context()
Returns the context of the executed coroutine.
|
void |
errorHandled()
Marks an error of this continuation as handled.
|
void |
execute(java.lang.Runnable rCommand)
Forwards the execution to the executor of the
CoroutineContext . |
java.lang.Void |
fail(java.lang.Throwable eError)
Signals that an error occurred during the coroutine execution.
|
<C> Channel<C> |
getChannel(ChannelId<C> rId)
Duplicated here for easier access during coroutine execution.
|
<V> V |
getConfiguration(org.obrel.core.RelationType<V> rConfigType)
Returns a configuration value with a default value of NULL.
|
<V> V |
getConfiguration(org.obrel.core.RelationType<V> rConfigType,
V rDefault)
Returns the value of a configuration relation.
|
Coroutine<?,?> |
getCurrentCoroutine()
Returns either the root coroutine or, if subroutines have been started
from it, the currently executing subroutine.
|
Suspension<?> |
getCurrentSuspension()
Returns the current suspension.
|
java.lang.Throwable |
getError()
Returns the error exception that caused a coroutine cancelation.
|
T |
getResult()
Return the result of the coroutine execution.
|
T |
getResult(long nTimeout,
java.util.concurrent.TimeUnit eUnit)
Return the result of the coroutine execution or throws a
CoroutineException if a timeout is reached. |
<V> V |
getState(org.obrel.core.RelationType<V> rStateType)
Returns a state value with a default value of NULL.
|
<V> V |
getState(org.obrel.core.RelationType<V> rStateType,
V rDefault)
Returns the value of a runtime state relation of the current execution.
|
long |
id()
Returns the unique ID of this instance.
|
boolean |
isCancelled()
Checks if the execution of the coroutine has been cancelled.
|
boolean |
isFinished()
Checks if the execution of the coroutine has finished.
|
Continuation<T> |
onCancel(java.util.function.Consumer<Continuation<T>> fRunOnCancel)
Sets a function to be run if the execution of this instance is cancelled.
|
Continuation<T> |
onError(java.util.function.Consumer<Continuation<T>> fRunOnError)
Sets a function to be run if the execution of this instance fails.
|
Continuation<T> |
onFinish(java.util.function.Consumer<Continuation<T>> fRunWhenDone)
Sets a function that will be invoked after the coroutine has successfully
finished execution and
finish(Object) has been invoked. |
CoroutineScope |
scope()
Returns the scope in which the coroutine is executed.
|
<V> Suspension<V> |
suspend(CoroutineStep<?,V> rSuspendingStep,
CoroutineStep<V,?> rSuspendedStep)
Suspends an invoking step for later invocation.
|
<V> Suspension<V> |
suspendTo(Suspension<V> rSuspension)
Suspends an invoking step for later invocation with the given instance of
a suspension subclass.
|
java.lang.String |
toString() |
deleteRelation, get, getRelation, getRelations, notifyRelationListeners, readRelations, relationsEqual, relationsHashCode, relationsString, set, set, transform, writeRelations
public Continuation(CoroutineScope rScope, Coroutine<?,T> rCoroutine)
Coroutine
in a certain scope.rScope
- The coroutine contextrCoroutine
- The coroutine that is executed with this continuationpublic void await()
getResult()
this will not throw an exception on failure or
cancellation. An application must check these by itself if needed through
getError()
and isCancelled()
.public boolean await(long nTimeout, java.util.concurrent.TimeUnit eUnit)
getResult(long, TimeUnit)
this will
not throw an exception on failure or cancellation. An application must
check these by itself if needed through getError()
and isCancelled()
.nTimeout
- The timeout valueeUnit
- The time unit of the the timeoutpublic void cancel()
Coroutine
at the next
suspension point. Due to the nature of the cooperative concurrency of
coroutines there is no guarantee as to when the cancellation will occur.
The bMayInterruptIfRunning parameter is ignored because the thread on
which the current step is running is not known.public final CoroutineContext context()
public void errorHandled()
java.lang.IllegalStateException
- If this instance has no errorpublic void execute(java.lang.Runnable rCommand)
CoroutineContext
.execute
in interface java.util.concurrent.Executor
Executor.execute(Runnable)
public java.lang.Void fail(java.lang.Throwable eError)
getError()
. It will also invoke CoroutineScope.fail(Continuation)
on the scope this continuation runs
in.eError
- The exception that caused the errorCompletableFuture.exceptionally(Function)
without the need to
return a (NULL) valuepublic final <C> Channel<C> getChannel(ChannelId<C> rId)
CoroutineScope.getChannel(ChannelId)
public <V> V getConfiguration(org.obrel.core.RelationType<V> rConfigType)
getConfiguration(RelationType, Object)
public <V> V getConfiguration(org.obrel.core.RelationType<V> rConfigType, V rDefault)
Coroutine steps that want to modify the configuration of the root
coroutine they are running in should set the configuration value on the
the continuation. To limit the change to the currently running coroutine
(e.g. a subroutine) configurations should be set on getCurrentCoroutine()
instead.
rConfigType
- The configuraton relation typerDefault
- The default value if no state relation existspublic final Coroutine<?,?> getCurrentCoroutine()
public final Suspension<?> getCurrentSuspension()
public java.lang.Throwable getError()
public T getResult()
CancellationException
will be thrown. If it has
failed with an error a CoroutineException
will be thrown.public T getResult(long nTimeout, java.util.concurrent.TimeUnit eUnit)
CoroutineException
if a timeout is reached. If this continuation has
been cancelled a CancellationException
will be thrown. If it has
failed with an error a CoroutineException
will be thrown.nTimeout
- The timeout valueeUnit
- The time unit of the the timeoutCoroutineException
- If the timeout has elapsed before finishing
or an error occurredjava.util.concurrent.CancellationException
- If the coroutine had been cancelledpublic <V> V getState(org.obrel.core.RelationType<V> rStateType)
getState(RelationType, Object)
public <V> V getState(org.obrel.core.RelationType<V> rStateType, V rDefault)
rStateType
- The state relation typerDefault
- The default value if no state relation existspublic final long id()
public boolean isCancelled()
getError()
will
return an exception.public boolean isFinished()
isCancelled()
. If it has been cancelled because of and error the method
getError()
will return an exception.public Continuation<T> onCancel(java.util.function.Consumer<Continuation<T>> fRunOnCancel)
fRunOnCancel
- A function to be run on cancellationpublic Continuation<T> onError(java.util.function.Consumer<Continuation<T>> fRunOnError)
fRunOnError
- A function to be run on cancellationpublic Continuation<T> onFinish(java.util.function.Consumer<Continuation<T>> fRunWhenDone)
finish(Object)
has been invoked. If the
execution of the coroutine is cancelled (by invoking cancel()
)
the code will not be invoked. The code will be run directly, not
asynchronously.fRunWhenDone
- The consumer to process this continuation with when
the execution has finishedpublic final CoroutineScope scope()
public <V> Suspension<V> suspend(CoroutineStep<?,V> rSuspendingStep, CoroutineStep<V,?> rSuspendedStep)
Suspension
that contains the state necessary for resuming the
execution. The suspension will not contain an input value because it is
typically not know upon suspension. It must be provided later, either
when resuming with Suspension.resume(Object)
or by setting it
into the suspension with Suspension.withValue(Object)
.rSuspendingStep
- The step initiating the suspensionrSuspendedStep
- The step to suspendpublic <V> Suspension<V> suspendTo(Suspension<V> rSuspension)
suspend(CoroutineStep, CoroutineStep)
instead.rSuspension
- The suspension to suspend topublic java.lang.String toString()
toString
in class org.obrel.core.RelatedObject