Debian Codegraph @

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