Debian Codegraph @

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