Package io.javago

Class BufferedQueueChannel<T>

java.lang.Object
io.javago.BufferedQueueChannel<T>
Type Parameters:
T - the type of messages handled by the channel
All Implemented Interfaces:
Channel<T>, InputChannel<T>, OutputChannel<T>, AutoCloseable, Iterable<T>

public class BufferedQueueChannel<T> extends Object implements Channel<T>
The BufferedQueueChannel class is an implementation of the Channel interface, providing a Go channel backed by a Queue for passing messages between threads. It supports both sending and receiving messages with a specified capacity.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a BufferedQueueChannel with a default capacity of 1.
    BufferedQueueChannel(int capacity)
    Constructs a BufferedQueueChannel with the specified capacity.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes the channel.
    boolean
    Waits until the channel has another message or is closed.
    boolean
    Waits until the channel has space for another message or is closed.
    boolean
    Checks if the channel is closed.
    boolean
    Checks if the channel is empty.
    boolean
    Checks if the channel is full.
    Returns an iterator over the elements in this channel.
    Receives a message from the channel.
    void
    send(T message)
    Sends a message through the channel.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Constructor Details

    • BufferedQueueChannel

      public BufferedQueueChannel()
      Constructs a BufferedQueueChannel with a default capacity of 1.
    • BufferedQueueChannel

      public BufferedQueueChannel(int capacity)
      Constructs a BufferedQueueChannel with the specified capacity.
      Parameters:
      capacity - the capacity of the channel
      Throws:
      IllegalArgumentException - if capacity is less than or equal to 0
  • Method Details

    • send

      public void send(T message)
      Sends a message through the channel. If the channel is full, this method blocks until space becomes available. Returns immediately if it is interrupted while blocking.
      Specified by:
      send in interface Channel<T>
      Specified by:
      send in interface OutputChannel<T>
      Parameters:
      message - the message to be sent
      Throws:
      IllegalStateException - if the channel is closed
    • receive

      public T receive()
      Receives a message from the channel. If the channel is empty, this method blocks until a message becomes available. Returns immediately if it is interrupted while blocking.
      Specified by:
      receive in interface Channel<T>
      Specified by:
      receive in interface InputChannel<T>
      Returns:
      the received message
      Throws:
      NoSuchElementException - if the channel is closed and empty
    • isClosed

      public boolean isClosed()
      Checks if the channel is closed.
      Specified by:
      isClosed in interface Channel<T>
      Specified by:
      isClosed in interface InputChannel<T>
      Specified by:
      isClosed in interface OutputChannel<T>
      Returns:
      true if the channel is closed, false otherwise
    • isEmpty

      public boolean isEmpty()
      Checks if the channel is empty.
      Specified by:
      isEmpty in interface Channel<T>
      Specified by:
      isEmpty in interface InputChannel<T>
      Specified by:
      isEmpty in interface OutputChannel<T>
      Returns:
      true if the channel is empty, false otherwise
    • isFull

      public boolean isFull()
      Checks if the channel is full.
      Specified by:
      isFull in interface Channel<T>
      Specified by:
      isFull in interface InputChannel<T>
      Specified by:
      isFull in interface OutputChannel<T>
      Returns:
      true if the channel is full, false otherwise
    • close

      public void close()
      Closes the channel. Once closed, no more messages can be sent, but any remaining messages can still be received. Closing an already closed channel has no effect.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Channel<T>
      Specified by:
      close in interface InputChannel<T>
      Specified by:
      close in interface OutputChannel<T>
    • iterator

      public Iterator<T> iterator()
      Returns an iterator over the elements in this channel.
      Specified by:
      iterator in interface Iterable<T>
      Returns:
      an Iterator over the elements in this channel
    • hasSpace

      public boolean hasSpace()
      Waits until the channel has space for another message or is closed. Returns immediately if it is interrupted while blocking.
      Specified by:
      hasSpace in interface Channel<T>
      Specified by:
      hasSpace in interface InputChannel<T>
      Specified by:
      hasSpace in interface OutputChannel<T>
      Returns:
      true if the channel has space, false if the channel is closed
    • hasNext

      public boolean hasNext()
      Waits until the channel has another message or is closed. Returns immediately if it is interrupted while blocking.
      Specified by:
      hasNext in interface Channel<T>
      Specified by:
      hasNext in interface InputChannel<T>
      Specified by:
      hasNext in interface OutputChannel<T>
      Returns:
      true if there are more messages, false if the channel is empty and closed