Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleUsing.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.Objects;
17 import java.util.concurrent.atomic.AtomicReference;
18
19 import io.reactivex.rxjava3.core.*;
20 import io.reactivex.rxjava3.disposables.Disposable;
21 import io.reactivex.rxjava3.exceptions.*;
22 import io.reactivex.rxjava3.functions.*;
23 import io.reactivex.rxjava3.internal.disposables.*;
24 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
25
26 public final class SingleUsing<T, U> extends Single<T> {
27
28 final Supplier<U> resourceSupplier;
29 final Function<? super U, ? extends SingleSource<? extends T>> singleFunction;
30 final Consumer<? super U> disposer;
31 final boolean eager;
32
33 public SingleUsing(Supplier<U> resourceSupplier,
34 Function<? super U, ? extends SingleSource<? extends T>> singleFunction,
35 Consumer<? super U> disposer,
36 boolean eager) {
37 this.resourceSupplier = resourceSupplier;
38 this.singleFunction = singleFunction;
39 this.disposer = disposer;
40 this.eager = eager;
41 }
42
43 @Override
44 protected void subscribeActual(final SingleObserver<? super T> observer) {
45
46 final U resource; // NOPMD
47
48 try {
49 resource = resourceSupplier.get();
50 } catch (Throwable ex) {
51 Exceptions.throwIfFatal(ex);
52 EmptyDisposable.error(ex, observer);
53 return;
54 }
55
56 SingleSource<? extends T> source;
57
58 try {
59 source = Objects.requireNonNull(singleFunction.apply(resource), "The singleFunction returned a null SingleSource");
60 } catch (Throwable ex) {
61 Exceptions.throwIfFatal(ex);
62
63 if (eager) {
64 try {
65 disposer.accept(resource);
66 } catch (Throwable exc) {
67 Exceptions.throwIfFatal(exc);
68 ex = new CompositeException(ex, exc);
69 }
70 }
71 EmptyDisposable.error(ex, observer);
72 if (!eager) {
73 try {
74 disposer.accept(resource);
75 } catch (Throwable exc) {
76 Exceptions.throwIfFatal(exc);
77 RxJavaPlugins.onError(exc);
78 }
79 }
80 return;
81 }
82
83 source.subscribe(new UsingSingleObserver<T, U>(observer, resource, eager, disposer));
84 }
85
86 static final class UsingSingleObserver<T, U> extends
87 AtomicReference<Object> implements SingleObserver<T>, Disposable {
88
89 private static final long serialVersionUID = -5331524057054083935L;
90
91 final SingleObserver<? super T> downstream;
92
93 final Consumer<? super U> disposer;
94
95 final boolean eager;
96
97 Disposable upstream;
98
99 UsingSingleObserver(SingleObserver<? super T> actual, U resource, boolean eager,
100 Consumer<? super U> disposer) {
101 super(resource);
102 this.downstream = actual;
103 this.eager = eager;
104 this.disposer = disposer;
105 }
106
107 @Override
108 public void dispose() {
109 if (eager) {
110 disposeResource();
111 upstream.dispose();
112 upstream = DisposableHelper.DISPOSED;
113 } else {
114 upstream.dispose();
115 upstream = DisposableHelper.DISPOSED;
116 disposeResource();
117 }
118 }
119
120 @Override
121 public boolean isDisposed() {
122 return upstream.isDisposed();
123 }
124
125 @Override
126 public void onSubscribe(Disposable d) {
127 if (DisposableHelper.validate(this.upstream, d)) {
128 this.upstream = d;
129
130 downstream.onSubscribe(this);
131 }
132 }
133
134 @SuppressWarnings("unchecked")
135 @Override
136 public void onSuccess(T value) {
137 upstream = DisposableHelper.DISPOSED;
138
139 if (eager) {
140 Object u = getAndSet(this);
141 if (u != this) {
142 try {
143 disposer.accept((U)u);
144 } catch (Throwable ex) {
145 Exceptions.throwIfFatal(ex);
146 downstream.onError(ex);
147 return;
148 }
149 } else {
150 return;
151 }
152 }
153
154 downstream.onSuccess(value);
155
156 if (!eager) {
157 disposeResource();
158 }
159 }
160
161 @SuppressWarnings("unchecked")
162 @Override
163 public void onError(Throwable e) {
164 upstream = DisposableHelper.DISPOSED;
165
166 if (eager) {
167 Object u = getAndSet(this);
168 if (u != this) {
169 try {
170 disposer.accept((U)u);
171 } catch (Throwable ex) {
172 Exceptions.throwIfFatal(ex);
173 e = new CompositeException(e, ex);
174 }
175 } else {
176 return;
177 }
178 }
179
180 downstream.onError(e);
181
182 if (!eager) {
183 disposeResource();
184 }
185 }
186
187 @SuppressWarnings("unchecked")
188 void disposeResource() {
189 Object u = getAndSet(this);
190 if (u != this) {
191 try {
192 disposer.accept((U)u);
193 } catch (Throwable ex) {
194 Exceptions.throwIfFatal(ex);
195 RxJavaPlugins.onError(ex);
196 }
197 }
198 }
199 }
200 }