Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/subscribers/ForEachWhileSubscriber.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.subscribers;
15
16 import java.util.concurrent.atomic.AtomicReference;
17
18 import org.reactivestreams.Subscription;
19
20 import io.reactivex.rxjava3.core.FlowableSubscriber;
21 import io.reactivex.rxjava3.disposables.Disposable;
22 import io.reactivex.rxjava3.exceptions.*;
23 import io.reactivex.rxjava3.functions.*;
24 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
25 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
26
27 public final class ForEachWhileSubscriber<T>
28 extends AtomicReference<Subscription>
29 implements FlowableSubscriber<T>, Disposable {
30
31 private static final long serialVersionUID = -4403180040475402120L;
32
33 final Predicate<? super T> onNext;
34
35 final Consumer<? super Throwable> onError;
36
37 final Action onComplete;
38
39 boolean done;
40
41 public ForEachWhileSubscriber(Predicate<? super T> onNext,
42 Consumer<? super Throwable> onError, Action onComplete) {
43 this.onNext = onNext;
44 this.onError = onError;
45 this.onComplete = onComplete;
46 }
47
48 @Override
49 public void onSubscribe(Subscription s) {
50 SubscriptionHelper.setOnce(this, s, Long.MAX_VALUE);
51 }
52
53 @Override
54 public void onNext(T t) {
55 if (done) {
56 return;
57 }
58
59 boolean b;
60 try {
61 b = onNext.test(t);
62 } catch (Throwable ex) {
63 Exceptions.throwIfFatal(ex);
64 dispose();
65 onError(ex);
66 return;
67 }
68
69 if (!b) {
70 dispose();
71 onComplete();
72 }
73 }
74
75 @Override
76 public void onError(Throwable t) {
77 if (done) {
78 RxJavaPlugins.onError(t);
79 return;
80 }
81 done = true;
82 try {
83 onError.accept(t);
84 } catch (Throwable ex) {
85 Exceptions.throwIfFatal(ex);
86 RxJavaPlugins.onError(new CompositeException(t, ex));
87 }
88 }
89
90 @Override
91 public void onComplete() {
92 if (done) {
93 return;
94 }
95 done = true;
96 try {
97 onComplete.run();
98 } catch (Throwable ex) {
99 Exceptions.throwIfFatal(ex);
100 RxJavaPlugins.onError(ex);
101 }
102 }
103
104 @Override
105 public void dispose() {
106 SubscriptionHelper.cancel(this);
107 }
108
109 @Override
110 public boolean isDisposed() {
111 return this.get() == SubscriptionHelper.CANCELLED;
112 }
113 }