Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleCreate.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.single;
15
16 import java.util.concurrent.atomic.AtomicReference;
17
18 import io.reactivex.rxjava3.core.*;
19 import io.reactivex.rxjava3.disposables.Disposable;
20 import io.reactivex.rxjava3.exceptions.Exceptions;
21 import io.reactivex.rxjava3.functions.Cancellable;
22 import io.reactivex.rxjava3.internal.disposables.*;
23 import io.reactivex.rxjava3.internal.util.ExceptionHelper;
24 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
25
26 public final class SingleCreate<T> extends Single<T> {
27
28 final SingleOnSubscribe<T> source;
29
30 public SingleCreate(SingleOnSubscribe<T> source) {
31 this.source = source;
32 }
33
34 @Override
35 protected void subscribeActual(SingleObserver<? super T> observer) {
36 Emitter<T> parent = new Emitter<>(observer);
37 observer.onSubscribe(parent);
38
39 try {
40 source.subscribe(parent);
41 } catch (Throwable ex) {
42 Exceptions.throwIfFatal(ex);
43 parent.onError(ex);
44 }
45 }
46
47 static final class Emitter<T>
48 extends AtomicReference<Disposable>
49 implements SingleEmitter<T>, Disposable {
50
51 private static final long serialVersionUID = -2467358622224974244L;
52
53 final SingleObserver<? super T> downstream;
54
55 Emitter(SingleObserver<? super T> downstream) {
56 this.downstream = downstream;
57 }
58
59 @Override
60 public void onSuccess(T value) {
61 if (get() != DisposableHelper.DISPOSED) {
62 Disposable d = getAndSet(DisposableHelper.DISPOSED);
63 if (d != DisposableHelper.DISPOSED) {
64 try {
65 if (value == null) {
66 downstream.onError(ExceptionHelper.createNullPointerException("onSuccess called with a null value."));
67 } else {
68 downstream.onSuccess(value);
69 }
70 } finally {
71 if (d != null) {
72 d.dispose();
73 }
74 }
75 }
76 }
77 }
78
79 @Override
80 public void onError(Throwable t) {
81 if (!tryOnError(t)) {
82 RxJavaPlugins.onError(t);
83 }
84 }
85
86 @Override
87 public boolean tryOnError(Throwable t) {
88 if (t == null) {
89 t = ExceptionHelper.createNullPointerException("onError called with a null Throwable.");
90 }
91 if (get() != DisposableHelper.DISPOSED) {
92 Disposable d = getAndSet(DisposableHelper.DISPOSED);
93 if (d != DisposableHelper.DISPOSED) {
94 try {
95 downstream.onError(t);
96 } finally {
97 if (d != null) {
98 d.dispose();
99 }
100 }
101 return true;
102 }
103 }
104 return false;
105 }
106
107 @Override
108 public void setDisposable(Disposable d) {
109 DisposableHelper.set(this, d);
110 }
111
112 @Override
113 public void setCancellable(Cancellable c) {
114 setDisposable(new CancellableDisposable(c));
115 }
116
117 @Override
118 public void dispose() {
119 DisposableHelper.dispose(this);
120 }
121
122 @Override
123 public boolean isDisposed() {
124 return DisposableHelper.isDisposed(get());
125 }
126
127 @Override
128 public String toString() {
129 return String.format("%s{%s}", getClass().getSimpleName(), super.toString());
130 }
131 }
132 }