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>
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
ConstructorsConstructorDescriptionConstructs aBufferedQueueChannel
with a default capacity of 1.BufferedQueueChannel
(int capacity) Constructs aBufferedQueueChannel
with the specified capacity. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the channel.boolean
hasNext()
Waits until the channel has another message or is closed.boolean
hasSpace()
Waits until the channel has space for another message or is closed.boolean
isClosed()
Checks if the channel is closed.boolean
isEmpty()
Checks if the channel is empty.boolean
isFull()
Checks if the channel is full.iterator()
Returns an iterator over the elements in this channel.receive()
Receives a message from the channel.void
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 aBufferedQueueChannel
with a default capacity of 1. -
BufferedQueueChannel
public BufferedQueueChannel(int capacity) Constructs aBufferedQueueChannel
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
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 interfaceChannel<T>
- Specified by:
send
in interfaceOutputChannel<T>
- Parameters:
message
- the message to be sent- Throws:
IllegalStateException
- if the channel is closed
-
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 interfaceChannel<T>
- Specified by:
receive
in interfaceInputChannel<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 interfaceChannel<T>
- Specified by:
isClosed
in interfaceInputChannel<T>
- Specified by:
isClosed
in interfaceOutputChannel<T>
- Returns:
true
if the channel is closed,false
otherwise
-
isEmpty
public boolean isEmpty()Checks if the channel is empty.- Specified by:
isEmpty
in interfaceChannel<T>
- Specified by:
isEmpty
in interfaceInputChannel<T>
- Specified by:
isEmpty
in interfaceOutputChannel<T>
- Returns:
true
if the channel is empty,false
otherwise
-
isFull
public boolean isFull()Checks if the channel is full.- Specified by:
isFull
in interfaceChannel<T>
- Specified by:
isFull
in interfaceInputChannel<T>
- Specified by:
isFull
in interfaceOutputChannel<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 interfaceAutoCloseable
- Specified by:
close
in interfaceChannel<T>
- Specified by:
close
in interfaceInputChannel<T>
- Specified by:
close
in interfaceOutputChannel<T>
-
iterator
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 interfaceChannel<T>
- Specified by:
hasSpace
in interfaceInputChannel<T>
- Specified by:
hasSpace
in interfaceOutputChannel<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 interfaceChannel<T>
- Specified by:
hasNext
in interfaceInputChannel<T>
- Specified by:
hasNext
in interfaceOutputChannel<T>
- Returns:
true
if there are more messages,false
if the channel is empty and closed
-