Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeCallbackObserver.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.maybe;
15
16 import java.util.concurrent.atomic.AtomicReference;
17
18 import io.reactivex.rxjava3.core.MaybeObserver;
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 /**
28 * MaybeObserver that delegates the onSuccess, onError and onComplete method calls to callbacks.
29 *
30 * @param <T> the value type
31 */
32 public final class MaybeCallbackObserver<T>
33 extends AtomicReference<Disposable>
34 implements MaybeObserver<T>, Disposable, LambdaConsumerIntrospection {
35
36 private static final long serialVersionUID = -6076952298809384986L;
37
38 final Consumer<? super T> onSuccess;
39
40 final Consumer<? super Throwable> onError;
41
42 final Action onComplete;
43
44 public MaybeCallbackObserver(Consumer<? super T> onSuccess, Consumer<? super Throwable> onError,
45 Action onComplete) {
46 super();
47 this.onSuccess = onSuccess;
48 this.onError = onError;
49 this.onComplete = onComplete;
50 }
51
52 @Override
53 public void dispose() {
54 DisposableHelper.dispose(this);
55 }
56
57 @Override
58 public boolean isDisposed() {
59 return DisposableHelper.isDisposed(get());
60 }
61
62 @Override
63 public void onSubscribe(Disposable d) {
64 DisposableHelper.setOnce(this, d);
65 }
66
67 @Override
68 public void onSuccess(T value) {
69 lazySet(DisposableHelper.DISPOSED);
70 try {
71 onSuccess.accept(value);
72 } catch (Throwable ex) {
73 Exceptions.throwIfFatal(ex);
74 RxJavaPlugins.onError(ex);
75 }
76 }
77
78 @Override
79 public void onError(Throwable e) {
80 lazySet(DisposableHelper.DISPOSED);
81 try {
82 onError.accept(e);
83 } catch (Throwable ex) {
84 Exceptions.throwIfFatal(ex);
85 RxJavaPlugins.onError(new CompositeException(e, ex));
86 }
87 }
88
89 @Override
90 public void onComplete() {
91 lazySet(DisposableHelper.DISPOSED);
92 try {
93 onComplete.run();
94 } catch (Throwable ex) {
95 Exceptions.throwIfFatal(ex);
96 RxJavaPlugins.onError(ex);
97 }
98 }
99
100 @Override
101 public boolean hasCustomOnError() {
102 return onError != Functions.ON_ERROR_MISSING;
103 }
104 }