Class SubscriberUtils
- java.lang.Object
-
- io.servicetalk.concurrent.internal.SubscriberUtils
-
public final class SubscriberUtils extends java.lang.ObjectA set of utilities for commonPublisherSource.Subscribertasks.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> intcalculateSourceRequested(java.util.concurrent.atomic.AtomicLongFieldUpdater<T> requestNUpdater, java.util.concurrent.atomic.AtomicLongFieldUpdater<T> sourceRequestedUpdater, java.util.concurrent.atomic.AtomicLongFieldUpdater<T> emittedUpdater, int limit, T owner)Attempts to incrementsourceRequestedUpdaterin order to make it the same asrequestNUpdaterwhile not exceeding thelimit.static booleancheckDuplicateSubscription(PublisherSource.Subscription existing, PublisherSource.Subscription next)Checks for an already existingPublisherSource.Subscriptionand if one is given callsCancellable.cancel()onnextand returnsfalse.static voiddeliverCompleteFromSource(CompletableSource.Subscriber subscriber)Deliver a terminal complete to aCompletableSource.Subscriberthat has not yet hadCompletableSource.Subscriber.onSubscribe(Cancellable)called.static <T> voiddeliverCompleteFromSource(PublisherSource.Subscriber<T> subscriber)Deliver a terminal complete to aPublisherSource.Subscriberthat has not yet hadPublisherSource.Subscriber.onSubscribe(PublisherSource.Subscription)called.static voiddeliverErrorFromSource(CompletableSource.Subscriber subscriber, java.lang.Throwable cause)Deliver a terminal error to aCompletableSource.Subscriberthat has not yet hadCompletableSource.Subscriber.onSubscribe(Cancellable)called.static <T> voiddeliverErrorFromSource(PublisherSource.Subscriber<T> subscriber, java.lang.Throwable cause)Deliver a terminal error to aPublisherSource.Subscriberthat has not yet hadPublisherSource.Subscriber.onSubscribe(PublisherSource.Subscription)called.static <T> voiddeliverErrorFromSource(SingleSource.Subscriber<T> subscriber, java.lang.Throwable cause)Deliver a terminal error to aSingleSource.Subscriberthat has not yet hadSingleSource.Subscriber.onSubscribe(Cancellable)called.static <T> voiddeliverSuccessFromSource(SingleSource.Subscriber<T> subscriber, T value)InvokesSingleSource.Subscriber.onSuccess(Object)ignoring an occurred exception if any.static voidhandleExceptionFromOnSubscribe(CompletableSource.Subscriber subscriber, java.lang.Throwable cause)Handle the case when a call toCompletableSource.Subscriber.onSubscribe(Cancellable)throws from a source.static <T> voidhandleExceptionFromOnSubscribe(PublisherSource.Subscriber<T> subscriber, java.lang.Throwable cause)Handle the case when a call toPublisherSource.Subscriber.onSubscribe(PublisherSource.Subscription)throws from a source.static <T> voidhandleExceptionFromOnSubscribe(SingleSource.Subscriber<T> subscriber, java.lang.Throwable cause)Handle the case when a call toSingleSource.Subscriber.onSubscribe(Cancellable)throws from a source.static booleanisRequestNValid(long n)Returnsfalseif the requested amount of elementsnis not-positive,trueotherwise.static java.lang.IllegalArgumentExceptionnewExceptionForInvalidRequestN(long n)Create a new exception for an invalid amount ofPublisherSource.Subscription.request(long)according to Reactive Streams, Rule 3.9.static voidsafeCancel(Cancellable cancellable)InvokesCancellable.cancel()ignoring any exceptions that are thrown.static voidsafeOnComplete(CompletableSource.Subscriber subscriber)InvokesCompletableSource.Subscriber.onComplete()ignoring an occurred exception if any.static <T> voidsafeOnComplete(PublisherSource.Subscriber<T> subscriber)InvokesPublisherSource.Subscriber.onComplete()ignoring an occurred exception if any.static voidsafeOnError(CompletableSource.Subscriber subscriber, java.lang.Throwable cause)InvokesCompletableSource.Subscriber.onError(Throwable)ignoring an occurred exception if any.static <T> voidsafeOnError(PublisherSource.Subscriber<T> subscriber, java.lang.Throwable cause)InvokesPublisherSource.Subscriber.onError(Throwable)ignoring an occurred exception if any.static <T> voidsafeOnError(SingleSource.Subscriber<T> subscriber, java.lang.Throwable cause)InvokesSingleSource.Subscriber.onError(Throwable)ignoring an occurred exception if any.static <T> voidsafeOnSuccess(SingleSource.Subscriber<T> subscriber, T value)InvokesSingleSource.Subscriber.onSuccess(Object)ignoring an occurred exception if any.
-
-
-
Method Detail
-
checkDuplicateSubscription
public static boolean checkDuplicateSubscription(@Nullable PublisherSource.Subscription existing, PublisherSource.Subscription next)Checks for an already existingPublisherSource.Subscriptionand if one is given callsCancellable.cancel()onnextand returnsfalse.- Parameters:
existing- the existingPublisherSource.Subscriptionornullif none exists.next- the nextPublisherSource.Subscriptionto use.- Returns:
trueif noPublisherSource.Subscriptionexists,falseotherwise.
-
isRequestNValid
public static boolean isRequestNValid(long n)
Returnsfalseif the requested amount of elementsnis not-positive,trueotherwise.- Parameters:
n- the number of elements to request.- Returns:
falseif the requested amount of elementsnis not-positive,trueotherwise.
-
newExceptionForInvalidRequestN
public static java.lang.IllegalArgumentException newExceptionForInvalidRequestN(long n)
Create a new exception for an invalid amount ofPublisherSource.Subscription.request(long)according to Reactive Streams, Rule 3.9.- Parameters:
n- the invalid request count.- Returns:
- The exception which clarifies the invalid behavior.
-
calculateSourceRequested
public static <T> int calculateSourceRequested(java.util.concurrent.atomic.AtomicLongFieldUpdater<T> requestNUpdater, java.util.concurrent.atomic.AtomicLongFieldUpdater<T> sourceRequestedUpdater, java.util.concurrent.atomic.AtomicLongFieldUpdater<T> emittedUpdater, int limit, T owner)Attempts to incrementsourceRequestedUpdaterin order to make it the same asrequestNUpdaterwhile not exceeding thelimit.- Type Parameters:
T- The type of object which owns the atomic updater parameters.- Parameters:
requestNUpdater- The total number which has been requested (typically fromPublisherSource.Subscription.request(long)).sourceRequestedUpdater- The total number which has actually been passed toPublisherSource.Subscription.request(long). This outstanding countsourceRequestedUpdater() - emittedUpdater.get()should never exceedlimit.emittedUpdater- The amount of data that has been emitted/delivered by the source.limit- The maximum outstanding demand from the source at any given time.owner- The object which all atomic updater parameters are associated with.- Returns:
- The amount that
sourceRequestedUpdaterwas increased by. This value is typically used to callPublisherSource.Subscription.request(long).
-
deliverCompleteFromSource
public static <T> void deliverCompleteFromSource(PublisherSource.Subscriber<T> subscriber)
Deliver a terminal complete to aPublisherSource.Subscriberthat has not yet hadPublisherSource.Subscriber.onSubscribe(PublisherSource.Subscription)called.- Type Parameters:
T- The type ofPublisherSource.Subscriber.- Parameters:
subscriber- ThePublisherSource.Subscriberto terminate.
-
deliverSuccessFromSource
public static <T> void deliverSuccessFromSource(SingleSource.Subscriber<T> subscriber, @Nullable T value)
InvokesSingleSource.Subscriber.onSuccess(Object)ignoring an occurred exception if any.- Type Parameters:
T- The type ofSingleSource.Subscriber.- Parameters:
subscriber- TheSingleSource.Subscriberthat may throw an exception fromSingleSource.Subscriber.onSuccess(Object).value- The value to pass toSingleSource.Subscriber.onSuccess(Object).
-
deliverCompleteFromSource
public static void deliverCompleteFromSource(CompletableSource.Subscriber subscriber)
Deliver a terminal complete to aCompletableSource.Subscriberthat has not yet hadCompletableSource.Subscriber.onSubscribe(Cancellable)called.- Parameters:
subscriber- TheCompletableSource.Subscriberto terminate.
-
deliverErrorFromSource
public static <T> void deliverErrorFromSource(PublisherSource.Subscriber<T> subscriber, java.lang.Throwable cause)
Deliver a terminal error to aPublisherSource.Subscriberthat has not yet hadPublisherSource.Subscriber.onSubscribe(PublisherSource.Subscription)called.- Type Parameters:
T- The type ofPublisherSource.Subscriber.- Parameters:
subscriber- ThePublisherSource.Subscriberto terminate.cause- The terminal event.
-
deliverErrorFromSource
public static <T> void deliverErrorFromSource(SingleSource.Subscriber<T> subscriber, java.lang.Throwable cause)
Deliver a terminal error to aSingleSource.Subscriberthat has not yet hadSingleSource.Subscriber.onSubscribe(Cancellable)called.- Type Parameters:
T- The type ofSingleSource.Subscriber.- Parameters:
subscriber- TheSingleSource.Subscriberto terminate.cause- The terminal event.
-
deliverErrorFromSource
public static void deliverErrorFromSource(CompletableSource.Subscriber subscriber, java.lang.Throwable cause)
Deliver a terminal error to aCompletableSource.Subscriberthat has not yet hadCompletableSource.Subscriber.onSubscribe(Cancellable)called.- Parameters:
subscriber- TheCompletableSource.Subscriberto terminate.cause- The terminal event.
-
handleExceptionFromOnSubscribe
public static <T> void handleExceptionFromOnSubscribe(PublisherSource.Subscriber<T> subscriber, java.lang.Throwable cause)
Handle the case when a call toPublisherSource.Subscriber.onSubscribe(PublisherSource.Subscription)throws from a source.- Type Parameters:
T- The type ofPublisherSource.Subscriber.- Parameters:
subscriber- ThePublisherSource.Subscriberthat threw an exception fromPublisherSource.Subscriber.onSubscribe(PublisherSource.Subscription).cause- The exception thrown bysubscriber.
-
handleExceptionFromOnSubscribe
public static <T> void handleExceptionFromOnSubscribe(SingleSource.Subscriber<T> subscriber, java.lang.Throwable cause)
Handle the case when a call toSingleSource.Subscriber.onSubscribe(Cancellable)throws from a source.- Type Parameters:
T- The type ofSingleSource.Subscriber.- Parameters:
subscriber- TheSingleSource.Subscriberthat threw an exception fromSingleSource.Subscriber.onSubscribe(Cancellable).cause- The exception thrown bysubscriber.
-
handleExceptionFromOnSubscribe
public static void handleExceptionFromOnSubscribe(CompletableSource.Subscriber subscriber, java.lang.Throwable cause)
Handle the case when a call toCompletableSource.Subscriber.onSubscribe(Cancellable)throws from a source.- Parameters:
subscriber- TheCompletableSource.Subscriberthat threw an exception fromCompletableSource.Subscriber.onSubscribe(Cancellable).cause- The exception thrown bysubscriber.
-
safeOnError
public static void safeOnError(CompletableSource.Subscriber subscriber, java.lang.Throwable cause)
InvokesCompletableSource.Subscriber.onError(Throwable)ignoring an occurred exception if any.- Parameters:
subscriber- TheCompletableSource.Subscriberthat may throw an exception fromCompletableSource.Subscriber.onError(Throwable).cause- The occurredThrowableforCompletableSource.Subscriber.onError(Throwable).
-
safeOnError
public static <T> void safeOnError(SingleSource.Subscriber<T> subscriber, java.lang.Throwable cause)
InvokesSingleSource.Subscriber.onError(Throwable)ignoring an occurred exception if any.- Type Parameters:
T- The type ofSingleSource.Subscriber.- Parameters:
subscriber- TheSingleSource.Subscriberthat may throw an exception fromSingleSource.Subscriber.onError(Throwable).cause- The occurredThrowableforSingleSource.Subscriber.onError(Throwable).
-
safeOnError
public static <T> void safeOnError(PublisherSource.Subscriber<T> subscriber, java.lang.Throwable cause)
InvokesPublisherSource.Subscriber.onError(Throwable)ignoring an occurred exception if any.- Type Parameters:
T- The type ofPublisherSource.Subscriber.- Parameters:
subscriber- ThePublisherSource.Subscriberthat may throw an exception fromPublisherSource.Subscriber.onError(Throwable).cause- The occurredThrowableforPublisherSource.Subscriber.onError(Throwable).
-
safeOnComplete
public static <T> void safeOnComplete(PublisherSource.Subscriber<T> subscriber)
InvokesPublisherSource.Subscriber.onComplete()ignoring an occurred exception if any.- Type Parameters:
T- The type ofPublisherSource.Subscriber.- Parameters:
subscriber- ThePublisherSource.Subscriberthat may throw an exception fromPublisherSource.Subscriber.onComplete().
-
safeOnSuccess
public static <T> void safeOnSuccess(SingleSource.Subscriber<T> subscriber, @Nullable T value)
InvokesSingleSource.Subscriber.onSuccess(Object)ignoring an occurred exception if any.- Type Parameters:
T- The type ofSingleSource.Subscriber.- Parameters:
subscriber- TheSingleSource.Subscriberthat may throw an exception fromSingleSource.Subscriber.onSuccess(Object).value- The value to pass toSingleSource.Subscriber.onSuccess(Object).
-
safeOnComplete
public static void safeOnComplete(CompletableSource.Subscriber subscriber)
InvokesCompletableSource.Subscriber.onComplete()ignoring an occurred exception if any.- Parameters:
subscriber- TheCompletableSource.Subscriberthat may throw an exception fromCompletableSource.Subscriber.onComplete().
-
safeCancel
public static void safeCancel(Cancellable cancellable)
InvokesCancellable.cancel()ignoring any exceptions that are thrown.- Parameters:
cancellable- TheCancellabletocancel.
-
-