- Type Parameters:
T- The type of the values processed by the channel.
- All Implemented Interfaces:
CloseableChannel,Sink<T>,Source<T>
- send(Object)-ing a value to the channel. Values can't be null.
- receive()-ing a value from the channel
- closing the channel using done() or error(Throwable)
There are three channel flavors:
- rendezvous channels, where senders and receivers must meet to exchange values - buffered channels, where a given number of sent values might be buffered, before subsequent `send`s block - unlimited channels, where an unlimited number of values might be buffered, hence `send` never blocks
To create a channel, use newRendezvousChannel(), newBufferedChannel(int) and
newUnlimitedChannel() methods. Additionally, newBufferedDefaultChannel() creates
a buffered channel with a "default" capacity of 16, which should be a good starting point for most use-cases.
In a rendezvous channel, senders and receivers block, until a matching party arrives (unless one is already waiting). Similarly, buffered channels block if the buffer is full (in case of senders), or in case of receivers, if the buffer is empty and there are no waiting senders.
All blocking operations behave properly upon interruption.
Channels might be closed, either because no more values will be produced by the source (using
done()), or because there was an error while producing or processing the received values (using
error(Throwable)).
After closing, no more values can be sent to the channel. If the channel is "done", any pending sends will be completed normally. If the channel is in an "error" state, pending sends will be interrupted and will return with the reason for the closure.
In case the channel is closed, one of the ChannelClosedExceptions is thrown. Alternatively, you can call
the less type-safe, but more exception-safe sendOrClosed(Object) and receiveOrClosed()
methods, which do not throw in case the channel is closed, but return one of the ChannelClosed values.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionvoiddone()Close the channel, indicating that no more elements will be sent.Close the channel, indicating that no more elements will be sent.voidClose the channel, indicating an error.errorOrClosed(Throwable reason) Close the channel, indicating an error.static <T> Channel<T> newBufferedChannel(int capacity) static <T> Channel<T> Creates a new buffered channel, with the default buffer size (16).static <T> Channel<T> static <T> Channel<T> receive()Receive a value from the channel.Create a clause which can be used inSelect.select(SelectClause[]).<U> SelectClause<U> receiveClause(Function<T, U> callback) Create a clause which can be used inSelect.select(SelectClause[]).Receive a value from the channel.voidSend a value to the channel.sendClause(T value) Create a clause which can be used inSelect.select(SelectClause[]).<U> SelectClause<U> sendClause(T value, Supplier<U> callback) Create a clause which can be used inSelect.select(SelectClause[]).sendOrClosed(T value) Send a value to the channel.toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface com.softwaremill.jox.CloseableChannel
isClosedForReceive, isClosedForSendMethods inherited from interface com.softwaremill.jox.Source
collectAsView, filterAsView, forEach, toList
-
Field Details
-
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZE- See Also:
-
-
Method Details
-
newRendezvousChannel
-
newBufferedChannel
-
newBufferedDefaultChannel
Creates a new buffered channel, with the default buffer size (16). -
newUnlimitedChannel
-
send
Description copied from interface:SinkSend a value to the channel.- Specified by:
sendin interfaceSink<T>- Parameters:
value- The value to send. Notnull.- Throws:
InterruptedException
-
sendOrClosed
Description copied from interface:SinkSend a value to the channel. Doesn't throw exceptions when the channel is closed, but returns a value.- Specified by:
sendOrClosedin interfaceSink<T>- Parameters:
value- The value to send. Notnull.- Returns:
- Either
null, orChannelClosed, when the channel is closed. - Throws:
InterruptedException
-
receive
Description copied from interface:SourceReceive a value from the channel.- Specified by:
receivein interfaceSource<T>- Throws:
InterruptedException
-
receiveOrClosed
Description copied from interface:SourceReceive a value from the channel. Doesn't throw exceptions when the channel is closed, but returns a value.- Specified by:
receiveOrClosedin interfaceSource<T>- Returns:
- Either a value of type
T, orChannelClosed, when the channel is closed. - Throws:
InterruptedException
-
done
public void done()Description copied from interface:CloseableChannelClose the channel, indicating that no more elements will be sent.Any elements that are already buffered will be delivered. Any send operations that are in progress will complete normally, when a receiver arrives. Any pending receive operations will complete with a channel closed result.
Subsequent
Sink.send(Object)operations will throwChannelClosedException.- Specified by:
donein interfaceCloseableChannel
-
doneOrClosed
Description copied from interface:CloseableChannelClose the channel, indicating that no more elements will be sent. Doesn't throw exceptions when the channel is closed, but returns a value.Any elements that are already buffered will be delivered. Any send operations that are in progress will complete normally, when a receiver arrives. Any pending receive operations will complete with a channel closed result.
Subsequent
Sink.send(Object)operations will throwChannelClosedException.- Specified by:
doneOrClosedin interfaceCloseableChannel- Returns:
- Either
null, orChannelClosed, when the channel is already closed.
-
error
Description copied from interface:CloseableChannelClose the channel, indicating an error.Any elements that are already buffered won't be delivered. Any send or receive operations that are in progress will complete with a channel closed result.
Subsequent
Sink.send(Object)andSource.receive()operations will throwChannelClosedException.- Specified by:
errorin interfaceCloseableChannel- Parameters:
reason- The reason of the error. Notnull.
-
errorOrClosed
Description copied from interface:CloseableChannelClose the channel, indicating an error. Doesn't throw exceptions when the channel is closed, but returns a value.Any elements that are already buffered won't be delivered. Any send or receive operations that are in progress will complete with a channel closed result.
Subsequent
Sink.send(Object)andSource.receive()operations will throwChannelClosedException.- Specified by:
errorOrClosedin interfaceCloseableChannel- Returns:
- Either
null, orChannelClosed, when the channel is already closed.
-
closedForSend
- Specified by:
closedForSendin interfaceCloseableChannel- Returns:
- Non-
nullif no more values can be sent to this channel;Sink.send(Object)will throwChannelClosedExceptionor returnChannelClosed(in the or-closed variant).nullif the channel is not closed, and values can be sent.When closed for send, receiving using
Source.receive()might still be possible, if the channel is done, and not in an error. This can be verified usingCloseableChannel.isClosedForReceive().
-
closedForReceive
- Specified by:
closedForReceivein interfaceCloseableChannel- Returns:
- Non-
nullif no more values can be received from this channel;Source.receive()will throwChannelClosedExceptionor returnChannelClosed(in the or-closed variant).nullif the channel is not closed, and values can be received.When closed for receive, sending values is also not possible,
CloseableChannel.isClosedForSend()will returntrue.
-
receiveClause
Description copied from interface:SourceCreate a clause which can be used inSelect.select(SelectClause[]). The clause will receive a value from the current channel.- Specified by:
receiveClausein interfaceSource<T>
-
receiveClause
Description copied from interface:SourceCreate a clause which can be used inSelect.select(SelectClause[]). The clause will receive a value from the current channel, and transform it using the providedcallback.- Specified by:
receiveClausein interfaceSource<T>
-
sendClause
Description copied from interface:SinkCreate a clause which can be used inSelect.select(SelectClause[]). The clause will send the given value to the current channel, and returnnullas the clause's result.- Specified by:
sendClausein interfaceSink<T>
-
sendClause
Description copied from interface:SinkCreate a clause which can be used inSelect.select(SelectClause[]). The clause will send the given value to the current channel, and return the value of the provided callback as the clause's result.- Specified by:
sendClausein interfaceSink<T>
-
toString
-