Debian Codegraph @

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