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 aBufferedQueueChannelwith a default capacity of 1.BufferedQueueChannel(int capacity) Constructs aBufferedQueueChannelwith the specified capacity. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the channel.booleanhasNext()Waits until the channel has another message or is closed.booleanhasSpace()Waits until the channel has space for another message or is closed.booleanisClosed()Checks if the channel is closed.booleanisEmpty()Checks if the channel is empty.booleanisFull()Checks if the channel is full.iterator()Returns an iterator over the elements in this channel.receive()Receives a message from the channel.voidSends a message through the channel.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
BufferedQueueChannel
public BufferedQueueChannel()Constructs aBufferedQueueChannelwith a default capacity of 1. -
BufferedQueueChannel
public BufferedQueueChannel(int capacity) Constructs aBufferedQueueChannelwith 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:
sendin interfaceChannel<T>- Specified by:
sendin 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:
receivein interfaceChannel<T>- Specified by:
receivein 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:
isClosedin interfaceChannel<T>- Specified by:
isClosedin interfaceInputChannel<T>- Specified by:
isClosedin interfaceOutputChannel<T>- Returns:
trueif the channel is closed,falseotherwise
-
isEmpty
public boolean isEmpty()Checks if the channel is empty.- Specified by:
isEmptyin interfaceChannel<T>- Specified by:
isEmptyin interfaceInputChannel<T>- Specified by:
isEmptyin interfaceOutputChannel<T>- Returns:
trueif the channel is empty,falseotherwise
-
isFull
public boolean isFull()Checks if the channel is full.- Specified by:
isFullin interfaceChannel<T>- Specified by:
isFullin interfaceInputChannel<T>- Specified by:
isFullin interfaceOutputChannel<T>- Returns:
trueif the channel is full,falseotherwise
-
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:
closein interfaceAutoCloseable- Specified by:
closein interfaceChannel<T>- Specified by:
closein interfaceInputChannel<T>- Specified by:
closein 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:
hasSpacein interfaceChannel<T>- Specified by:
hasSpacein interfaceInputChannel<T>- Specified by:
hasSpacein interfaceOutputChannel<T>- Returns:
trueif the channel has space,falseif 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:
hasNextin interfaceChannel<T>- Specified by:
hasNextin interfaceInputChannel<T>- Specified by:
hasNextin interfaceOutputChannel<T>- Returns:
trueif there are more messages,falseif the channel is empty and closed
-