Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableAnySingle.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 org.reactivestreams.Subscription;
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.Predicate;
22 import io.reactivex.rxjava3.internal.fuseable.FuseToFlowable;
23 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
24 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
25
26 public final class FlowableAnySingle<T> extends Single<Boolean> implements FuseToFlowable<Boolean> {
27 final Flowable<T> source;
28
29 final Predicate<? super T> predicate;
30
31 public FlowableAnySingle(Flowable<T> source, Predicate<? super T> predicate) {
32 this.source = source;
33 this.predicate = predicate;
34 }
35
36 @Override
37 protected void subscribeActual(SingleObserver<? super Boolean> observer) {
38 source.subscribe(new AnySubscriber<>(observer, predicate));
39 }
40
41 @Override
42 public Flowable<Boolean> fuseToFlowable() {
43 return RxJavaPlugins.onAssembly(new FlowableAny<>(source, predicate));
44 }
45
46 static final class AnySubscriber<T> implements FlowableSubscriber<T>, Disposable {
47
48 final SingleObserver<? super Boolean> downstream;
49
50 final Predicate<? super T> predicate;
51
52 Subscription upstream;
53
54 boolean done;
55
56 AnySubscriber(SingleObserver<? super Boolean> actual, Predicate<? super T> predicate) {
57 this.downstream = actual;
58 this.predicate = predicate;
59 }
60
61 @Override
62 public void onSubscribe(Subscription s) {
63 if (SubscriptionHelper.validate(this.upstream, s)) {
64 this.upstream = s;
65 downstream.onSubscribe(this);
66 s.request(Long.MAX_VALUE);
67 }
68 }
69
70 @Override
71 public void onNext(T t) {
72 if (done) {
73 return;
74 }
75 boolean b;
76 try {
77 b = predicate.test(t);
78 } catch (Throwable e) {
79 Exceptions.throwIfFatal(e);
80 upstream.cancel();
81 upstream = SubscriptionHelper.CANCELLED;
82 onError(e);
83 return;
84 }
85 if (b) {
86 done = true;
87 upstream.cancel();
88 upstream = SubscriptionHelper.CANCELLED;
89 downstream.onSuccess(true);
90 }
91 }
92
93 @Override
94 public void onError(Throwable t) {
95 if (done) {
96 RxJavaPlugins.onError(t);
97 return;
98 }
99
100 done = true;
101 upstream = SubscriptionHelper.CANCELLED;
102 downstream.onError(t);
103 }
104
105 @Override
106 public void onComplete() {
107 if (!done) {
108 done = true;
109 upstream = SubscriptionHelper.CANCELLED;
110 downstream.onSuccess(false);
111 }
112 }
113
114 @Override
115 public void dispose() {
116 upstream.cancel();
117 upstream = SubscriptionHelper.CANCELLED;
118 }
119
120 @Override
121 public boolean isDisposed() {
122 return upstream == SubscriptionHelper.CANCELLED;
123 }
124 }
125 }