Class Subscriptions


  • public class Subscriptions
    extends java.lang.Object
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static long add​(long a, long b)
      Adds two long values and caps the sum at Long.MAX_VALUE.
      static long add​(java.util.concurrent.atomic.AtomicLong requested, long requests)
      Atomically adds the positive value n to the requested value in the AtomicLong and caps the result at Long.MAX_VALUE and returns the previous value.
      static long addAndHandledAlreadyCancelled​(java.util.concurrent.atomic.AtomicLong requested, long n)
      Atomically adds the positive value n to the requested value in the AtomicLong and caps the result at Long.MAX_VALUE and returns the previous value and considers Long.MIN_VALUE as a cancel indication (no addition then).
      static boolean addFailure​(java.util.concurrent.atomic.AtomicReference<java.lang.Throwable> failures, java.lang.Throwable failure)  
      static void cancel​(java.util.concurrent.atomic.AtomicReference<org.reactivestreams.Subscription> reference)  
      static void complete​(org.reactivestreams.Subscriber<?> subscriber)
      Invokes onSubscribe on the given Subscriber with the cancelled subscription instance followed immediately by a call to onComplete.
      static org.reactivestreams.Subscription empty()  
      static void fail​(org.reactivestreams.Subscriber<?> subscriber, java.lang.Throwable failure)
      Invokes onSubscribe on the given Subscriber with the cancelled subscription instance followed immediately by a call to onError with the given failure.
      static void fail​(org.reactivestreams.Subscriber<?> subscriber, java.lang.Throwable failure, org.reactivestreams.Publisher<?> upstream)  
      static java.lang.IllegalArgumentException getInvalidRequestException()  
      static java.lang.Throwable markFailureAsTerminated​(java.util.concurrent.atomic.AtomicReference<java.lang.Throwable> failures)  
      static long multiply​(long n, long times)
      Cap a multiplication to Long.MAX_VALUE
      static long produced​(java.util.concurrent.atomic.AtomicLong requested, long amount)
      Concurrent subtraction bound to 0, mostly used to decrement a request tracker by the amount produced by the operator.
      static long producedAndHandleAlreadyCancelled​(java.util.concurrent.atomic.AtomicLong requested, long n)
      Atomically subtract the given number from the target atomic long if it doesn't contain Long.MIN_VALUE (indicating some cancelled state) or Long.MAX_VALUE (unbounded mode).
      static void requestIfNotNullOrAccumulate​(java.util.concurrent.atomic.AtomicReference<org.reactivestreams.Subscription> field, java.util.concurrent.atomic.AtomicLong requested, long requests)
      Atomically requests from the Subscription in the field if not null, otherwise accumulates the request amount in the requested field to be requested once the field is set to non-null.
      static boolean setIfEmpty​(java.util.concurrent.atomic.AtomicReference<org.reactivestreams.Subscription> container, org.reactivestreams.Subscription subscription)
      Atomically sets the subscription on the container if the content is still null.
      static boolean setIfEmptyAndRequest​(java.util.concurrent.atomic.AtomicReference<org.reactivestreams.Subscription> container, java.util.concurrent.atomic.AtomicLong requested, org.reactivestreams.Subscription subscription)
      Atomically sets the new Subscription in the container and requests any accumulated amount from the requested counter.
      static <T> org.reactivestreams.Subscription single​(org.reactivestreams.Subscriber<T> downstream, T item)  
      static long subOrZero​(long a, long b)
      Cap a subtraction to 0
      static long subtract​(java.util.concurrent.atomic.AtomicLong requested, long emitted)
      Atomically subtract the given number (positive, not validated) from the target field unless it contains Long.MAX_VALUE.
      static java.lang.Throwable terminate​(java.util.concurrent.atomic.AtomicReference<java.lang.Throwable> failure)  
      static void terminateAndPropagate​(java.util.concurrent.atomic.AtomicReference<java.lang.Throwable> failures, org.reactivestreams.Subscriber<?> subscriber)  
      static int unboundedOrLimit​(int prefetch)  
      static long unboundedOrRequests​(int concurrency)  
      • Methods inherited from class java.lang.Object

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

      • TERMINATED

        public static final java.lang.Throwable TERMINATED
    • Method Detail

      • getInvalidRequestException

        public static java.lang.IllegalArgumentException getInvalidRequestException()
      • empty

        public static org.reactivestreams.Subscription empty()
      • complete

        public static void complete​(org.reactivestreams.Subscriber<?> subscriber)
        Invokes onSubscribe on the given Subscriber with the cancelled subscription instance followed immediately by a call to onComplete.
        Parameters:
        subscriber - the subscriber, must not be null
      • fail

        public static void fail​(org.reactivestreams.Subscriber<?> subscriber,
                                java.lang.Throwable failure)
        Invokes onSubscribe on the given Subscriber with the cancelled subscription instance followed immediately by a call to onError with the given failure.
        Parameters:
        subscriber - the subscriber, must not be null
        failure - the failure, must not be null
      • fail

        public static void fail​(org.reactivestreams.Subscriber<?> subscriber,
                                java.lang.Throwable failure,
                                org.reactivestreams.Publisher<?> upstream)
      • add

        public static long add​(long a,
                               long b)
        Adds two long values and caps the sum at Long.MAX_VALUE.
        Parameters:
        a - the first value
        b - the second value
        Returns:
        the sum capped at Long.MAX_VALUE
      • add

        public static long add​(java.util.concurrent.atomic.AtomicLong requested,
                               long requests)
        Atomically adds the positive value n to the requested value in the AtomicLong and caps the result at Long.MAX_VALUE and returns the previous value.
        Parameters:
        requested - the AtomicLong holding the current requested value
        requests - the value to add, must be positive (not verified)
        Returns:
        the original value before the add
      • subtract

        public static long subtract​(java.util.concurrent.atomic.AtomicLong requested,
                                    long emitted)
        Atomically subtract the given number (positive, not validated) from the target field unless it contains Long.MAX_VALUE.
        Parameters:
        requested - the target field holding the current requested amount
        emitted - the produced element count, positive (not validated)
        Returns:
        the new amount
      • unboundedOrLimit

        public static int unboundedOrLimit​(int prefetch)
      • unboundedOrRequests

        public static long unboundedOrRequests​(int concurrency)
      • addFailure

        public static boolean addFailure​(java.util.concurrent.atomic.AtomicReference<java.lang.Throwable> failures,
                                         java.lang.Throwable failure)
      • cancel

        public static void cancel​(java.util.concurrent.atomic.AtomicReference<org.reactivestreams.Subscription> reference)
      • markFailureAsTerminated

        public static java.lang.Throwable markFailureAsTerminated​(java.util.concurrent.atomic.AtomicReference<java.lang.Throwable> failures)
      • terminateAndPropagate

        public static void terminateAndPropagate​(java.util.concurrent.atomic.AtomicReference<java.lang.Throwable> failures,
                                                 org.reactivestreams.Subscriber<?> subscriber)
      • multiply

        public static long multiply​(long n,
                                    long times)
        Cap a multiplication to Long.MAX_VALUE
        Parameters:
        n - left operand
        times - right operand
        Returns:
        n * times or Long.MAX_VALUE
      • requestIfNotNullOrAccumulate

        public static void requestIfNotNullOrAccumulate​(java.util.concurrent.atomic.AtomicReference<org.reactivestreams.Subscription> field,
                                                        java.util.concurrent.atomic.AtomicLong requested,
                                                        long requests)
        Atomically requests from the Subscription in the field if not null, otherwise accumulates the request amount in the requested field to be requested once the field is set to non-null.
        Parameters:
        field - the target field that may already contain a Subscription
        requested - the current requested amount
        requests - the request amount, positive (verified)
      • setIfEmptyAndRequest

        public static boolean setIfEmptyAndRequest​(java.util.concurrent.atomic.AtomicReference<org.reactivestreams.Subscription> container,
                                                   java.util.concurrent.atomic.AtomicLong requested,
                                                   org.reactivestreams.Subscription subscription)
        Atomically sets the new Subscription in the container and requests any accumulated amount from the requested counter.
        Parameters:
        container - the target field for the new Subscription
        requested - the current requested amount
        subscription - the new Subscription, must not be null
        Returns:
        true if the Subscription was set the first time
      • setIfEmpty

        public static boolean setIfEmpty​(java.util.concurrent.atomic.AtomicReference<org.reactivestreams.Subscription> container,
                                         org.reactivestreams.Subscription subscription)
        Atomically sets the subscription on the container if the content is still null. If not the passed subscription gets cancelled.
        Parameters:
        container - the target container
        subscription - the new subscription to set
        Returns:
        true if the operation succeeded, false if the target container was already set.
      • terminate

        public static java.lang.Throwable terminate​(java.util.concurrent.atomic.AtomicReference<java.lang.Throwable> failure)
      • produced

        public static long produced​(java.util.concurrent.atomic.AtomicLong requested,
                                    long amount)
        Concurrent subtraction bound to 0, mostly used to decrement a request tracker by the amount produced by the operator.
        Parameters:
        requested - the atomic long keeping track of requests
        amount - delta to subtract
        Returns:
        value after subtraction or zero
      • subOrZero

        public static long subOrZero​(long a,
                                     long b)
        Cap a subtraction to 0
        Parameters:
        a - left operand
        b - right operand
        Returns:
        Subtraction result or 0 if overflow
      • single

        public static <T> org.reactivestreams.Subscription single​(org.reactivestreams.Subscriber<T> downstream,
                                                                  T item)
      • producedAndHandleAlreadyCancelled

        public static long producedAndHandleAlreadyCancelled​(java.util.concurrent.atomic.AtomicLong requested,
                                                             long n)
        Atomically subtract the given number from the target atomic long if it doesn't contain Long.MIN_VALUE (indicating some cancelled state) or Long.MAX_VALUE (unbounded mode).
        Parameters:
        requested - the target field holding the current requested amount
        n - the produced item count, must be positive
        Returns:
        the new amount
      • addAndHandledAlreadyCancelled

        public static long addAndHandledAlreadyCancelled​(java.util.concurrent.atomic.AtomicLong requested,
                                                         long n)
        Atomically adds the positive value n to the requested value in the AtomicLong and caps the result at Long.MAX_VALUE and returns the previous value and considers Long.MIN_VALUE as a cancel indication (no addition then).
        Parameters:
        requested - the AtomicLong holding the current requested value
        n - the value to add, must be positive (not verified)
        Returns:
        the original value before the add