Class ScalarSubscription<T>

  • Type Parameters:
    T - the value type
    All Implemented Interfaces:
    QueueFuseable<T>, QueueSubscription<T>, SimpleQueue<T>, java.io.Serializable, org.reactivestreams.Subscription

    public final class ScalarSubscription<T>
    extends java.util.concurrent.atomic.AtomicInteger
    implements QueueSubscription<T>
    A Subscription that holds a constant value and emits it only when requested.
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      (package private) static int CANCELLED
      Cancel has been called.
      (package private) static int NO_REQUEST
      No request has been issued yet.
      (package private) static int REQUESTED
      Request has been called.
      private static long serialVersionUID  
      (package private) org.reactivestreams.Subscriber<? super T> subscriber
      The actual subscriber.
      (package private) T value
      The single value to emit, set to null.
    • Constructor Summary

      Constructors 
      Constructor Description
      ScalarSubscription​(org.reactivestreams.Subscriber<? super T> subscriber, T value)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void cancel()  
      void clear()
      Removes all enqueued items from this queue.
      boolean isCancelled()
      Returns true if this Subscription was cancelled.
      boolean isEmpty()
      Returns true if the queue is empty.
      boolean offer​(T e)
      Atomically enqueue a single value.
      boolean offer​(T v1, T v2)
      Atomically enqueue two values.
      T poll()
      Tries to dequeue a value (non-null) or returns null if the queue is empty.
      void request​(long n)  
      int requestFusion​(int mode)
      Request a fusion mode from the upstream.
      • Methods inherited from class java.util.concurrent.atomic.AtomicInteger

        accumulateAndGet, addAndGet, compareAndExchange, compareAndExchangeAcquire, compareAndExchangeRelease, compareAndSet, decrementAndGet, doubleValue, floatValue, get, getAcquire, getAndAccumulate, getAndAdd, getAndDecrement, getAndIncrement, getAndSet, getAndUpdate, getOpaque, getPlain, incrementAndGet, intValue, lazySet, longValue, set, setOpaque, setPlain, setRelease, toString, updateAndGet, weakCompareAndSet, weakCompareAndSetAcquire, weakCompareAndSetPlain, weakCompareAndSetRelease, weakCompareAndSetVolatile
      • Methods inherited from class java.lang.Number

        byteValue, shortValue
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • value

        final T value
        The single value to emit, set to null.
      • subscriber

        final org.reactivestreams.Subscriber<? super T> subscriber
        The actual subscriber.
      • NO_REQUEST

        static final int NO_REQUEST
        No request has been issued yet.
        See Also:
        Constant Field Values
    • Constructor Detail

      • ScalarSubscription

        public ScalarSubscription​(org.reactivestreams.Subscriber<? super T> subscriber,
                                  T value)
    • Method Detail

      • request

        public void request​(long n)
        Specified by:
        request in interface org.reactivestreams.Subscription
      • cancel

        public void cancel()
        Specified by:
        cancel in interface org.reactivestreams.Subscription
      • isCancelled

        public boolean isCancelled()
        Returns true if this Subscription was cancelled.
        Returns:
        true if this Subscription was cancelled
      • offer

        public boolean offer​(T e)
        Description copied from interface: SimpleQueue
        Atomically enqueue a single value.
        Specified by:
        offer in interface SimpleQueue<T>
        Parameters:
        e - the value to enqueue, not null
        Returns:
        true if successful, false if the value was not enqueued likely due to reaching the queue capacity)
      • offer

        public boolean offer​(T v1,
                             T v2)
        Description copied from interface: SimpleQueue
        Atomically enqueue two values.
        Specified by:
        offer in interface SimpleQueue<T>
        Parameters:
        v1 - the first value to enqueue, not null
        v2 - the second value to enqueue, not null
        Returns:
        true if successful, false if the value was not enqueued likely due to reaching the queue capacity)
      • poll

        @Nullable
        public T poll()
        Description copied from interface: SimpleQueue
        Tries to dequeue a value (non-null) or returns null if the queue is empty.

        If the producer uses SimpleQueue.offer(Object, Object) and when polling in pairs, if the first poll() returns a non-null item, the second poll() is guaranteed to return a non-null item as well.

        Specified by:
        poll in interface SimpleQueue<T>
        Returns:
        the item or null to indicate an empty queue
      • isEmpty

        public boolean isEmpty()
        Description copied from interface: SimpleQueue
        Returns true if the queue is empty.

        Note however that due to potential fused functions in SimpleQueue.poll() it is possible this method returns false but then poll() returns null because the fused function swallowed the available item(s).

        Specified by:
        isEmpty in interface SimpleQueue<T>
        Returns:
        true if the queue is empty
      • clear

        public void clear()
        Description copied from interface: SimpleQueue
        Removes all enqueued items from this queue.
        Specified by:
        clear in interface SimpleQueue<T>