Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableSequenceEqual.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.flowable;
15
16 import java.util.concurrent.atomic.*;
17
18 import org.reactivestreams.*;
19
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.exceptions.*;
22 import io.reactivex.rxjava3.functions.BiPredicate;
23 import io.reactivex.rxjava3.internal.subscriptions.*;
24 import io.reactivex.rxjava3.internal.util.AtomicThrowable;
25 import io.reactivex.rxjava3.operators.QueueSubscription;
26 import io.reactivex.rxjava3.operators.SimpleQueue;
27 import io.reactivex.rxjava3.operators.SpscArrayQueue;
28
29 public final class FlowableSequenceEqual<T> extends Flowable<Boolean> {
30 final Publisher<? extends T> first;
31 final Publisher<? extends T> second;
32 final BiPredicate<? super T, ? super T> comparer;
33 final int prefetch;
34
35 public FlowableSequenceEqual(Publisher<? extends T> first, Publisher<? extends T> second,
36 BiPredicate<? super T, ? super T> comparer, int prefetch) {
37 this.first = first;
38 this.second = second;
39 this.comparer = comparer;
40 this.prefetch = prefetch;
41 }
42
43 @Override
44 public void subscribeActual(Subscriber<? super Boolean> s) {
45 EqualCoordinator<T> parent = new EqualCoordinator<>(s, prefetch, comparer);
46 s.onSubscribe(parent);
47 parent.subscribe(first, second);
48 }
49
50 /**
51 * Provides callbacks for the EqualSubscribers.
52 */
53 interface EqualCoordinatorHelper {
54
55 void drain();
56
57 void innerError(Throwable ex);
58 }
59
60 static final class EqualCoordinator<T> extends DeferredScalarSubscription<Boolean>
61 implements EqualCoordinatorHelper {
62
63 private static final long serialVersionUID = -6178010334400373240L;
64
65 final BiPredicate<? super T, ? super T> comparer;
66
67 final EqualSubscriber<T> first;
68
69 final EqualSubscriber<T> second;
70
71 final AtomicThrowable errors;
72
73 final AtomicInteger wip;
74
75 T v1;
76
77 T v2;
78
79 EqualCoordinator(Subscriber<? super Boolean> actual, int prefetch, BiPredicate<? super T, ? super T> comparer) {
80 super(actual);
81 this.comparer = comparer;
82 this.wip = new AtomicInteger();
83 this.first = new EqualSubscriber<>(this, prefetch);
84 this.second = new EqualSubscriber<>(this, prefetch);
85 this.errors = new AtomicThrowable();
86 }
87
88 void subscribe(Publisher<? extends T> source1, Publisher<? extends T> source2) {
89 source1.subscribe(first);
90 source2.subscribe(second);
91 }
92
93 @Override
94 public void cancel() {
95 super.cancel();
96 first.cancel();
97 second.cancel();
98 errors.tryTerminateAndReport();
99 if (wip.getAndIncrement() == 0) {
100 first.clear();
101 second.clear();
102 }
103 }
104
105 void cancelAndClear() {
106 first.cancel();
107 first.clear();
108 second.cancel();
109 second.clear();
110 }
111
112 @Override
113 public void drain() {
114 if (wip.getAndIncrement() != 0) {
115 return;
116 }
117
118 int missed = 1;
119
120 for (;;) {
121 SimpleQueue<T> q1 = first.queue;
122 SimpleQueue<T> q2 = second.queue;
123
124 if (q1 != null && q2 != null) {
125 for (;;) {
126 if (isCancelled()) {
127 first.clear();
128 second.clear();
129 return;
130 }
131
132 Throwable ex = errors.get();
133 if (ex != null) {
134 cancelAndClear();
135
136 errors.tryTerminateConsumer(downstream);
137 return;
138 }
139
140 boolean d1 = first.done;
141
142 T a = v1;
143 if (a == null) {
144 try {
145 a = q1.poll();
146 } catch (Throwable exc) {
147 Exceptions.throwIfFatal(exc);
148 cancelAndClear();
149 errors.tryAddThrowableOrReport(exc);
150 errors.tryTerminateConsumer(downstream);
151 return;
152 }
153 v1 = a;
154 }
155 boolean e1 = a == null;
156
157 boolean d2 = second.done;
158 T b = v2;
159 if (b == null) {
160 try {
161 b = q2.poll();
162 } catch (Throwable exc) {
163 Exceptions.throwIfFatal(exc);
164 cancelAndClear();
165 errors.tryAddThrowableOrReport(exc);
166 errors.tryTerminateConsumer(downstream);
167 return;
168 }
169 v2 = b;
170 }
171
172 boolean e2 = b == null;
173
174 if (d1 && d2 && e1 && e2) {
175 complete(true);
176 return;
177 }
178 if ((d1 && d2) && (e1 != e2)) {
179 cancelAndClear();
180 complete(false);
181 return;
182 }
183
184 if (e1 || e2) {
185 break;
186 }
187
188 boolean c;
189
190 try {
191 c = comparer.test(a, b);
192 } catch (Throwable exc) {
193 Exceptions.throwIfFatal(exc);
194 cancelAndClear();
195 errors.tryAddThrowableOrReport(exc);
196 errors.tryTerminateConsumer(downstream);
197 return;
198 }
199
200 if (!c) {
201 cancelAndClear();
202 complete(false);
203 return;
204 }
205
206 v1 = null;
207 v2 = null;
208
209 first.request();
210 second.request();
211 }
212
213 } else {
214 if (isCancelled()) {
215 first.clear();
216 second.clear();
217 return;
218 }
219
220 Throwable ex = errors.get();
221 if (ex != null) {
222 cancelAndClear();
223
224 errors.tryTerminateConsumer(downstream);
225 return;
226 }
227 }
228
229 missed = wip.addAndGet(-missed);
230 if (missed == 0) {
231 break;
232 }
233 }
234 }
235
236 @Override
237 public void innerError(Throwable t) {
238 if (errors.tryAddThrowableOrReport(t)) {
239 drain();
240 }
241 }
242 }
243
244 static final class EqualSubscriber<T>
245 extends AtomicReference<Subscription>
246 implements FlowableSubscriber<T> {
247
248 private static final long serialVersionUID = 4804128302091633067L;
249
250 final EqualCoordinatorHelper parent;
251
252 final int prefetch;
253
254 final int limit;
255
256 long produced;
257
258 volatile SimpleQueue<T> queue;
259
260 volatile boolean done;
261
262 int sourceMode;
263
264 EqualSubscriber(EqualCoordinatorHelper parent, int prefetch) {
265 this.parent = parent;
266 this.limit = prefetch - (prefetch >> 2);
267 this.prefetch = prefetch;
268 }
269
270 @Override
271 public void onSubscribe(Subscription s) {
272 if (SubscriptionHelper.setOnce(this, s)) {
273 if (s instanceof QueueSubscription) {
274 @SuppressWarnings("unchecked")
275 QueueSubscription<T> qs = (QueueSubscription<T>) s;
276
277 int m = qs.requestFusion(QueueSubscription.ANY);
278 if (m == QueueSubscription.SYNC) {
279 sourceMode = m;
280 queue = qs;
281 done = true;
282 parent.drain();
283 return;
284 }
285 if (m == QueueSubscription.ASYNC) {
286 sourceMode = m;
287 queue = qs;
288 s.request(prefetch);
289 return;
290 }
291 }
292
293 queue = new SpscArrayQueue<>(prefetch);
294
295 s.request(prefetch);
296 }
297 }
298
299 @Override
300 public void onNext(T t) {
301 if (sourceMode == QueueSubscription.NONE) {
302 if (!queue.offer(t)) {
303 onError(new MissingBackpressureException());
304 return;
305 }
306 }
307 parent.drain();
308 }
309
310 @Override
311 public void onError(Throwable t) {
312 parent.innerError(t);
313 }
314
315 @Override
316 public void onComplete() {
317 done = true;
318 parent.drain();
319 }
320
321 public void request() {
322 if (sourceMode != QueueSubscription.SYNC) {
323 long p = produced + 1;
324 if (p >= limit) {
325 produced = 0;
326 get().request(p);
327 } else {
328 produced = p;
329 }
330 }
331 }
332
333 public void cancel() {
334 SubscriptionHelper.cancel(this);
335 }
336
337 void clear() {
338 SimpleQueue<T> sq = queue;
339 if (sq != null) {
340 sq.clear();
341 }
342 }
343 }
344 }