Package nl.colorize.util
Class Subject<T>
java.lang.Object
nl.colorize.util.Subject<T>
- Type Parameters:
T
- The type of event that can be subscribed to.
- All Implemented Interfaces:
Flow.Publisher<T>
Publishes (possibly asynchronous) events to registered subscribers. This
class implements the Flow API.
It can be used on all platforms supported by this library, including
TeaVM. The term "subject" originates from
the original Gang of Four's description of the
observer pattern.
Subject
instances are thread-safe and can be accessed from different
threads. This facilitates workflows where publishers and subscribers operate
on different threads.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Creates and registers aFlow.Subscriber
that collects received events into the specified queue.void
complete()
Marks thisSubject
as completed, meaning that no new events or errors will be published to subscribers.static <T> Flow.Subscriber
<T> createSubscriber
(Consumer<T> eventCallback) Returns an implementation of theFlow.Subscriber
interface that implementsFlow.Subscriber.onNext(Object)
using a callback method.static <T> Flow.Subscriber
<T> createSubscriber
(Consumer<T> eventFn, Consumer<Throwable> errorFn) Returns an implementation of theFlow.Subscriber
interface that implementsFlow.Subscriber.onNext(Object)
andFlow.Subscriber.onError(Throwable)
using callback methods.Returns a newSubject
that will forward events to its own subscribers, but only if the event matches the specified predicate.<S> Subject
<S> Returns a newSubject
that will forward events to its own subscribers, but first uses the specified mapping function on each event.void
Performs the specified operation and publishes the resulting event to subscribers.void
Publishes the next event to all event subscribers.void
Publishes the next error to all error subscribers.static <T> Subject
<T> Creates aSubject
that will publish the specified values to its subscribers.static <T> Subject
<T> of
(T... values) Creates aSubject
that will publish the specified values to its subscribers.void
Attempts to perform an operation for the specified number of attempts, automatically retrying the operation if the initial attempt(s) failed.void
Attempts to perform an operation for the specified number of attempts, automatically retrying the operation if the initial attempt(s) failed.static <T> Subject
<T> Performs the specified operation and returns aSubject
that can be used to subscribe to the results.static <T> Subject
<T> runBackground
(Callable<T> operation) Performs the specified operation in a new background thread, and returns aSubject
that can be used to subscribe to the background operation.void
subscribe
(Flow.Subscriber<? super T> subscriber) Registers the specified subscriber to receive published events and errors.Registers the specified callback function as a subscriber for events.Registers the specified callback functions as event and error subscribers.subscribeErrors
(Consumer<Throwable> onError) Registers the specified callback function as an error subscriber.void
unsubscribe
(Flow.Subscriber<? super T> subscriber) Deprecated.void
unsubscribe
(Flow.Subscription subscription) Deprecated.Prefer usingFlow.Subscription.cancel()
instead.
-
Constructor Details
-
Subject
public Subject()
-
-
Method Details
-
next
Publishes the next event to all event subscribers. This method does nothing if thisSubject
has already been marked as completed. -
nextError
Publishes the next error to all error subscribers. If no error subscribers have been registered, this will invoke the default error handler that will print a log messsage describing the unhandled error. This method does nothing if thisSubject
has already been marked as completed. -
next
Performs the specified operation and publishes the resulting event to subscribers. If the operation completes normally, the return value is published to subscribers as an event. If an exception occurs during the operation, this exception is published to error subscribers. Does nothing if thisSubject
has already been marked as completed. -
retry
Attempts to perform an operation for the specified number of attempts, automatically retrying the operation if the initial attempt(s) failed. Noteattempts
includes the original attempt, so the number of retries is basicallyattempts - 1
.If the operation is not successful, an error is sent to subscribers based on the last failed attempt.
-
retry
Attempts to perform an operation for the specified number of attempts, automatically retrying the operation if the initial attempt(s) failed. The specified time delay (in milliseconds) is applied in between attempts. Noteattempts
includes the original attempt, so the number of retries is basicallyattempts - 1
.If the operation is not successful, an error is sent to subscribers based on the last failed attempt.
-
subscribe
Registers the specified subscriber to receive published events and errors. The subscriber will immediately be notified of previously published events.- Specified by:
subscribe
in interfaceFlow.Publisher<T>
-
subscribe
Registers the specified callback function as a subscriber for events. The subscriber will log errors, but not explicitly handle them. Returns aFlow.Subscription
for the registered subscriber. -
subscribe
Registers the specified callback functions as event and error subscribers. The subscribers will immediately be notified of previously published events and/or errors. Returns aFlow.Subscription
for the registered subscriber. -
subscribeErrors
Registers the specified callback function as an error subscriber. The subscriber will immediately be notified of previously published errors. Returns aFlow.Subscription
for the registered subscriber. -
subscribe
Subscribes the specified otherSubject
to listen for both events and errors generated by thisSubject
. This effectively means that events published by this instance will be forwarded tosubscriber
's subscribers. Returns aFlow.Subscription
for the registered subscriber. -
collect
Creates and registers aFlow.Subscriber
that collects received events into the specified queue. Consumers can then poll this queue to periodically process events. -
unsubscribe
Deprecated.Prefer usingFlow.Subscription.cancel()
instead.Unsubscribes the specified subscription. If the subscriber is not currently registered with thisSubject
, this method does nothing. -
unsubscribe
Deprecated.Prefer usingFlow.Subscription.cancel()
instead.Removes a previously registered subscriber. If the subscriber is not currently registered with thisSubject
, this method does nothing. -
complete
public void complete()Marks thisSubject
as completed, meaning that no new events or errors will be published to subscribers. However, old events might still be published when additional subscribers are registered. -
map
Returns a newSubject
that will forward events to its own subscribers, but first uses the specified mapping function on each event. Errors will be forwarded as-is. -
filter
Returns a newSubject
that will forward events to its own subscribers, but only if the event matches the specified predicate. Errors will be forwarded as-is. -
of
Creates aSubject
that will publish the specified values to its subscribers. -
of
Creates aSubject
that will publish the specified values to its subscribers. -
run
Performs the specified operation and returns aSubject
that can be used to subscribe to the results. -
runBackground
Performs the specified operation in a new background thread, and returns aSubject
that can be used to subscribe to the background operation. -
createSubscriber
Returns an implementation of theFlow.Subscriber
interface that implementsFlow.Subscriber.onNext(Object)
using a callback method. Errors will be logged, all other methods use no-op implementations. -
createSubscriber
public static <T> Flow.Subscriber<T> createSubscriber(Consumer<T> eventFn, Consumer<Throwable> errorFn) Returns an implementation of theFlow.Subscriber
interface that implementsFlow.Subscriber.onNext(Object)
andFlow.Subscriber.onError(Throwable)
using callback methods. The other methods use no-op implementations.
-
Flow.Subscription.cancel()
instead.