Debian Codegraph @

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