Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableWindowBoundary.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.operators.observable;
15
16 import java.util.concurrent.atomic.*;
17
18 import io.reactivex.rxjava3.core.*;
19 import io.reactivex.rxjava3.disposables.Disposable;
20 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
21 import io.reactivex.rxjava3.internal.queue.MpscLinkedQueue;
22 import io.reactivex.rxjava3.internal.util.AtomicThrowable;
23 import io.reactivex.rxjava3.observers.DisposableObserver;
24 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
25 import io.reactivex.rxjava3.subjects.UnicastSubject;
26
27 public final class ObservableWindowBoundary<T, B> extends AbstractObservableWithUpstream<T, Observable<T>> {
28 final ObservableSource<B> other;
29 final int capacityHint;
30
31 public ObservableWindowBoundary(ObservableSource<T> source, ObservableSource<B> other, int capacityHint) {
32 super(source);
33 this.other = other;
34 this.capacityHint = capacityHint;
35 }
36
37 @Override
38 public void subscribeActual(Observer<? super Observable<T>> observer) {
39 WindowBoundaryMainObserver<T, B> parent = new WindowBoundaryMainObserver<>(observer, capacityHint);
40
41 observer.onSubscribe(parent);
42 other.subscribe(parent.boundaryObserver);
43
44 source.subscribe(parent);
45 }
46
47 static final class WindowBoundaryMainObserver<T, B>
48 extends AtomicInteger
49 implements Observer<T>, Disposable, Runnable {
50
51 private static final long serialVersionUID = 2233020065421370272L;
52
53 final Observer<? super Observable<T>> downstream;
54
55 final int capacityHint;
56
57 final WindowBoundaryInnerObserver<T, B> boundaryObserver;
58
59 final AtomicReference<Disposable> upstream;
60
61 final AtomicInteger windows;
62
63 final MpscLinkedQueue<Object> queue;
64
65 final AtomicThrowable errors;
66
67 final AtomicBoolean stopWindows;
68
69 static final Object NEXT_WINDOW = new Object();
70
71 volatile boolean done;
72
73 UnicastSubject<T> window;
74
75 WindowBoundaryMainObserver(Observer<? super Observable<T>> downstream, int capacityHint) {
76 this.downstream = downstream;
77 this.capacityHint = capacityHint;
78 this.boundaryObserver = new WindowBoundaryInnerObserver<>(this);
79 this.upstream = new AtomicReference<>();
80 this.windows = new AtomicInteger(1);
81 this.queue = new MpscLinkedQueue<>();
82 this.errors = new AtomicThrowable();
83 this.stopWindows = new AtomicBoolean();
84 }
85
86 @Override
87 public void onSubscribe(Disposable d) {
88 if (DisposableHelper.setOnce(upstream, d)) {
89
90 innerNext();
91 }
92 }
93
94 @Override
95 public void onNext(T t) {
96 queue.offer(t);
97 drain();
98 }
99
100 @Override
101 public void onError(Throwable e) {
102 boundaryObserver.dispose();
103 if (errors.tryAddThrowableOrReport(e)) {
104 done = true;
105 drain();
106 }
107 }
108
109 @Override
110 public void onComplete() {
111 boundaryObserver.dispose();
112 done = true;
113 drain();
114 }
115
116 @Override
117 public void dispose() {
118 if (stopWindows.compareAndSet(false, true)) {
119 boundaryObserver.dispose();
120 if (windows.decrementAndGet() == 0) {
121 DisposableHelper.dispose(upstream);
122 }
123 }
124 }
125
126 @Override
127 public boolean isDisposed() {
128 return stopWindows.get();
129 }
130
131 @Override
132 public void run() {
133 if (windows.decrementAndGet() == 0) {
134 DisposableHelper.dispose(upstream);
135 }
136 }
137
138 void innerNext() {
139 queue.offer(NEXT_WINDOW);
140 drain();
141 }
142
143 void innerError(Throwable e) {
144 DisposableHelper.dispose(upstream);
145 if (errors.tryAddThrowableOrReport(e)) {
146 done = true;
147 drain();
148 }
149 }
150
151 void innerComplete() {
152 DisposableHelper.dispose(upstream);
153 done = true;
154 drain();
155 }
156
157 @SuppressWarnings("unchecked")
158 void drain() {
159 if (getAndIncrement() != 0) {
160 return;
161 }
162
163 int missed = 1;
164 Observer<? super Observable<T>> downstream = this.downstream;
165 MpscLinkedQueue<Object> queue = this.queue;
166 AtomicThrowable errors = this.errors;
167
168 for (;;) {
169
170 for (;;) {
171 if (windows.get() == 0) {
172 queue.clear();
173 window = null;
174 return;
175 }
176
177 UnicastSubject<T> w = window;
178
179 boolean d = done;
180
181 if (d && errors.get() != null) {
182 queue.clear();
183 Throwable ex = errors.terminate();
184 if (w != null) {
185 window = null;
186 w.onError(ex);
187 }
188 downstream.onError(ex);
189 return;
190 }
191
192 Object v = queue.poll();
193
194 boolean empty = v == null;
195
196 if (d && empty) {
197 Throwable ex = errors.terminate();
198 if (ex == null) {
199 if (w != null) {
200 window = null;
201 w.onComplete();
202 }
203 downstream.onComplete();
204 } else {
205 if (w != null) {
206 window = null;
207 w.onError(ex);
208 }
209 downstream.onError(ex);
210 }
211 return;
212 }
213
214 if (empty) {
215 break;
216 }
217
218 if (v != NEXT_WINDOW) {
219 w.onNext((T)v);
220 continue;
221 }
222
223 if (w != null) {
224 window = null;
225 w.onComplete();
226 }
227
228 if (!stopWindows.get()) {
229 w = UnicastSubject.create(capacityHint, this);
230 window = w;
231 windows.getAndIncrement();
232
233 ObservableWindowSubscribeIntercept<T> intercept = new ObservableWindowSubscribeIntercept<>(w);
234 downstream.onNext(intercept);
235 if (intercept.tryAbandon()) {
236 w.onComplete();
237 }
238 }
239 }
240
241 missed = addAndGet(-missed);
242 if (missed == 0) {
243 break;
244 }
245 }
246 }
247 }
248
249 static final class WindowBoundaryInnerObserver<T, B> extends DisposableObserver<B> {
250
251 final WindowBoundaryMainObserver<T, B> parent;
252
253 boolean done;
254
255 WindowBoundaryInnerObserver(WindowBoundaryMainObserver<T, B> parent) {
256 this.parent = parent;
257 }
258
259 @Override
260 public void onNext(B t) {
261 if (done) {
262 return;
263 }
264 parent.innerNext();
265 }
266
267 @Override
268 public void onError(Throwable t) {
269 if (done) {
270 RxJavaPlugins.onError(t);
271 return;
272 }
273 done = true;
274 parent.innerError(t);
275 }
276
277 @Override
278 public void onComplete() {
279 if (done) {
280 return;
281 }
282 done = true;
283 parent.innerComplete();
284 }
285 }
286 }