Package io.javago
Interface Channel<T>
- Type Parameters:
T- the type of messages handled by the channel
- All Superinterfaces:
AutoCloseable,InputChannel<T>,Iterable<T>,OutputChannel<T>
- All Known Implementing Classes:
BufferedQueueChannel
The
Channel interface defines the operations to create Go's channel in Java that can send and receive
messages of a specified type.
It extends AutoCloseable to support resource management and Iterable to allow for iteration over the
messages in the channel.-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the channel.booleanhasNext()Waits until the channel has another message.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.static <T> Channel<T> make()Creates a new channel with a default capacity.static <T> Channel<T> make(int capacity) Creates a new channel with the specified capacity.receive()Receives a message from the channel, waiting if necessary for a message to be sent.voidSends a message through the channel, waiting if necessary for space to become available.Methods inherited from interface java.lang.Iterable
forEach, iterator, spliterator
-
Method Details
-
send
Sends a message through the channel, waiting if necessary for space to become available.- Specified by:
sendin interfaceOutputChannel<T>- Parameters:
message- the message to be sent- Throws:
IllegalStateException- if the channel
-
receive
T receive()Receives a message from the channel, waiting if necessary for a message to be sent.- Specified by:
receivein interfaceInputChannel<T>- Returns:
- the received message
- Throws:
NoSuchElementException- if the channel is both closed and empty
-
isClosed
boolean isClosed()Checks if the channel is closed.- Specified by:
isClosedin interfaceInputChannel<T>- Specified by:
isClosedin interfaceOutputChannel<T>- Returns:
trueif the channel is closed,falseotherwise
-
close
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 interfaceInputChannel<T>- Specified by:
closein interfaceOutputChannel<T>
-
isEmpty
boolean isEmpty()Checks if the channel is empty.- Specified by:
isEmptyin interfaceInputChannel<T>- Specified by:
isEmptyin interfaceOutputChannel<T>- Returns:
trueif the channel is empty,falseotherwise
-
isFull
boolean isFull()Checks if the channel is full.- Specified by:
isFullin interfaceInputChannel<T>- Specified by:
isFullin interfaceOutputChannel<T>- Returns:
trueif the channel is full,falseotherwise
-
hasSpace
boolean hasSpace()Waits until the channel has space for another message or is closed.- Specified by:
hasSpacein interfaceInputChannel<T>- Specified by:
hasSpacein interfaceOutputChannel<T>- Returns:
trueif the channel has space,falseif the channel is closed
-
hasNext
boolean hasNext()Waits until the channel has another message.- Specified by:
hasNextin interfaceInputChannel<T>- Specified by:
hasNextin interfaceOutputChannel<T>- Returns:
trueif there are more messages,falseif the channel is both closed and empty.
-
make
Creates a new channel with a default capacity.- Type Parameters:
T- the type of messages handled by the channel- Returns:
- a new
Channelinstance
-
make
Creates a new channel with the specified capacity.- Type Parameters:
T- the type of messages handled by the channel- Parameters:
capacity- the capacity of the channel- Returns:
- a new
Channelinstance with the specified capacity
-