Package de.esoco.coroutine.step
Class ChannelReceive<T>
- java.lang.Object
-
- org.obrel.core.RelatedObject
-
- de.esoco.coroutine.CoroutineStep<I,O>
-
- de.esoco.coroutine.step.ChannelStep<java.lang.Void,T>
-
- de.esoco.coroutine.step.ChannelReceive<T>
-
- All Implemented Interfaces:
de.esoco.lib.property.Fluent<CoroutineStep<java.lang.Void,T>>,org.obrel.core.FluentRelatable<CoroutineStep<java.lang.Void,T>>,org.obrel.core.Relatable
public class ChannelReceive<T> extends ChannelStep<java.lang.Void,T>
A coroutine step that receives a value from a channel. If the channel is empty at the time of this step's invocation the coroutine execution will be suspended until channel data becomes available. The channel to receive from will be queried (and if not existing created) withCoroutineScope.getChannel(ChannelId).
-
-
Constructor Summary
Constructors Constructor Description ChannelReceive(java.util.function.Function<Continuation<?>,ChannelId<T>> fGetChannelId)Creates a new instance that receives from a channel the ID of which is provided in a state relation.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected Texecute(java.lang.Void input, Continuation<?> continuation)This method must be implemented by subclasses to provide the actual functionality of this step.static <T> ChannelReceive<T>receive(ChannelId<T> rId)Suspends until a value can be received from a certain channel.static <T> ChannelReceive<T>receive(java.util.function.Function<Continuation<?>,ChannelId<T>> fGetChannelId)Suspends until a value can be received from the channel with the ID provided by the given function.voidrunAsync(java.util.concurrent.CompletableFuture<java.lang.Void> 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.step.ChannelStep
getChannel
-
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
-
ChannelReceive
public ChannelReceive(java.util.function.Function<Continuation<?>,ChannelId<T>> fGetChannelId)
Creates a new instance that receives from a channel the ID of which is provided in a state relation.- Parameters:
fGetChannelId- The function that will return the channel ID
-
-
Method Detail
-
receive
public static <T> ChannelReceive<T> receive(ChannelId<T> rId)
Suspends until a value can be received from a certain channel.- Parameters:
rId- The ID of the channel to receive from- Returns:
- A new instance of this class
-
receive
public static <T> ChannelReceive<T> receive(java.util.function.Function<Continuation<?>,ChannelId<T>> fGetChannelId)
Suspends until a value can be received from the channel with the ID provided by the given function.- Parameters:
fGetChannelId- The function that will return the channel ID- Returns:
- A new step instance
-
runAsync
public void runAsync(java.util.concurrent.CompletableFuture<java.lang.Void> 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<java.lang.Void,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(java.lang.Void input, Continuation<?> continuation)
This method must be implemented by subclasses to provide the actual functionality of this step.- Specified by:
executein classCoroutineStep<java.lang.Void,T>- Parameters:
input- The input valuecontinuation- The continuation of the execution- Returns:
- The result of the execution
-
-