Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/subscribers/BasicFuseableSubscriber.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.internal.subscribers;
15
16 import org.reactivestreams.*;
17
18 import io.reactivex.rxjava3.core.FlowableSubscriber;
19 import io.reactivex.rxjava3.exceptions.Exceptions;
20 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
21 import io.reactivex.rxjava3.operators.QueueSubscription;
22 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
23
24 /**
25 * Base class for a fuseable intermediate subscriber.
26 * @param <T> the upstream value type
27 * @param <R> the downstream value type
28 */
29 public abstract class BasicFuseableSubscriber<T, R> implements FlowableSubscriber<T>, QueueSubscription<R> {
30
31 /** The downstream subscriber. */
32 protected final Subscriber<? super R> downstream;
33
34 /** The upstream subscription. */
35 protected Subscription upstream;
36
37 /** The upstream's QueueSubscription if not null. */
38 protected QueueSubscription<T> qs;
39
40 /** Flag indicating no further onXXX event should be accepted. */
41 protected boolean done;
42
43 /** Holds the established fusion mode of the upstream. */
44 protected int sourceMode;
45
46 /**
47 * Construct a BasicFuseableSubscriber by wrapping the given subscriber.
48 * @param downstream the subscriber, not null (not verified)
49 */
50 public BasicFuseableSubscriber(Subscriber<? super R> downstream) {
51 this.downstream = downstream;
52 }
53
54 // final: fixed protocol steps to support fuseable and non-fuseable upstream
55 @SuppressWarnings("unchecked")
56 @Override
57 public final void onSubscribe(Subscription s) {
58 if (SubscriptionHelper.validate(this.upstream, s)) {
59
60 this.upstream = s;
61 if (s instanceof QueueSubscription) {
62 this.qs = (QueueSubscription<T>)s;
63 }
64
65 if (beforeDownstream()) {
66
67 downstream.onSubscribe(this);
68
69 afterDownstream();
70 }
71
72 }
73 }
74
75 /**
76 * Override this to perform actions before the call {@code actual.onSubscribe(this)} happens.
77 * @return true if onSubscribe should continue with the call
78 */
79 protected boolean beforeDownstream() {
80 return true;
81 }
82
83 /**
84 * Override this to perform actions after the call to {@code actual.onSubscribe(this)} happened.
85 */
86 protected void afterDownstream() {
87 // default no-op
88 }
89
90 // -----------------------------------
91 // Convenience and state-aware methods
92 // -----------------------------------
93
94 @Override
95 public void onError(Throwable t) {
96 if (done) {
97 RxJavaPlugins.onError(t);
98 return;
99 }
100 done = true;
101 downstream.onError(t);
102 }
103
104 /**
105 * Rethrows the throwable if it is a fatal exception or calls {@link #onError(Throwable)}.
106 * @param t the throwable to rethrow or signal to the actual subscriber
107 */
108 protected final void fail(Throwable t) {
109 Exceptions.throwIfFatal(t);
110 upstream.cancel();
111 onError(t);
112 }
113
114 @Override
115 public void onComplete() {
116 if (done) {
117 return;
118 }
119 done = true;
120 downstream.onComplete();
121 }
122
123 /**
124 * Calls the upstream's QueueSubscription.requestFusion with the mode and
125 * saves the established mode in {@link #sourceMode} if that mode doesn't
126 * have the {@link QueueSubscription#BOUNDARY} flag set.
127 * <p>
128 * If the upstream doesn't support fusion ({@link #qs} is null), the method
129 * returns {@link QueueSubscription#NONE}.
130 * @param mode the fusion mode requested
131 * @return the established fusion mode
132 */
133 protected final int transitiveBoundaryFusion(int mode) {
134 QueueSubscription<T> qs = this.qs;
135 if (qs != null) {
136 if ((mode & BOUNDARY) == 0) {
137 int m = qs.requestFusion(mode);
138 if (m != NONE) {
139 sourceMode = m;
140 }
141 return m;
142 }
143 }
144 return NONE;
145 }
146
147 // --------------------------------------------------------------
148 // Default implementation of the RS and QS protocol (can be overridden)
149 // --------------------------------------------------------------
150
151 @Override
152 public void request(long n) {
153 upstream.request(n);
154 }
155
156 @Override
157 public void cancel() {
158 upstream.cancel();
159 }
160
161 @Override
162 public boolean isEmpty() {
163 return qs.isEmpty();
164 }
165
166 @Override
167 public void clear() {
168 qs.clear();
169 }
170
171 // -----------------------------------------------------------
172 // The rest of the Queue interface methods shouldn't be called
173 // -----------------------------------------------------------
174
175 @Override
176 public final boolean offer(R e) {
177 throw new UnsupportedOperationException("Should not be called!");
178 }
179
180 @Override
181 public final boolean offer(R v1, R v2) {
182 throw new UnsupportedOperationException("Should not be called!");
183 }
184 }