Debian Codegraph @

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