Debian Codegraph @

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