Package org.apache.groovy.runtime.async

# Class DefaultAsyncChannel<T>

java.lang.Object
org.apache.groovy.runtime.async.DefaultAsyncChannel<T>

Type Parameters:
  `T` - the payload type

All Implemented Interfaces:
  `AsyncChannel<T>`, `Iterable<T>`

public final class DefaultAsyncChannel<T> extends Object implements AsyncChannel<T>
Default lock-based implementation of `AsyncChannel`.

Uses a `ReentrantLock` to coordinate access to the internal buffer and the waiting-sender/waiting-receiver queues. All operations return `Awaitable` immediately; the underlying `CompletableFuture` is completed asynchronously when matching counterparts arrive.

Since:
  6.0.0
See Also:
  - `AsyncChannel`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `DefaultAsyncChannel()`
  `DefaultAsyncChannel(int capacity)`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `close()`
  Closes this channel.
  `int`
  `getBufferedSize()`
  Returns the number of values currently buffered.
  `int`
  `getCapacity()`
  Returns this channel's buffer capacity.
  `boolean`
  `isClosed()`
  Returns `true` if this channel has been closed.
  `Iterator<T>`
  `iterator()`
  Returns a blocking iterator that receives values until the channel is closed and drained.
  `Awaitable<T>`
  `receive()`
  Receives the next value from this channel.
  `Awaitable<Void>`
  `send(T value)`
  Sends a value through this channel.
  `String`
  `toString()`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

  ### Methods inherited from interface groovy.concurrent.AsyncChannel

  `filter, map, merge, split, tap`

  ### Methods inherited from interface java.lang.Iterable

  `forEach, spliterator`

- ## Constructor Details

  - ### DefaultAsyncChannel

    public DefaultAsyncChannel()

  - ### DefaultAsyncChannel

    public DefaultAsyncChannel(int capacity)

- ## Method Details

  - ### getCapacity

    public int getCapacity()
    Description copied from interface: `AsyncChannel`
    Returns this channel's buffer capacity.
    Specified by:
      `getCapacity` in interface `AsyncChannel<T>`

  - ### getBufferedSize

    public int getBufferedSize()
    Description copied from interface: `AsyncChannel`
    Returns the number of values currently buffered.
    Specified by:
      `getBufferedSize` in interface `AsyncChannel<T>`

  - ### isClosed

    public boolean isClosed()
    Description copied from interface: `AsyncChannel`
    Returns `true` if this channel has been closed.
    Specified by:
      `isClosed` in interface `AsyncChannel<T>`

  - ### send

    public Awaitable<Void> send(T value)
    Description copied from interface: `AsyncChannel`
    Sends a value through this channel.

    The returned `Awaitable` completes when the value has been delivered to a receiver or buffered. Sending to a closed channel fails immediately with `ChannelClosedException`.

    Specified by:
      `send` in interface `AsyncChannel<T>`
    Parameters:
      `value` - the value to send; must not be `null`
    Returns:
      an Awaitable that completes when the send succeeds

  - ### receive

    public Awaitable<T> receive()
    Description copied from interface: `AsyncChannel`
    Receives the next value from this channel.

    The returned `Awaitable` completes when a value is available. Receiving from a closed, empty channel fails with `ChannelClosedException`.

    Specified by:
      `receive` in interface `AsyncChannel<T>`
    Returns:
      an Awaitable that yields the next value

  - ### close

    public boolean close()
    Description copied from interface: `AsyncChannel`
    Closes this channel. Idempotent.

    Buffered values remain receivable. Pending senders fail with `ChannelClosedException`. After all buffered values are drained, subsequent receives also fail.

    Specified by:
      `close` in interface `AsyncChannel<T>`
    Returns:
      `true` if this call actually closed the channel

  - ### iterator

    public Iterator<T> iterator()
    Returns a blocking iterator that receives values until the channel is closed and drained. Each `next()` call blocks until a value is available. `ChannelClosedException` signals end-of-iteration.
    Specified by:
      `iterator` in interface `Iterable<T>`

  - ### toString

    public String toString()
    Overrides:
      `toString` in class `Object`