| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.internal.util; |
| 15 |
|
| 16 |
import java.util.Objects; |
| 17 |
import java.util.concurrent.atomic.AtomicReference; |
| 18 |
|
| 19 |
import org.reactivestreams.Subscription; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 22 |
import io.reactivex.rxjava3.exceptions.ProtocolViolationException; |
| 23 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 24 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 25 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 26 |
|
| 27 |
/** |
| 28 |
* Utility class to help report multiple subscriptions with the same |
| 29 |
* consumer type instead of the internal "Disposable already set!" message |
| 30 |
* that is practically reserved for internal operators and indicate bugs in them. |
| 31 |
*/ |
| 32 |
public final class EndConsumerHelper { |
| 33 |
|
| 34 |
/** |
| 35 |
* Utility class. |
| 36 |
*/ |
| 37 |
private EndConsumerHelper() { |
| 38 |
throw new IllegalStateException("No instances!"); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Ensures that the upstream Disposable is null and returns true, otherwise |
| 43 |
* disposes the next Disposable and if the upstream is not the shared |
| 44 |
* disposed instance, reports a ProtocolViolationException due to |
| 45 |
* multiple subscribe attempts. |
| 46 |
* @param upstream the upstream current value |
| 47 |
* @param next the Disposable to check for nullness and dispose if necessary |
| 48 |
* @param observer the class of the consumer to have a personalized |
| 49 |
* error message if the upstream already contains a non-cancelled Disposable. |
| 50 |
* @return true if successful, false if the upstream was non null |
| 51 |
*/ |
| 52 |
public static boolean validate(Disposable upstream, Disposable next, Class<?> observer) { |
| 53 |
Objects.requireNonNull(next, "next is null"); |
| 54 |
if (upstream != null) { |
| 55 |
next.dispose(); |
| 56 |
if (upstream != DisposableHelper.DISPOSED) { |
| 57 |
reportDoubleSubscription(observer); |
| 58 |
} |
| 59 |
return false; |
| 60 |
} |
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Atomically updates the target upstream AtomicReference from null to the non-null |
| 66 |
* next Disposable, otherwise disposes next and reports a ProtocolViolationException |
| 67 |
* if the AtomicReference doesn't contain the shared disposed indicator. |
| 68 |
* @param upstream the target AtomicReference to update |
| 69 |
* @param next the Disposable to set on it atomically |
| 70 |
* @param observer the class of the consumer to have a personalized |
| 71 |
* error message if the upstream already contains a non-cancelled Disposable. |
| 72 |
* @return true if successful, false if the content of the AtomicReference was non null |
| 73 |
*/ |
| 74 |
public static boolean setOnce(AtomicReference<Disposable> upstream, Disposable next, Class<?> observer) { |
| 75 |
Objects.requireNonNull(next, "next is null"); |
| 76 |
if (!upstream.compareAndSet(null, next)) { |
| 77 |
next.dispose(); |
| 78 |
if (upstream.get() != DisposableHelper.DISPOSED) { |
| 79 |
reportDoubleSubscription(observer); |
| 80 |
} |
| 81 |
return false; |
| 82 |
} |
| 83 |
return true; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Ensures that the upstream Subscription is null and returns true, otherwise |
| 88 |
* cancels the next Subscription and if the upstream is not the shared |
| 89 |
* cancelled instance, reports a ProtocolViolationException due to |
| 90 |
* multiple subscribe attempts. |
| 91 |
* @param upstream the upstream current value |
| 92 |
* @param next the Subscription to check for nullness and cancel if necessary |
| 93 |
* @param subscriber the class of the consumer to have a personalized |
| 94 |
* error message if the upstream already contains a non-cancelled Subscription. |
| 95 |
* @return true if successful, false if the upstream was non null |
| 96 |
*/ |
| 97 |
public static boolean validate(Subscription upstream, Subscription next, Class<?> subscriber) { |
| 98 |
Objects.requireNonNull(next, "next is null"); |
| 99 |
if (upstream != null) { |
| 100 |
next.cancel(); |
| 101 |
if (upstream != SubscriptionHelper.CANCELLED) { |
| 102 |
reportDoubleSubscription(subscriber); |
| 103 |
} |
| 104 |
return false; |
| 105 |
} |
| 106 |
return true; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Atomically updates the target upstream AtomicReference from null to the non-null |
| 111 |
* next Subscription, otherwise cancels next and reports a ProtocolViolationException |
| 112 |
* if the AtomicReference doesn't contain the shared cancelled indicator. |
| 113 |
* @param upstream the target AtomicReference to update |
| 114 |
* @param next the Subscription to set on it atomically |
| 115 |
* @param subscriber the class of the consumer to have a personalized |
| 116 |
* error message if the upstream already contains a non-cancelled Subscription. |
| 117 |
* @return true if successful, false if the content of the AtomicReference was non null |
| 118 |
*/ |
| 119 |
public static boolean setOnce(AtomicReference<Subscription> upstream, Subscription next, Class<?> subscriber) { |
| 120 |
Objects.requireNonNull(next, "next is null"); |
| 121 |
if (!upstream.compareAndSet(null, next)) { |
| 122 |
next.cancel(); |
| 123 |
if (upstream.get() != SubscriptionHelper.CANCELLED) { |
| 124 |
reportDoubleSubscription(subscriber); |
| 125 |
} |
| 126 |
return false; |
| 127 |
} |
| 128 |
return true; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Builds the error message with the consumer class. |
| 133 |
* @param consumer the class of the consumer |
| 134 |
* @return the error message string |
| 135 |
*/ |
| 136 |
public static String composeMessage(String consumer) { |
| 137 |
return "It is not allowed to subscribe with a(n) " + consumer + " multiple times. " |
| 138 |
+ "Please create a fresh instance of " + consumer + " and subscribe that to the target source instead."; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Report a ProtocolViolationException with a personalized message referencing |
| 143 |
* the simple type name of the consumer class and report it via |
| 144 |
* RxJavaPlugins.onError. |
| 145 |
* @param consumer the class of the consumer |
| 146 |
*/ |
| 147 |
public static void reportDoubleSubscription(Class<?> consumer) { |
| 148 |
RxJavaPlugins.onError(new ProtocolViolationException(composeMessage(consumer.getName()))); |
| 149 |
} |
| 150 |
} |