/* * Copyright (c) 2016-present, RxJava Contributors. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License is * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See * the License for the specific language governing permissions and limitations under the License. */ package io.reactivex.rxjava3.internal.subscriptions; import java.util.Objects; import java.util.concurrent.atomic.*; import org.reactivestreams.Subscription; import io.reactivex.rxjava3.exceptions.ProtocolViolationException; import io.reactivex.rxjava3.internal.util.BackpressureHelper; import io.reactivex.rxjava3.plugins.RxJavaPlugins; /** * Utility methods to validate Subscriptions in the various onSubscribe calls. */ public enum SubscriptionHelper implements Subscription { /** * Represents a cancelled Subscription. *
Don't leak this instance!
*/
CANCELLED
;
@Override
public void request(long n) {
// deliberately ignored
}
@Override
public void cancel() {
// deliberately ignored
}
/**
* Verifies that current is null, next is not null, otherwise signals errors
* to the RxJavaPlugins and returns false.
* @param current the current Subscription, expected to be null
* @param next the next Subscription, expected to be non-null
* @return true if the validation succeeded
*/
public static boolean validate(Subscription current, Subscription next) {
if (next == null) {
RxJavaPlugins.onError(new NullPointerException("next is null"));
return false;
}
if (current != null) {
next.cancel();
reportSubscriptionSet();
return false;
}
return true;
}
/**
* Reports that the subscription is already set to the RxJavaPlugins error handler,
* which is an indication of a onSubscribe management bug.
*/
public static void reportSubscriptionSet() {
RxJavaPlugins.onError(new ProtocolViolationException("Subscription already set!"));
}
/**
* Validates that the n is positive.
* @param n the request amount
* @return false if n is non-positive.
*/
public static boolean validate(long n) {
if (n <= 0) {
RxJavaPlugins.onError(new IllegalArgumentException("n > 0 required but it was " + n));
return false;
}
return true;
}
/**
* Reports to the plugin error handler that there were more values produced than requested, which
* is a sign of internal backpressure handling bug.
* @param n the overproduction amount
*/
public static void reportMoreProduced(long n) {
RxJavaPlugins.onError(new ProtocolViolationException("More produced than requested: " + n));
}
/**
* Atomically sets the subscription on the field and cancels the
* previous subscription if any.
* @param field the target field to set the new subscription on
* @param s the new subscription
* @return true if the operation succeeded, false if the target field
* holds the {@link #CANCELLED} instance.
* @see #replace(AtomicReference, Subscription)
*/
public static boolean set(AtomicReference If the field is not null and doesn't contain the {@link #CANCELLED}
* instance, the {@link #reportSubscriptionSet()} is called.
* @param field the target field
* @param s the new subscription to set
* @return true if the operation succeeded, false if the target field was not null.
*/
public static boolean setOnce(AtomicReference
* If the field is not null and doesn't contain the {@link #CANCELLED}
* instance, the {@link #reportSubscriptionSet()} is called.
* @param field the target field
* @param s the new subscription to set
* @param request the amount to request, positive (not verified)
* @return true if the operation succeeded, false if the target field was not null.
* @since 2.1.11
*/
public static boolean setOnce(AtomicReference