Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/completable/CompletableCreate.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.completable;
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 CompletableCreate extends Completable {
27
28 final CompletableOnSubscribe source;
29
30 public CompletableCreate(CompletableOnSubscribe source) {
31 this.source = source;
32 }
33
34 @Override
35 protected void subscribeActual(CompletableObserver observer) {
36 Emitter 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
48 extends AtomicReference<Disposable>
49 implements CompletableEmitter, Disposable {
50
51 private static final long serialVersionUID = -2467358622224974244L;
52
53 final CompletableObserver downstream;
54
55 Emitter(CompletableObserver downstream) {
56 this.downstream = downstream;
57 }
58
59 @Override
60 public void onComplete() {
61 if (get() != DisposableHelper.DISPOSED) {
62 Disposable d = getAndSet(DisposableHelper.DISPOSED);
63 if (d != DisposableHelper.DISPOSED) {
64 try {
65 downstream.onComplete();
66 } finally {
67 if (d != null) {
68 d.dispose();
69 }
70 }
71 }
72 }
73 }
74
75 @Override
76 public void onError(Throwable t) {
77 if (!tryOnError(t)) {
78 RxJavaPlugins.onError(t);
79 }
80 }
81
82 @Override
83 public boolean tryOnError(Throwable t) {
84 if (t == null) {
85 t = ExceptionHelper.createNullPointerException("onError called with a null Throwable.");
86 }
87 if (get() != DisposableHelper.DISPOSED) {
88 Disposable d = getAndSet(DisposableHelper.DISPOSED);
89 if (d != DisposableHelper.DISPOSED) {
90 try {
91 downstream.onError(t);
92 } finally {
93 if (d != null) {
94 d.dispose();
95 }
96 }
97 return true;
98 }
99 }
100 return false;
101 }
102
103 @Override
104 public void setDisposable(Disposable d) {
105 DisposableHelper.set(this, d);
106 }
107
108 @Override
109 public void setCancellable(Cancellable c) {
110 setDisposable(new CancellableDisposable(c));
111 }
112
113 @Override
114 public void dispose() {
115 DisposableHelper.dispose(this);
116 }
117
118 @Override
119 public boolean isDisposed() {
120 return DisposableHelper.isDisposed(get());
121 }
122
123 @Override
124 public String toString() {
125 return String.format("%s{%s}", getClass().getSimpleName(), super.toString());
126 }
127 }
128 }