Package com.google.genkit.core
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
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 -
Method Summary
-
Constructor Details
-
BufferedInputSource
public BufferedInputSource()Creates a newBufferedInputSource.
-
-
Method Details
-
offer
Enqueues one input for the consumer. Must not be called afterend().- Parameters:
input- the input to enqueue; must not benull
-
end
public void end()Signals end-of-stream. After this call,next()will returnOptional.empty()once all previously enqueued inputs have been consumed. This method is idempotent. -
next
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:
nextin interfaceInputSource<I>- Returns:
- an
Optionalcontaining the next input, orOptional.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:
closein interfaceAutoCloseable- Specified by:
closein interfaceInputSource<I>
-