Debian Codegraph @

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