Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeFromSupplier.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 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 /**
23 * Executes a supplier and signals its value as success or signals an exception.
24 *
25 * @param <T> the value type
26 * @since 3.0.0
27 */
28 public final class MaybeFromSupplier<T> extends Maybe<T> implements Supplier<T> {
29
30 final Supplier<? extends T> supplier;
31
32 public MaybeFromSupplier(Supplier<? extends T> supplier) {
33 this.supplier = supplier;
34 }
35
36 @Override
37 protected void subscribeActual(MaybeObserver<? super T> observer) {
38 Disposable d = Disposable.empty();
39 observer.onSubscribe(d);
40
41 if (!d.isDisposed()) {
42
43 T v;
44
45 try {
46 v = supplier.get();
47 } catch (Throwable ex) {
48 Exceptions.throwIfFatal(ex);
49 if (!d.isDisposed()) {
50 observer.onError(ex);
51 } else {
52 RxJavaPlugins.onError(ex);
53 }
54 return;
55 }
56
57 if (!d.isDisposed()) {
58 if (v == null) {
59 observer.onComplete();
60 } else {
61 observer.onSuccess(v);
62 }
63 }
64 }
65 }
66
67 @Override
68 public T get() throws Throwable {
69 return supplier.get();
70 }
71 }