| 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.maybe; |
| 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.exceptions.Exceptions; |
| 21 |
import io.reactivex.rxjava3.functions.BiPredicate; |
| 22 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 23 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 24 |
|
| 25 |
/** |
| 26 |
* Compares two MaybeSources to see if they are both empty or emit the same value compared |
| 27 |
* via a BiPredicate. |
| 28 |
* |
| 29 |
* @param <T> the common base type of the sources |
| 30 |
*/ |
| 31 |
public final class MaybeEqualSingle<T> extends Single<Boolean> { |
| 32 |
final MaybeSource<? extends T> source1; |
| 33 |
|
| 34 |
final MaybeSource<? extends T> source2; |
| 35 |
|
| 36 |
final BiPredicate<? super T, ? super T> isEqual; |
| 37 |
|
| 38 |
public MaybeEqualSingle(MaybeSource<? extends T> source1, MaybeSource<? extends T> source2, |
| 39 |
BiPredicate<? super T, ? super T> isEqual) { |
| 40 |
this.source1 = source1; |
| 41 |
this.source2 = source2; |
| 42 |
this.isEqual = isEqual; |
| 43 |
} |
| 44 |
|
| 45 |
@Override |
| 46 |
protected void subscribeActual(SingleObserver<? super Boolean> observer) { |
| 47 |
EqualCoordinator<T> parent = new EqualCoordinator<>(observer, isEqual); |
| 48 |
observer.onSubscribe(parent); |
| 49 |
parent.subscribe(source1, source2); |
| 50 |
} |
| 51 |
|
| 52 |
@SuppressWarnings("serial") |
| 53 |
static final class EqualCoordinator<T> |
| 54 |
extends AtomicInteger |
| 55 |
implements Disposable { |
| 56 |
final SingleObserver<? super Boolean> downstream; |
| 57 |
|
| 58 |
final EqualObserver<T> observer1; |
| 59 |
|
| 60 |
final EqualObserver<T> observer2; |
| 61 |
|
| 62 |
final BiPredicate<? super T, ? super T> isEqual; |
| 63 |
|
| 64 |
EqualCoordinator(SingleObserver<? super Boolean> actual, BiPredicate<? super T, ? super T> isEqual) { |
| 65 |
super(2); |
| 66 |
this.downstream = actual; |
| 67 |
this.isEqual = isEqual; |
| 68 |
this.observer1 = new EqualObserver<>(this); |
| 69 |
this.observer2 = new EqualObserver<>(this); |
| 70 |
} |
| 71 |
|
| 72 |
void subscribe(MaybeSource<? extends T> source1, MaybeSource<? extends T> source2) { |
| 73 |
source1.subscribe(observer1); |
| 74 |
source2.subscribe(observer2); |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public void dispose() { |
| 79 |
observer1.dispose(); |
| 80 |
observer2.dispose(); |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public boolean isDisposed() { |
| 85 |
return DisposableHelper.isDisposed(observer1.get()); |
| 86 |
} |
| 87 |
|
| 88 |
@SuppressWarnings("unchecked") |
| 89 |
void done() { |
| 90 |
if (decrementAndGet() == 0) { |
| 91 |
Object o1 = observer1.value; |
| 92 |
Object o2 = observer2.value; |
| 93 |
|
| 94 |
if (o1 != null && o2 != null) { |
| 95 |
boolean b; |
| 96 |
|
| 97 |
try { |
| 98 |
b = isEqual.test((T)o1, (T)o2); |
| 99 |
} catch (Throwable ex) { |
| 100 |
Exceptions.throwIfFatal(ex); |
| 101 |
downstream.onError(ex); |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
downstream.onSuccess(b); |
| 106 |
} else { |
| 107 |
downstream.onSuccess(o1 == null && o2 == null); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
void error(EqualObserver<T> sender, Throwable ex) { |
| 113 |
if (getAndSet(0) > 0) { |
| 114 |
if (sender == observer1) { |
| 115 |
observer2.dispose(); |
| 116 |
} else { |
| 117 |
observer1.dispose(); |
| 118 |
} |
| 119 |
downstream.onError(ex); |
| 120 |
} else { |
| 121 |
RxJavaPlugins.onError(ex); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
static final class EqualObserver<T> |
| 127 |
extends AtomicReference<Disposable> |
| 128 |
implements MaybeObserver<T> { |
| 129 |
|
| 130 |
private static final long serialVersionUID = -3031974433025990931L; |
| 131 |
|
| 132 |
final EqualCoordinator<T> parent; |
| 133 |
|
| 134 |
Object value; |
| 135 |
|
| 136 |
EqualObserver(EqualCoordinator<T> parent) { |
| 137 |
this.parent = parent; |
| 138 |
} |
| 139 |
|
| 140 |
public void dispose() { |
| 141 |
DisposableHelper.dispose(this); |
| 142 |
} |
| 143 |
|
| 144 |
@Override |
| 145 |
public void onSubscribe(Disposable d) { |
| 146 |
DisposableHelper.setOnce(this, d); |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public void onSuccess(T value) { |
| 151 |
this.value = value; |
| 152 |
parent.done(); |
| 153 |
} |
| 154 |
|
| 155 |
@Override |
| 156 |
public void onError(Throwable e) { |
| 157 |
parent.error(this, e); |
| 158 |
} |
| 159 |
|
| 160 |
@Override |
| 161 |
public void onComplete() { |
| 162 |
parent.done(); |
| 163 |
} |
| 164 |
|
| 165 |
} |
| 166 |
} |