Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableTakeWhile.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.SubscriptionHelper;
22 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
23
24 public final class FlowableTakeWhile<T> extends AbstractFlowableWithUpstream<T, T> {
25 final Predicate<? super T> predicate;
26 public FlowableTakeWhile(Flowable<T> source, Predicate<? super T> predicate) {
27 super(source);
28 this.predicate = predicate;
29 }
30
31 @Override
32 protected void subscribeActual(Subscriber<? super T> s) {
33 source.subscribe(new TakeWhileSubscriber<>(s, predicate));
34 }
35
36 static final class TakeWhileSubscriber<T> implements FlowableSubscriber<T>, Subscription {
37 final Subscriber<? super T> downstream;
38 final Predicate<? super T> predicate;
39
40 Subscription upstream;
41
42 boolean done;
43
44 TakeWhileSubscriber(Subscriber<? super T> actual, Predicate<? super T> predicate) {
45 this.downstream = actual;
46 this.predicate = predicate;
47 }
48
49 @Override
50 public void onSubscribe(Subscription s) {
51 if (SubscriptionHelper.validate(this.upstream, s)) {
52 this.upstream = s;
53 downstream.onSubscribe(this);
54 }
55 }
56
57 @Override
58 public void onNext(T t) {
59 if (done) {
60 return;
61 }
62 boolean b;
63 try {
64 b = predicate.test(t);
65 } catch (Throwable e) {
66 Exceptions.throwIfFatal(e);
67 upstream.cancel();
68 onError(e);
69 return;
70 }
71
72 if (!b) {
73 done = true;
74 upstream.cancel();
75 downstream.onComplete();
76 return;
77 }
78
79 downstream.onNext(t);
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 downstream.onComplete();
99 }
100
101 @Override
102 public void request(long n) {
103 upstream.request(n);
104 }
105
106 @Override
107 public void cancel() {
108 upstream.cancel();
109 }
110 }
111 }