Class CoroutineEnvironment

  • All Implemented Interfaces:
    org.obrel.core.Relatable
    Direct Known Subclasses:
    CoroutineContext, CoroutineScope

    public abstract class CoroutineEnvironment
    extends org.obrel.core.RelatedObject
    A base class for environments to execute coroutines in. The main common property of environments is that they manage the channels for the communication between coroutines. They allow to set relations for coroutine configuration and/or state.
    • 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 with getChannel(ChannelId).
      <T> Channel<T> getChannel​(ChannelId<T> id)
      Returns a channel for a certain ID.
      boolean hasChannel​(ChannelId<?> id)
      Checks whether this context contains a certain channel.
      void removeChannel​(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
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface org.obrel.core.Relatable

        deleteRelation, deleteRelations, getAll, getOption, getRelationCount, getRelations, hasFlag, hasRelation, hasRelations, init, set, set, set, setOption, streamRelations
    • Constructor Detail

      • CoroutineEnvironment

        public CoroutineEnvironment()
    • 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 with getChannel(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 ID
        capacity - 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 calling createChannel(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