Class SingleSubscriberHolder<T>

  • Type Parameters:
    T - the type of the Subscriber

    public class SingleSubscriberHolder<T>
    extends Object
    A subscriber container that accepts only a single, one-time subscriber registration.
    • Constructor Detail

      • SingleSubscriberHolder

        public SingleSubscriberHolder()
    • Method Detail

      • register

        public boolean register​(Flow.Subscriber<? super T> subscriber)
        Register a new subscriber.

        In case the subscriber registration fails (e.g. the holder already holds a previously registered subscriber or the holder has been closed), the newly registered subscriber is notified about the error by invoking it's subscriber.onError method.

        Parameters:
        subscriber - subscriber to be registered in the holder.
        Returns:
        true if the subscriber was successfully registered, false otherwise.
      • close

        public void close​(Consumer<Flow.Subscriber<? super T>> completionHandler)
        Mark the subscriber holder as closed.

        Invoking this method will ensure that any new attempts to register a new subscriber would fail.

        In case this holder holds a subscriber and the close method has not been invoked yet, the supplied completionHandler is invoked using the value of the registered subscriber as an input parameter. This means that the supplied completion handler is guaranteed to be invoked at most once.

        Parameters:
        completionHandler - completion handler to be invoked to tryProcess any completion logic on a registered subscriber, provided there is a registered subscriber and it has not been previously passed to a completion handler (e.g. in a previous invocation of this method).
      • cancel

        public void cancel()
        Hard cancel - nothing is sent to the subscriber but subscription is considered as canceled.
      • get

        public Flow.Subscriber<? super T> get()
                                       throws InterruptedException,
                                              ExecutionException
        Get the stored subscriber.

        This method blocks indefinitely until a subscriber is registered or the holder has been closed.

        Returns:
        registered subscriber.
        Throws:
        ExecutionException - if the subscriber retrieval has failed (e.g. because this holder has been closed before a subscriber has been registered).
        InterruptedException - if the current thread was interrupted
      • isClosed

        public boolean isClosed()
        Check if this subscriber holder has been closed.
        Returns:
        true if the holder is closed, false otherwise.