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 provides an implementation of a publisher in the
Flow API.
It can be used on all platforms supported by this library, including
TeaVM.
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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidcomplete()Marks thisSubjectas completed, meaning that no new events or errors will be published to subscribers.Returns aSubjectthat forwards events to its own subscribers, though only events that match the specified predicate.<S> Subject<S> Returns aSubjectthat forwards events to its own subscribers, first applying the specified mapper function to each event.<S> Subject<S> Returns aSubjectthat forwards events to its own subscribers, first applying the specified mapper function to each event.voidPerforms the specified operation and publishes the resulting event to subscribers.voidPublishes the next event to all event subscribers.voidPublishes the next error to all error subscribers.static <T> Subject<T> Creates aSubjectthat will immediately publish the specified values to its subscribers.static <T> Subject<T> of(T... values) Creates aSubjectthat will immediately publish the specified values to its subscribers.static <T> Subject<T> Performs the specified operation in the current thread, returns aSubjectthat can be used to subscribe to its results.static <T> Subject<T> Performs the specified operation in a new thread, returns aSubjectthat can be used to subscribe to its results.voidsubscribe(Flow.Subscriber<? super T> subscriber) Registers the specified subscriber to receive published events and errors.Registers a subscriber that will invoke the specified callback functions on events.Registers a subscriber that will invoke the specified callback functions on events and errors, respectively.Registers a subscriber that will invoke the specified callback functions on events, errors, and completion, respectively.subscribeErrors(Consumer<Throwable> onError) Registers a subscriber that will invoke the specified callback functions on errors.
-
Constructor Details
-
Subject
public Subject()
-
-
Method Details
-
next
-
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 thisSubjecthas 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 thisSubjecthas already been marked as completed. -
subscribe
Registers the specified subscriber to receive published events and errors. If there are currently undelivered events or errors, the new subscriber will immediately be notified of them.- Specified by:
subscribein interfaceFlow.Publisher<T>
-
subscribe
public Flow.Subscription subscribe(Consumer<T> onEvent, Consumer<Throwable> onError, Runnable onComplete) Registers a subscriber that will invoke the specified callback functions on events, errors, and completion, respectively. The new subscriber will immediately be notified of any undelivered events.- Returns:
- A
Flow.Subscriptionfor the registered subscriber.
-
subscribe
Registers a subscriber that will invoke the specified callback functions on events and errors, respectively. The new subscriber will immediately be notified of any undelivered events.- Returns:
- A
Flow.Subscriptionfor the registered subscriber.
-
subscribe
Registers a subscriber that will invoke the specified callback functions on events. The new subscriber will immediately be notified of any undelivered events.- Returns:
- A
Flow.Subscriptionfor the registered subscriber.
-
subscribeErrors
Registers a subscriber that will invoke the specified callback functions on errors. The new subscriber will immediately be notified of any undelivered errors.- Returns:
- A
Flow.Subscriptionfor the registered subscriber.
-
subscribe
Subscribes the specified otherSubjectto 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:
- A
Flow.Subscriptionfor the registered subscriber.
-
complete
public void complete()Marks thisSubjectas completed, meaning that no new events or errors will be published to subscribers. -
map
Returns aSubjectthat forwards events to its own subscribers, first applying the specified mapper function to each event. Intended as the equivalent ofStream.map(Function)for asynchronous events. -
flatMap
-
filter
-
of
Creates aSubjectthat will immediately publish the specified values to its subscribers. -
of
-
run
-
runAsync
-