Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/observers/ConsumerSingleObserver.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.SingleObserver;
19 import io.reactivex.rxjava3.disposables.Disposable;
20 import io.reactivex.rxjava3.exceptions.*;
21 import io.reactivex.rxjava3.functions.Consumer;
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 ConsumerSingleObserver<T>
28 extends AtomicReference<Disposable>
29 implements SingleObserver<T>, Disposable, LambdaConsumerIntrospection {
30
31 private static final long serialVersionUID = -7012088219455310787L;
32
33 final Consumer<? super T> onSuccess;
34
35 final Consumer<? super Throwable> onError;
36
37 public ConsumerSingleObserver(Consumer<? super T> onSuccess, Consumer<? super Throwable> onError) {
38 this.onSuccess = onSuccess;
39 this.onError = onError;
40 }
41
42 @Override
43 public void onError(Throwable e) {
44 lazySet(DisposableHelper.DISPOSED);
45 try {
46 onError.accept(e);
47 } catch (Throwable ex) {
48 Exceptions.throwIfFatal(ex);
49 RxJavaPlugins.onError(new CompositeException(e, ex));
50 }
51 }
52
53 @Override
54 public void onSubscribe(Disposable d) {
55 DisposableHelper.setOnce(this, d);
56 }
57
58 @Override
59 public void onSuccess(T value) {
60 lazySet(DisposableHelper.DISPOSED);
61 try {
62 onSuccess.accept(value);
63 } catch (Throwable ex) {
64 Exceptions.throwIfFatal(ex);
65 RxJavaPlugins.onError(ex);
66 }
67 }
68
69 @Override
70 public void dispose() {
71 DisposableHelper.dispose(this);
72 }
73
74 @Override
75 public boolean isDisposed() {
76 return get() == DisposableHelper.DISPOSED;
77 }
78
79 @Override
80 public boolean hasCustomOnError() {
81 return onError != Functions.ON_ERROR_MISSING;
82 }
83 }