java.lang.Object
org.bsc.async.internal.reactive.GeneratorSubscriber<T>
- Type Parameters:
T- The type of elements produced by this generator.
- All Implemented Interfaces:
Iterable<T>,Flow.Subscriber<T>,AsyncGenerator<T>,AsyncGenerator.Cancellable<T>,AsyncGenerator.IsCancellable
public class GeneratorSubscriber<T>
extends Object
implements AsyncGenerator.Cancellable<T>, Flow.Subscriber<T>
Represents a subscriber for generating asynchronous data streams.
This class implements the Flow.Subscriber and AsyncGenerator interfaces to handle data flow
and produce asynchronous data. It is designed to subscribe to a publisher, process incoming items,
and manage error and completion signals.
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.bsc.async.AsyncGenerator
AsyncGenerator.Base<E>, AsyncGenerator.BaseCancellable<E>, AsyncGenerator.Cancellable<E>, AsyncGenerator.Data<E>, AsyncGenerator.Embed<E>, AsyncGenerator.EmbedCompletionHandler, AsyncGenerator.HasResultValue, AsyncGenerator.IsCancellable, AsyncGenerator.WithEmbed<E>, AsyncGenerator.WithResult<E> -
Field Summary
Fields inherited from interface org.bsc.async.AsyncGenerator.IsCancellable
CANCELLED -
Constructor Summary
ConstructorsConstructorDescriptionGeneratorSubscriber(P publisher, BlockingQueue<AsyncGenerator.Data<T>> queue) Constructs a new instance ofGeneratorSubscriber.GeneratorSubscriber(P publisher, Supplier<Object> mapResult, BlockingQueue<AsyncGenerator.Data<T>> queue) Constructs a new instance ofGeneratorSubscriber. -
Method Summary
Modifier and TypeMethodDescriptionbooleancancel(boolean mayInterruptIfRunning) method that request to cancel generatorfinal Executorexecutor()booleanChecks if the asynchronous generation has been cancelled.next()Returns the nextData<T>object from this iteration.voidThis method is called when the asynchronous operation is completed successfully.voidHandles an error by queuing it in the delegate's queue with an errored data.voidPasses the received item to the delegated queue as anAsyncGenerator.Dataobject.voidonSubscribe(Flow.Subscription subscription) Handles the subscription event from a Flux.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.bsc.async.AsyncGenerator
flatMap, forEachAsync, iterator, map, reduce, reduceAsync, stream, toCompletableFutureMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
GeneratorSubscriber
public GeneratorSubscriber(P publisher, Supplier<Object> mapResult, BlockingQueue<AsyncGenerator.Data<T>> queue) Constructs a new instance ofGeneratorSubscriber.- Type Parameters:
P- the type of the publisher, which must extendFlow.Publisher- Parameters:
mapResult- function that will set generator's resultpublisher- the source publisher that will push data to this subscriberqueue- the blocking queue used for storing asynchronous generator data
-
GeneratorSubscriber
Constructs a new instance ofGeneratorSubscriber.- Type Parameters:
P- the type of the publisher, which must extendFlow.Publisher- Parameters:
publisher- the source publisher that will push data to this subscriberqueue- the blocking queue used for storing asynchronous generator data
-
-
Method Details
-
mapResult
-
onSubscribe
Handles the subscription event from a Flux.This method is called when a subscription to the source
Flowhas been established. The providedFlow.Subscriptioncan be used to manage and control the flow of data emissions.- Specified by:
onSubscribein interfaceFlow.Subscriber<T>- Parameters:
subscription- The subscription object representing this resource owner lifecycle. Used to signal that resources being subscribed to should not be released until this subscription is disposed.
-
onNext
Passes the received item to the delegated queue as anAsyncGenerator.Dataobject.- Specified by:
onNextin interfaceFlow.Subscriber<T>- Parameters:
item- The item to be processed and queued.
-
onError
Handles an error by queuing it in the delegate's queue with an errored data.- Specified by:
onErrorin interfaceFlow.Subscriber<T>- Parameters:
error- The Throwable that represents the error to be handled.
-
onComplete
public void onComplete()This method is called when the asynchronous operation is completed successfully. It notifies the delegate that no more data will be provided by adding a done marker to the queue.- Specified by:
onCompletein interfaceFlow.Subscriber<T>
-
next
Returns the nextData<T>object from this iteration.- Specified by:
nextin interfaceAsyncGenerator<T>- Returns:
- the next element in the iteration, or null if there is no such element
-
executor
- Specified by:
executorin interfaceAsyncGenerator<T>
-
isCancelled
public boolean isCancelled()Description copied from interface:AsyncGenerator.IsCancellableChecks if the asynchronous generation has been cancelled.The default implementation always returns
false. Implementations that support cancellation should override this method.- Specified by:
isCancelledin interfaceAsyncGenerator.IsCancellable- Returns:
trueif the generator has been cancelled,falseotherwise.
-
cancel
public boolean cancel(boolean mayInterruptIfRunning) Description copied from interface:AsyncGenerator.IsCancellablemethod that request to cancel generator- Specified by:
cancelin interfaceAsyncGenerator.IsCancellable
-