Debian Codegraph @

src/main/java/io/reactivex/rxjava3/subscribers/DefaultSubscriber.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 org.reactivestreams.Subscription;
17
18 import io.reactivex.rxjava3.core.FlowableSubscriber;
19 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
20 import io.reactivex.rxjava3.internal.util.EndConsumerHelper;
21
22 /**
23 * Abstract base implementation of a {@link org.reactivestreams.Subscriber Subscriber} with
24 * support for requesting via {@link #request(long)}, cancelling via
25 * via {@link #cancel()} (both synchronously) and calls {@link #onStart()}
26 * when the subscription happens.
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.
32 *
33 * <p>Note that calling {@link #request(long)} from {@link #onStart()} may trigger
34 * an immediate, asynchronous emission of data to {@link #onNext(Object)}. Make sure
35 * all initialization happens before the call to {@code request()} in {@code onStart()}.
36 * Calling {@link #request(long)} inside {@link #onNext(Object)} can happen at any time
37 * because by design, {@code onNext} calls from upstream are non-reentrant and non-overlapping.
38 *
39 * <p>Use the protected {@link #cancel()} to cancel the sequence from within an
40 * {@code onNext} implementation.
41 *
42 * <p>Like all other consumers, {@code DefaultSubscriber} can be subscribed only once.
43 * Any subsequent attempt to subscribe it to a new source will yield an
44 * {@link IllegalStateException} with message {@code "It is not allowed to subscribe with a(n) <class name> multiple times."}.
45 *
46 * <p>Implementation of {@link #onStart()}, {@link #onNext(Object)}, {@link #onError(Throwable)}
47 * and {@link #onComplete()} are not allowed to throw any unchecked exceptions.
48 * If for some reason this can't be avoided, use {@link io.reactivex.rxjava3.core.Flowable#safeSubscribe(org.reactivestreams.Subscriber)}
49 * instead of the standard {@code subscribe()} method.
50 * @param <T> the value type
51 *
52 * <p>Example<pre><code>
53 * Flowable.range(1, 5)
54 * .subscribe(new DefaultSubscriber&lt;Integer&gt;() {
55 * &#64;Override public void onStart() {
56 * System.out.println("Start!");
57 * request(1);
58 * }
59 * &#64;Override public void onNext(Integer t) {
60 * if (t == 3) {
61 * cancel();
62 * }
63 * System.out.println(t);
64 * request(1);
65 * }
66 * &#64;Override public void onError(Throwable t) {
67 * t.printStackTrace();
68 * }
69 * &#64;Override public void onComplete() {
70 * System.out.println("Done!");
71 * }
72 * });
73 * </code></pre>
74 */
75 public abstract class DefaultSubscriber<T> implements FlowableSubscriber<T> {
76
77 Subscription upstream;
78
79 @Override
80 public final void onSubscribe(Subscription s) {
81 if (EndConsumerHelper.validate(this.upstream, s, getClass())) {
82 this.upstream = s;
83 onStart();
84 }
85 }
86
87 /**
88 * Requests from the upstream {@link Subscription}.
89 * @param n the request amount, positive
90 */
91 protected final void request(long n) {
92 Subscription s = this.upstream;
93 if (s != null) {
94 s.request(n);
95 }
96 }
97
98 /**
99 * Cancels the upstream's {@link Subscription}.
100 */
101 protected final void cancel() {
102 Subscription s = this.upstream;
103 this.upstream = SubscriptionHelper.CANCELLED;
104 s.cancel();
105 }
106 /**
107 * Called once the subscription has been set on this observer; override this
108 * to perform initialization or issue an initial request.
109 * <p>
110 * The default implementation requests {@link Long#MAX_VALUE}.
111 */
112 protected void onStart() {
113 request(Long.MAX_VALUE);
114 }
115
116 }