Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeCreate.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.*;
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 /**
27 * Provides an API over MaybeObserver that serializes calls to onXXX and manages cancellation
28 * in a safe manner.
29 *
30 * @param <T> the value type emitted
31 */
32 public final class MaybeCreate<T> extends Maybe<T> {
33
34 final MaybeOnSubscribe<T> source;
35
36 public MaybeCreate(MaybeOnSubscribe<T> source) {
37 this.source = source;
38 }
39
40 @Override
41 protected void subscribeActual(MaybeObserver<? super T> observer) {
42 Emitter<T> parent = new Emitter<>(observer);
43 observer.onSubscribe(parent);
44
45 try {
46 source.subscribe(parent);
47 } catch (Throwable ex) {
48 Exceptions.throwIfFatal(ex);
49 parent.onError(ex);
50 }
51 }
52
53 static final class Emitter<T>
54 extends AtomicReference<Disposable>
55 implements MaybeEmitter<T>, Disposable {
56
57 final MaybeObserver<? super T> downstream;
58
59 Emitter(MaybeObserver<? super T> downstream) {
60 this.downstream = downstream;
61 }
62
63 private static final long serialVersionUID = -2467358622224974244L;
64
65 @Override
66 public void onSuccess(T value) {
67 if (get() != DisposableHelper.DISPOSED) {
68 Disposable d = getAndSet(DisposableHelper.DISPOSED);
69 if (d != DisposableHelper.DISPOSED) {
70 try {
71 if (value == null) {
72 downstream.onError(ExceptionHelper.createNullPointerException("onSuccess called with a null value."));
73 } else {
74 downstream.onSuccess(value);
75 }
76 } finally {
77 if (d != null) {
78 d.dispose();
79 }
80 }
81 }
82 }
83 }
84
85 @Override
86 public void onError(Throwable t) {
87 if (!tryOnError(t)) {
88 RxJavaPlugins.onError(t);
89 }
90 }
91
92 @Override
93 public boolean tryOnError(Throwable t) {
94 if (t == null) {
95 t = ExceptionHelper.createNullPointerException("onError called with a null Throwable.");
96 }
97 if (get() != DisposableHelper.DISPOSED) {
98 Disposable d = getAndSet(DisposableHelper.DISPOSED);
99 if (d != DisposableHelper.DISPOSED) {
100 try {
101 downstream.onError(t);
102 } finally {
103 if (d != null) {
104 d.dispose();
105 }
106 }
107 return true;
108 }
109 }
110 return false;
111 }
112
113 @Override
114 public void onComplete() {
115 if (get() != DisposableHelper.DISPOSED) {
116 Disposable d = getAndSet(DisposableHelper.DISPOSED);
117 if (d != DisposableHelper.DISPOSED) {
118 try {
119 downstream.onComplete();
120 } finally {
121 if (d != null) {
122 d.dispose();
123 }
124 }
125 }
126 }
127 }
128
129 @Override
130 public void setDisposable(Disposable d) {
131 DisposableHelper.set(this, d);
132 }
133
134 @Override
135 public void setCancellable(Cancellable c) {
136 setDisposable(new CancellableDisposable(c));
137 }
138
139 @Override
140 public void dispose() {
141 DisposableHelper.dispose(this);
142 }
143
144 @Override
145 public boolean isDisposed() {
146 return DisposableHelper.isDisposed(get());
147 }
148
149 @Override
150 public String toString() {
151 return String.format("%s{%s}", getClass().getSimpleName(), super.toString());
152 }
153 }
154 }