Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleFromSupplier.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 io.reactivex.rxjava3.core.*;
17 import io.reactivex.rxjava3.disposables.*;
18 import io.reactivex.rxjava3.exceptions.Exceptions;
19 import io.reactivex.rxjava3.functions.Supplier;
20 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
21
22 import java.util.Objects;
23
24 /**
25 * Calls a supplier and emits its value or exception to the incoming SingleObserver.
26 * @param <T> the value type returned
27 * @since 3.0.0
28 */
29 public final class SingleFromSupplier<T> extends Single<T> {
30
31 final Supplier<? extends T> supplier;
32
33 public SingleFromSupplier(Supplier<? extends T> supplier) {
34 this.supplier = supplier;
35 }
36
37 @Override
38 protected void subscribeActual(SingleObserver<? super T> observer) {
39 Disposable d = Disposable.empty();
40 observer.onSubscribe(d);
41
42 if (d.isDisposed()) {
43 return;
44 }
45 T value;
46
47 try {
48 value = Objects.requireNonNull(supplier.get(), "The supplier returned a null value");
49 } catch (Throwable ex) {
50 Exceptions.throwIfFatal(ex);
51 if (!d.isDisposed()) {
52 observer.onError(ex);
53 } else {
54 RxJavaPlugins.onError(ex);
55 }
56 return;
57 }
58
59 if (!d.isDisposed()) {
60 observer.onSuccess(value);
61 }
62 }
63 }