Class BufferedInputSource<I>

java.lang.Object
com.google.genkit.core.BufferedInputSource<I>
Type Parameters:
I - the type of each input element
All Implemented Interfaces:
InputSource<I>, AutoCloseable

public final class BufferedInputSource<I> extends Object implements InputSource<I>
A thread-safe InputSource that is fed by a producer via offer(I) and signals end-of-stream via end().

Internally uses a LinkedBlockingQueue of Optional<I> values. The end-of-stream sentinel is Optional.empty(). Once the sentinel has been consumed, every subsequent call to next() returns empty immediately.

Thread-safety: safe for one producer thread calling offer(I)/end() and one consumer thread calling next().

  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new BufferedInputSource.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    No-op; the queue needs no explicit resource release.
    void
    end()
    Signals end-of-stream.
    Blocks until the next input is available or the stream ends.
    void
    offer(I input)
    Enqueues one input for the consumer.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • BufferedInputSource

      public BufferedInputSource()
      Creates a new BufferedInputSource.
  • Method Details

    • offer

      public void offer(I input)
      Enqueues one input for the consumer. Must not be called after end().
      Parameters:
      input - the input to enqueue; must not be null
    • end

      public void end()
      Signals end-of-stream. After this call, next() will return Optional.empty() once all previously enqueued inputs have been consumed. This method is idempotent.
    • next

      public Optional<I> next() throws InterruptedException
      Blocks until the next input is available or the stream ends.

      Blocks until an input is available or end-of-stream is signalled. Once end-of-stream is reached, all subsequent calls return Optional.empty() without blocking.

      Specified by:
      next in interface InputSource<I>
      Returns:
      an Optional containing the next input, or Optional.empty() when the stream has ended
      Throws:
      InterruptedException - if the calling thread is interrupted while waiting
    • close

      public void close()
      No-op; the queue needs no explicit resource release.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface InputSource<I>