Debian Codegraph @

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