Package org.apache.groovy.runtime.async

# Class FlowPublisherAdapter

java.lang.Object
org.apache.groovy.runtime.async.FlowPublisherAdapter

All Implemented Interfaces:
  `AwaitableAdapter`

public final class FlowPublisherAdapter extends Object implements AwaitableAdapter
Adapter for `Flow.Publisher`, the JDK's built-in Reactive Streams type. Enables:

- `await publisher` — completes with the first `onNext` value, then cancels the subscription. Completes with `null` if the publisher signals `onComplete` without emitting.
- `for await (item in publisher)` — iterates over emitted values with bounded backpressure (see `DEFAULT_BATCH_SIZE`).

Conformance:

- Reactive Streams §2.13: `onNext(null)` is treated as a protocol violation and surfaced as a `NullPointerException`.
- A second `onSubscribe` after the first is cancelled.
- Signals after a terminal `onError`/`onComplete` are ignored.

This adapter is registered as the lowest-priority built-in (after SPI-loaded adapters) so framework-specific adapters (Reactor, RxJava) take precedence for their concrete types.

Since:
  6.0.0

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final int`
  `DEFAULT_BATCH_SIZE`
  Default request batch size for `for await` iteration.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `FlowPublisherAdapter()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `supportsAwaitable(Class<?> type)`
  Returns `true` if this adapter can convert instances of the given type to `Awaitable` (single-value async result).
  `boolean`
  `supportsIterable(Class<?> type)`
  Returns `true` if this adapter can convert instances of the given type to an `Iterable` for `for await` loops.
  `<T> Awaitable<T>`
  `toAwaitable(Object source)`
  Converts the given source object to an `Awaitable`.
  `<T> Iterable<T>`
  `toIterable(Object source)`
  Converts the given source object to an `Iterable`.

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

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

- ## Field Details

  - ### DEFAULT_BATCH_SIZE

    public static final int DEFAULT_BATCH_SIZE
    Default request batch size for `for await` iteration. Chosen as a compromise between throughput (larger = fewer `request()` calls) and memory (larger = bigger in-flight buffer). Override per-call by wrapping with a custom adapter if needed.
    See Also:
      - Constant Field Values

- ## Constructor Details

  - ### FlowPublisherAdapter

    public FlowPublisherAdapter()

- ## Method Details

  - ### supportsAwaitable

    public boolean supportsAwaitable(Class<?> type)
    Description copied from interface: `AwaitableAdapter`
    Returns `true` if this adapter can convert instances of the given type to `Awaitable` (single-value async result).
    Specified by:
      `supportsAwaitable` in interface `AwaitableAdapter`

  - ### supportsIterable

    public boolean supportsIterable(Class<?> type)
    Description copied from interface: `AwaitableAdapter`
    Returns `true` if this adapter can convert instances of the given type to an `Iterable` for `for await` loops. Defaults to `false`; override for multi-value async types (e.g., Reactor `Flux`, RxJava `Observable`).
    Specified by:
      `supportsIterable` in interface `AwaitableAdapter`

  - ### toAwaitable

    public <T> Awaitable<T> toAwaitable(Object source)
    Description copied from interface: `AwaitableAdapter`
    Converts the given source object to an `Awaitable`. Called only when `AwaitableAdapter.supportsAwaitable(java.lang.Class<?>)` returned `true`.
    Specified by:
      `toAwaitable` in interface `AwaitableAdapter`

  - ### toIterable

    public <T> Iterable<T> toIterable(Object source)
    Description copied from interface: `AwaitableAdapter`
    Converts the given source object to an `Iterable`. Called only when `AwaitableAdapter.supportsIterable(java.lang.Class<?>)` returned `true`. The returned iterable typically blocks on `next()` until the next element is available — with virtual threads this is efficient.
    Specified by:
      `toIterable` in interface `AwaitableAdapter`