Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleEquals.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.single;
15
16 import java.util.Objects;
17 import java.util.concurrent.atomic.AtomicInteger;
18
19 import io.reactivex.rxjava3.core.*;
20 import io.reactivex.rxjava3.disposables.*;
21 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
22
23 public final class SingleEquals<T> extends Single<Boolean> {
24
25 final SingleSource<? extends T> first;
26 final SingleSource<? extends T> second;
27
28 public SingleEquals(SingleSource<? extends T> first, SingleSource<? extends T> second) {
29 this.first = first;
30 this.second = second;
31 }
32
33 @Override
34 protected void subscribeActual(final SingleObserver<? super Boolean> observer) {
35
36 final AtomicInteger count = new AtomicInteger();
37 final Object[] values = { null, null };
38
39 final CompositeDisposable set = new CompositeDisposable();
40 observer.onSubscribe(set);
41
42 first.subscribe(new InnerObserver<T>(0, set, values, observer, count));
43 second.subscribe(new InnerObserver<T>(1, set, values, observer, count));
44 }
45
46 static class InnerObserver<T> implements SingleObserver<T> {
47 final int index;
48 final CompositeDisposable set;
49 final Object[] values;
50 final SingleObserver<? super Boolean> downstream;
51 final AtomicInteger count;
52
53 InnerObserver(int index, CompositeDisposable set, Object[] values, SingleObserver<? super Boolean> observer, AtomicInteger count) {
54 this.index = index;
55 this.set = set;
56 this.values = values;
57 this.downstream = observer;
58 this.count = count;
59 }
60
61 @Override
62 public void onSubscribe(Disposable d) {
63 set.add(d);
64 }
65
66 @Override
67 public void onSuccess(T value) {
68 values[index] = value;
69
70 if (count.incrementAndGet() == 2) {
71 downstream.onSuccess(Objects.equals(values[0], values[1]));
72 }
73 }
74
75 @Override
76 public void onError(Throwable e) {
77 int state = count.getAndSet(-1);
78 if (state == 0 || state == 1) {
79 set.dispose();
80 downstream.onError(e);
81 } else {
82 RxJavaPlugins.onError(e);
83 }
84 }
85 }
86 }