| 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.AtomicInteger; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.*; |
| 19 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 20 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 21 |
import io.reactivex.rxjava3.functions.BiPredicate; |
| 22 |
import io.reactivex.rxjava3.internal.disposables.ArrayCompositeDisposable; |
| 23 |
import io.reactivex.rxjava3.operators.SpscLinkedArrayQueue; |
| 24 |
|
| 25 |
public final class ObservableSequenceEqual<T> extends Observable<Boolean> { |
| 26 |
final ObservableSource<? extends T> first; |
| 27 |
final ObservableSource<? extends T> second; |
| 28 |
final BiPredicate<? super T, ? super T> comparer; |
| 29 |
final int bufferSize; |
| 30 |
|
| 31 |
public ObservableSequenceEqual(ObservableSource<? extends T> first, ObservableSource<? extends T> second, |
| 32 |
BiPredicate<? super T, ? super T> comparer, int bufferSize) { |
| 33 |
this.first = first; |
| 34 |
this.second = second; |
| 35 |
this.comparer = comparer; |
| 36 |
this.bufferSize = bufferSize; |
| 37 |
} |
| 38 |
|
| 39 |
@Override |
| 40 |
public void subscribeActual(Observer<? super Boolean> observer) { |
| 41 |
EqualCoordinator<T> ec = new EqualCoordinator<>(observer, bufferSize, first, second, comparer); |
| 42 |
observer.onSubscribe(ec); |
| 43 |
ec.subscribe(); |
| 44 |
} |
| 45 |
|
| 46 |
static final class EqualCoordinator<T> extends AtomicInteger implements Disposable { |
| 47 |
|
| 48 |
private static final long serialVersionUID = -6178010334400373240L; |
| 49 |
final Observer<? super Boolean> downstream; |
| 50 |
final BiPredicate<? super T, ? super T> comparer; |
| 51 |
final ArrayCompositeDisposable resources; |
| 52 |
final ObservableSource<? extends T> first; |
| 53 |
final ObservableSource<? extends T> second; |
| 54 |
final EqualObserver<T>[] observers; |
| 55 |
|
| 56 |
volatile boolean cancelled; |
| 57 |
|
| 58 |
T v1; |
| 59 |
|
| 60 |
T v2; |
| 61 |
|
| 62 |
EqualCoordinator(Observer<? super Boolean> actual, int bufferSize, |
| 63 |
ObservableSource<? extends T> first, ObservableSource<? extends T> second, |
| 64 |
BiPredicate<? super T, ? super T> comparer) { |
| 65 |
this.downstream = actual; |
| 66 |
this.first = first; |
| 67 |
this.second = second; |
| 68 |
this.comparer = comparer; |
| 69 |
@SuppressWarnings("unchecked") |
| 70 |
EqualObserver<T>[] as = new EqualObserver[2]; |
| 71 |
this.observers = as; |
| 72 |
as[0] = new EqualObserver<>(this, 0, bufferSize); |
| 73 |
as[1] = new EqualObserver<>(this, 1, bufferSize); |
| 74 |
this.resources = new ArrayCompositeDisposable(2); |
| 75 |
} |
| 76 |
|
| 77 |
boolean setDisposable(Disposable d, int index) { |
| 78 |
return resources.setResource(index, d); |
| 79 |
} |
| 80 |
|
| 81 |
void subscribe() { |
| 82 |
EqualObserver<T>[] as = observers; |
| 83 |
first.subscribe(as[0]); |
| 84 |
second.subscribe(as[1]); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public void dispose() { |
| 89 |
if (!cancelled) { |
| 90 |
cancelled = true; |
| 91 |
resources.dispose(); |
| 92 |
|
| 93 |
if (getAndIncrement() == 0) { |
| 94 |
EqualObserver<T>[] as = observers; |
| 95 |
as[0].queue.clear(); |
| 96 |
as[1].queue.clear(); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public boolean isDisposed() { |
| 103 |
return cancelled; |
| 104 |
} |
| 105 |
|
| 106 |
void cancel(SpscLinkedArrayQueue<T> q1, SpscLinkedArrayQueue<T> q2) { |
| 107 |
cancelled = true; |
| 108 |
q1.clear(); |
| 109 |
q2.clear(); |
| 110 |
} |
| 111 |
|
| 112 |
void drain() { |
| 113 |
if (getAndIncrement() != 0) { |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
int missed = 1; |
| 118 |
EqualObserver<T>[] as = observers; |
| 119 |
|
| 120 |
final EqualObserver<T> observer1 = as[0]; |
| 121 |
final SpscLinkedArrayQueue<T> q1 = observer1.queue; |
| 122 |
final EqualObserver<T> observer2 = as[1]; |
| 123 |
final SpscLinkedArrayQueue<T> q2 = observer2.queue; |
| 124 |
|
| 125 |
for (;;) { |
| 126 |
|
| 127 |
for (;;) { |
| 128 |
if (cancelled) { |
| 129 |
q1.clear(); |
| 130 |
q2.clear(); |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
boolean d1 = observer1.done; |
| 135 |
|
| 136 |
if (d1) { |
| 137 |
Throwable e = observer1.error; |
| 138 |
if (e != null) { |
| 139 |
cancel(q1, q2); |
| 140 |
|
| 141 |
downstream.onError(e); |
| 142 |
return; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
boolean d2 = observer2.done; |
| 147 |
if (d2) { |
| 148 |
Throwable e = observer2.error; |
| 149 |
if (e != null) { |
| 150 |
cancel(q1, q2); |
| 151 |
|
| 152 |
downstream.onError(e); |
| 153 |
return; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
if (v1 == null) { |
| 158 |
v1 = q1.poll(); |
| 159 |
} |
| 160 |
boolean e1 = v1 == null; |
| 161 |
|
| 162 |
if (v2 == null) { |
| 163 |
v2 = q2.poll(); |
| 164 |
} |
| 165 |
boolean e2 = v2 == null; |
| 166 |
|
| 167 |
if (d1 && d2 && e1 && e2) { |
| 168 |
downstream.onNext(true); |
| 169 |
downstream.onComplete(); |
| 170 |
return; |
| 171 |
} |
| 172 |
if ((d1 && d2) && (e1 != e2)) { |
| 173 |
cancel(q1, q2); |
| 174 |
|
| 175 |
downstream.onNext(false); |
| 176 |
downstream.onComplete(); |
| 177 |
return; |
| 178 |
} |
| 179 |
|
| 180 |
if (!e1 && !e2) { |
| 181 |
boolean c; |
| 182 |
|
| 183 |
try { |
| 184 |
c = comparer.test(v1, v2); |
| 185 |
} catch (Throwable ex) { |
| 186 |
Exceptions.throwIfFatal(ex); |
| 187 |
cancel(q1, q2); |
| 188 |
|
| 189 |
downstream.onError(ex); |
| 190 |
return; |
| 191 |
} |
| 192 |
|
| 193 |
if (!c) { |
| 194 |
cancel(q1, q2); |
| 195 |
|
| 196 |
downstream.onNext(false); |
| 197 |
downstream.onComplete(); |
| 198 |
return; |
| 199 |
} |
| 200 |
|
| 201 |
v1 = null; |
| 202 |
v2 = null; |
| 203 |
} |
| 204 |
|
| 205 |
if (e1 || e2) { |
| 206 |
break; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
missed = addAndGet(-missed); |
| 211 |
if (missed == 0) { |
| 212 |
break; |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
static final class EqualObserver<T> implements Observer<T> { |
| 219 |
final EqualCoordinator<T> parent; |
| 220 |
final SpscLinkedArrayQueue<T> queue; |
| 221 |
final int index; |
| 222 |
|
| 223 |
volatile boolean done; |
| 224 |
Throwable error; |
| 225 |
|
| 226 |
EqualObserver(EqualCoordinator<T> parent, int index, int bufferSize) { |
| 227 |
this.parent = parent; |
| 228 |
this.index = index; |
| 229 |
this.queue = new SpscLinkedArrayQueue<>(bufferSize); |
| 230 |
} |
| 231 |
|
| 232 |
@Override |
| 233 |
public void onSubscribe(Disposable d) { |
| 234 |
parent.setDisposable(d, index); |
| 235 |
} |
| 236 |
|
| 237 |
@Override |
| 238 |
public void onNext(T t) { |
| 239 |
queue.offer(t); |
| 240 |
parent.drain(); |
| 241 |
} |
| 242 |
|
| 243 |
@Override |
| 244 |
public void onError(Throwable t) { |
| 245 |
error = t; |
| 246 |
done = true; |
| 247 |
parent.drain(); |
| 248 |
} |
| 249 |
|
| 250 |
@Override |
| 251 |
public void onComplete() { |
| 252 |
done = true; |
| 253 |
parent.drain(); |
| 254 |
} |
| 255 |
} |
| 256 |
} |