Debian Codegraph @

src/main/java/io/reactivex/rxjava3/subscribers/DisposableSubscriber.java

raw
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.subscribers;
15
16 import java.util.concurrent.atomic.AtomicReference;
17
18 import org.reactivestreams.Subscription;
19
20 import io.reactivex.rxjava3.core.FlowableSubscriber;
21 import io.reactivex.rxjava3.disposables.Disposable;
22 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
23 import io.reactivex.rxjava3.internal.util.EndConsumerHelper;
24
25 /**
26 * An abstract Subscriber that allows asynchronous, external cancellation by implementing {@link Disposable}.
27 *
28 * <p>All pre-implemented final methods are thread-safe.
29 *
30 * <p>The default {@link #onStart()} requests {@link Long#MAX_VALUE} by default. Override
31 * the method to request a custom <em>positive</em> amount. Use the protected {@link #request(long)}
32 * to request more items and {@link #cancel()} to cancel the sequence from within an
33 * {@code onNext} implementation.
34 *
35 * <p>Note that calling {@link #request(long)} from {@link #onStart()} may trigger
36 * an immediate, asynchronous emission of data to {@link #onNext(Object)}. Make sure
37 * all initialization happens before the call to {@code request()} in {@code onStart()}.
38 * Calling {@link #request(long)} inside {@link #onNext(Object)} can happen at any time
39 * because by design, {@code onNext} calls from upstream are non-reentrant and non-overlapping.
40 *
41 * <p>Like all other consumers, {@code DisposableSubscriber} can be subscribed only once.
42 * Any subsequent attempt to subscribe it to a new source will yield an
43 * {@link IllegalStateException} with message {@code "It is not allowed to subscribe with a(n) <class name> multiple times."}.
44 *
45 * <p>Implementation of {@link #onStart()}, {@link #onNext(Object)}, {@link #onError(Throwable)}
46 * and {@link #onComplete()} are not allowed to throw any unchecked exceptions.
47 * If for some reason this can't be avoided, use {@link io.reactivex.rxjava3.core.Flowable#safeSubscribe(org.reactivestreams.Subscriber)}
48 * instead of the standard {@code subscribe()} method.
49 *
50 * <p>Example<pre><code>
51 * Disposable d =
52 * Flowable.range(1, 5)
53 * .subscribeWith(new DisposableSubscriber&lt;Integer&gt;() {
54 * &#64;Override public void onStart() {
55 * request(1);
56 * }
57 * &#64;Override public void onNext(Integer t) {
58 * if (t == 3) {
59 * cancel();
60 * }
61 * System.out.println(t);
62 * request(1);
63 * }
64 * &#64;Override public void onError(Throwable t) {
65 * t.printStackTrace();
66 * }
67 * &#64;Override public void onComplete() {
68 * System.out.println("Done!");
69 * }
70 * });
71 * // ...
72 * d.dispose();
73 * </code></pre>
74 * @param <T> the received value type.
75 */
76 public abstract class DisposableSubscriber<T> implements FlowableSubscriber<T>, Disposable {
77 final AtomicReference<Subscription> upstream = new AtomicReference<>();
78
79 @Override
80 public final void onSubscribe(Subscription s) {
81 if (EndConsumerHelper.setOnce(this.upstream, s, getClass())) {
82 onStart();
83 }
84 }
85
86 /**
87 * Called once the single upstream {@link Subscription} is set via {@link #onSubscribe(Subscription)}.
88 */
89 protected void onStart() {
90 upstream.get().request(Long.MAX_VALUE);
91 }
92
93 /**
94 * Requests the specified amount from the upstream if its {@link Subscription} is set via
95 * onSubscribe already.
96 * <p>Note that calling this method before a {@link Subscription} is set via {@link #onSubscribe(Subscription)}
97 * leads to {@link NullPointerException} and meant to be called from inside {@link #onStart()} or
98 * {@link #onNext(Object)}.
99 * @param n the request amount, positive
100 */
101 protected final void request(long n) {
102 upstream.get().request(n);
103 }
104
105 /**
106 * Cancels the Subscription set via {@link #onSubscribe(Subscription)} or makes sure a
107 * {@link Subscription} set asynchronously (later) is cancelled immediately.
108 * <p>This method is thread-safe and can be exposed as a public API.
109 */
110 protected final void cancel() {
111 dispose();
112 }
113
114 @Override
115 public final boolean isDisposed() {
116 return upstream.get() == SubscriptionHelper.CANCELLED;
117 }
118
119 @Override
120 public final void dispose() {
121 SubscriptionHelper.cancel(upstream);
122 }
123 }