Debian Codegraph @

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