Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableSequenceEqualSingle.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.AtomicInteger;
17
18 import org.reactivestreams.Publisher;
19
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.disposables.Disposable;
22 import io.reactivex.rxjava3.exceptions.Exceptions;
23 import io.reactivex.rxjava3.functions.BiPredicate;
24 import io.reactivex.rxjava3.internal.fuseable.*;
25 import io.reactivex.rxjava3.internal.operators.flowable.FlowableSequenceEqual.*;
26 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
27 import io.reactivex.rxjava3.internal.util.AtomicThrowable;
28 import io.reactivex.rxjava3.operators.SimpleQueue;
29 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
30
31 public final class FlowableSequenceEqualSingle<T> extends Single<Boolean> implements FuseToFlowable<Boolean> {
32 final Publisher<? extends T> first;
33 final Publisher<? extends T> second;
34 final BiPredicate<? super T, ? super T> comparer;
35 final int prefetch;
36
37 public FlowableSequenceEqualSingle(Publisher<? extends T> first, Publisher<? extends T> second,
38 BiPredicate<? super T, ? super T> comparer, int prefetch) {
39 this.first = first;
40 this.second = second;
41 this.comparer = comparer;
42 this.prefetch = prefetch;
43 }
44
45 @Override
46 public void subscribeActual(SingleObserver<? super Boolean> observer) {
47 EqualCoordinator<T> parent = new EqualCoordinator<>(observer, prefetch, comparer);
48 observer.onSubscribe(parent);
49 parent.subscribe(first, second);
50 }
51
52 @Override
53 public Flowable<Boolean> fuseToFlowable() {
54 return RxJavaPlugins.onAssembly(new FlowableSequenceEqual<>(first, second, comparer, prefetch));
55 }
56
57 static final class EqualCoordinator<T>
58 extends AtomicInteger
59 implements Disposable, EqualCoordinatorHelper {
60
61 private static final long serialVersionUID = -6178010334400373240L;
62
63 final SingleObserver<? super Boolean> downstream;
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 T v1;
74
75 T v2;
76
77 EqualCoordinator(SingleObserver<? super Boolean> actual, int prefetch, BiPredicate<? super T, ? super T> comparer) {
78 this.downstream = actual;
79 this.comparer = comparer;
80 this.first = new EqualSubscriber<>(this, prefetch);
81 this.second = new EqualSubscriber<>(this, prefetch);
82 this.errors = new AtomicThrowable();
83 }
84
85 void subscribe(Publisher<? extends T> source1, Publisher<? extends T> source2) {
86 source1.subscribe(first);
87 source2.subscribe(second);
88 }
89
90 @Override
91 public void dispose() {
92 first.cancel();
93 second.cancel();
94 errors.tryTerminateAndReport();
95 if (getAndIncrement() == 0) {
96 first.clear();
97 second.clear();
98 }
99 }
100
101 @Override
102 public boolean isDisposed() {
103 return first.get() == SubscriptionHelper.CANCELLED;
104 }
105
106 void cancelAndClear() {
107 first.cancel();
108 first.clear();
109 second.cancel();
110 second.clear();
111 }
112
113 @Override
114 public void drain() {
115 if (getAndIncrement() != 0) {
116 return;
117 }
118
119 int missed = 1;
120
121 for (;;) {
122 SimpleQueue<T> q1 = first.queue;
123 SimpleQueue<T> q2 = second.queue;
124
125 if (q1 != null && q2 != null) {
126 for (;;) {
127 if (isDisposed()) {
128 first.clear();
129 second.clear();
130 return;
131 }
132
133 Throwable ex = errors.get();
134 if (ex != null) {
135 cancelAndClear();
136
137 errors.tryTerminateConsumer(downstream);
138 return;
139 }
140
141 boolean d1 = first.done;
142
143 T a = v1;
144 if (a == null) {
145 try {
146 a = q1.poll();
147 } catch (Throwable exc) {
148 Exceptions.throwIfFatal(exc);
149 cancelAndClear();
150 errors.tryAddThrowableOrReport(exc);
151 errors.tryTerminateConsumer(downstream);
152 return;
153 }
154 v1 = a;
155 }
156 boolean e1 = a == null;
157
158 boolean d2 = second.done;
159 T b = v2;
160 if (b == null) {
161 try {
162 b = q2.poll();
163 } catch (Throwable exc) {
164 Exceptions.throwIfFatal(exc);
165 cancelAndClear();
166 errors.tryAddThrowableOrReport(exc);
167 errors.tryTerminateConsumer(downstream);
168 return;
169 }
170 v2 = b;
171 }
172
173 boolean e2 = b == null;
174
175 if (d1 && d2 && e1 && e2) {
176 downstream.onSuccess(true);
177 return;
178 }
179 if ((d1 && d2) && (e1 != e2)) {
180 cancelAndClear();
181 downstream.onSuccess(false);
182 return;
183 }
184
185 if (e1 || e2) {
186 break;
187 }
188
189 boolean c;
190
191 try {
192 c = comparer.test(a, b);
193 } catch (Throwable exc) {
194 Exceptions.throwIfFatal(exc);
195 cancelAndClear();
196 errors.tryAddThrowableOrReport(exc);
197 errors.tryTerminateConsumer(downstream);
198 return;
199 }
200
201 if (!c) {
202 cancelAndClear();
203 downstream.onSuccess(false);
204 return;
205 }
206
207 v1 = null;
208 v2 = null;
209
210 first.request();
211 second.request();
212 }
213
214 } else {
215 if (isDisposed()) {
216 first.clear();
217 second.clear();
218 return;
219 }
220
221 Throwable ex = errors.get();
222 if (ex != null) {
223 cancelAndClear();
224
225 errors.tryTerminateConsumer(downstream);
226 return;
227 }
228 }
229
230 missed = addAndGet(-missed);
231 if (missed == 0) {
232 break;
233 }
234 }
235 }
236
237 @Override
238 public void innerError(Throwable t) {
239 if (errors.tryAddThrowableOrReport(t)) {
240 drain();
241 }
242 }
243 }
244 }