Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableFromCallable.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.observable;
15
16 import java.util.concurrent.Callable;
17
18 import io.reactivex.rxjava3.core.*;
19 import io.reactivex.rxjava3.exceptions.Exceptions;
20 import io.reactivex.rxjava3.functions.Supplier;
21 import io.reactivex.rxjava3.internal.observers.DeferredScalarDisposable;
22 import io.reactivex.rxjava3.internal.util.ExceptionHelper;
23 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
24
25 /**
26 * Calls a Callable and emits its resulting single value or signals its exception.
27 * @param <T> the value type
28 */
29 public final class ObservableFromCallable<T> extends Observable<T> implements Supplier<T> {
30
31 final Callable<? extends T> callable;
32
33 public ObservableFromCallable(Callable<? extends T> callable) {
34 this.callable = callable;
35 }
36
37 @Override
38 public void subscribeActual(Observer<? super T> observer) {
39 DeferredScalarDisposable<T> d = new DeferredScalarDisposable<>(observer);
40 observer.onSubscribe(d);
41 if (d.isDisposed()) {
42 return;
43 }
44 T value;
45 try {
46 value = ExceptionHelper.nullCheck(callable.call(), "Callable returned a null value.");
47 } catch (Throwable e) {
48 Exceptions.throwIfFatal(e);
49 if (!d.isDisposed()) {
50 observer.onError(e);
51 } else {
52 RxJavaPlugins.onError(e);
53 }
54 return;
55 }
56 d.complete(value);
57 }
58
59 @Override
60 public T get() throws Throwable {
61 return ExceptionHelper.nullCheck(callable.call(), "The Callable returned a null value.");
62 }
63 }