Package de.esoco.coroutine
Class CoroutineEnvironment
- java.lang.Object
-
- org.obrel.core.RelatedObject
-
- de.esoco.coroutine.CoroutineEnvironment
-
- All Implemented Interfaces:
org.obrel.core.Relatable
- Direct Known Subclasses:
CoroutineContext,CoroutineScope
public abstract class CoroutineEnvironment extends org.obrel.core.RelatedObjectA base class for environments to execute coroutines in. The main common property of environments is that they manage thechannelsfor the communication between coroutines. They allow to set relations for coroutine configuration and/or state.
-
-
Constructor Summary
Constructors Constructor Description CoroutineEnvironment()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> Channel<T>createChannel(ChannelId<T> id, int capacity)Creates a new channel with a certain capacity and stores it in this context for lookup withgetChannel(ChannelId).<T> Channel<T>getChannel(ChannelId<T> id)Returns a channel for a certain ID.booleanhasChannel(ChannelId<?> id)Checks whether this context contains a certain channel.voidremoveChannel(ChannelId<?> id)Removes a channel from this context.-
Methods inherited from class org.obrel.core.RelatedObject
deleteRelation, get, getRelation, getRelations, notifyRelationListeners, readRelations, relationsEqual, relationsHashCode, relationsString, set, set, toString, transform, writeRelations
-
-
-
-
Method Detail
-
createChannel
public <T> Channel<T> createChannel(ChannelId<T> id, int capacity)
Creates a new channel with a certain capacity and stores it in this context for lookup withgetChannel(ChannelId). If a channel with the given ID already exists an exception will be thrown to prevent accidental overwriting of channels.- Parameters:
id- The channel IDcapacity- The channel capacity- Returns:
- The new channel
- Throws:
java.lang.IllegalArgumentException- If a channel with the given ID already exists
-
getChannel
public <T> Channel<T> getChannel(ChannelId<T> id)
Returns a channel for a certain ID. If no such channel exists a new channel with a capacity of 1 (one) entry will be created and stored under the given ID. To prevent this the channel needs to be created in advance by callingcreateChannel(ChannelId, int).- Parameters:
id- The channel ID- Returns:
- The channel for the given ID
-
hasChannel
public boolean hasChannel(ChannelId<?> id)
Checks whether this context contains a certain channel. As with the other channel access methods the result depends on the current execution state of the coroutines in this context. It is recommended to only invoke this method if concurrent modification of the queries channel will not occur.- Parameters:
id- The channel ID- Returns:
- TRUE if the channel exists in this context
-
removeChannel
public void removeChannel(ChannelId<?> id)
Removes a channel from this context. This method should be applied with caution because concurrently running coroutines may try to access or event re-create the channel in parallel. It is recommended to invoke this method only on contexts without running coroutines that access the channel ID. Otherwise synchronization is necessary.- Parameters:
id- The channel ID
-
-