Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableAny.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.observable;
15
16 import io.reactivex.rxjava3.core.*;
17 import io.reactivex.rxjava3.disposables.Disposable;
18 import io.reactivex.rxjava3.exceptions.Exceptions;
19 import io.reactivex.rxjava3.functions.Predicate;
20 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
21 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
22
23 public final class ObservableAny<T> extends AbstractObservableWithUpstream<T, Boolean> {
24 final Predicate<? super T> predicate;
25 public ObservableAny(ObservableSource<T> source, Predicate<? super T> predicate) {
26 super(source);
27 this.predicate = predicate;
28 }
29
30 @Override
31 protected void subscribeActual(Observer<? super Boolean> t) {
32 source.subscribe(new AnyObserver<>(t, predicate));
33 }
34
35 static final class AnyObserver<T> implements Observer<T>, Disposable {
36
37 final Observer<? super Boolean> downstream;
38 final Predicate<? super T> predicate;
39
40 Disposable upstream;
41
42 boolean done;
43
44 AnyObserver(Observer<? super Boolean> actual, Predicate<? super T> predicate) {
45 this.downstream = actual;
46 this.predicate = predicate;
47 }
48
49 @Override
50 public void onSubscribe(Disposable d) {
51 if (DisposableHelper.validate(this.upstream, d)) {
52 this.upstream = d;
53 downstream.onSubscribe(this);
54 }
55 }
56
57 @Override
58 public void onNext(T t) {
59 if (done) {
60 return;
61 }
62 boolean b;
63 try {
64 b = predicate.test(t);
65 } catch (Throwable e) {
66 Exceptions.throwIfFatal(e);
67 upstream.dispose();
68 onError(e);
69 return;
70 }
71 if (b) {
72 done = true;
73 upstream.dispose();
74 downstream.onNext(true);
75 downstream.onComplete();
76 }
77 }
78
79 @Override
80 public void onError(Throwable t) {
81 if (done) {
82 RxJavaPlugins.onError(t);
83 return;
84 }
85
86 done = true;
87 downstream.onError(t);
88 }
89
90 @Override
91 public void onComplete() {
92 if (!done) {
93 done = true;
94 downstream.onNext(false);
95 downstream.onComplete();
96 }
97 }
98
99 @Override
100 public void dispose() {
101 upstream.dispose();
102 }
103
104 @Override
105 public boolean isDisposed() {
106 return upstream.isDisposed();
107 }
108 }
109 }