Debian Codegraph @

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