Interface CloseableChannel

All Known Subinterfaces:
Sink<T>, Source<T>
All Known Implementing Classes:
Channel

public interface CloseableChannel
A channel which can be closed.

A channel can be closed in two ways:

A channel can be closed only once. Subsequent calls to done() or error(Throwable) will throw ChannelClosedException, or return the original closing reason (when using doneOrClosed() or errorOrClosed(Throwable)).

Closing the channel is thread-safe.

  • Method Details

    • done

      void done()
      Close 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 throw ChannelClosedException.

      Throws:
      ChannelClosedException - When the channel is already closed.
    • doneOrClosed

      Object doneOrClosed()
      Close 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 throw ChannelClosedException.

      Returns:
      Either null, or ChannelClosed, when the channel is already closed.
    • error

      void error(Throwable reason)
      Close 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) and Source.receive() operations will throw ChannelClosedException.

      Parameters:
      reason - The reason of the error. Not null.
      Throws:
      ChannelClosedException - When the channel is already closed.
    • errorOrClosed

      Object errorOrClosed(Throwable reason)
      Close 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) and Source.receive() operations will throw ChannelClosedException.

      Returns:
      Either null, or ChannelClosed, when the channel is already closed.
    • isClosedForSend

      default boolean isClosedForSend()
      Returns:
      true if no more values can be sent to this channel; Sink.send(Object) will throw ChannelClosedException or return ChannelClosed (in the or-closed variant).

      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 using isClosedForReceive().

    • isClosedForReceive

      default boolean isClosedForReceive()
      Returns:
      true if no more values can be received from this channel; Source.receive() will throw ChannelClosedException or return ChannelClosed (in the or-closed variant).

      When closed for receive, sending values is also not possible, isClosedForSend() will return true.

      When false, values might be received from the channel, when calling Source.receive(), but it's not guaranteed that some values will be available. They might be received concurrently.

    • closedForSend

      ChannelClosed closedForSend()
      Returns:
      Non-null if no more values can be sent to this channel; Sink.send(Object) will throw ChannelClosedException or return ChannelClosed (in the or-closed variant).

      null if 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 using isClosedForReceive().

    • closedForReceive

      ChannelClosed closedForReceive()
      Returns:
      Non-null if no more values can be received from this channel; Source.receive() will throw ChannelClosedException or return ChannelClosed (in the or-closed variant).

      null if the channel is not closed, and values can be received.

      When closed for receive, sending values is also not possible, isClosedForSend() will return true.