| 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.Callable; |
| 18 |
|
| 19 |
import io.reactivex.rxjava3.core.*; |
| 20 |
import io.reactivex.rxjava3.disposables.*; |
| 21 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 22 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 23 |
|
| 24 |
public final class SingleFromCallable<T> extends Single<T> { |
| 25 |
|
| 26 |
final Callable<? extends T> callable; |
| 27 |
|
| 28 |
public SingleFromCallable(Callable<? extends T> callable) { |
| 29 |
this.callable = callable; |
| 30 |
} |
| 31 |
|
| 32 |
@Override |
| 33 |
protected void subscribeActual(SingleObserver<? super T> observer) { |
| 34 |
Disposable d = Disposable.empty(); |
| 35 |
observer.onSubscribe(d); |
| 36 |
|
| 37 |
if (d.isDisposed()) { |
| 38 |
return; |
| 39 |
} |
| 40 |
T value; |
| 41 |
|
| 42 |
try { |
| 43 |
value = Objects.requireNonNull(callable.call(), "The callable returned a null value"); |
| 44 |
} catch (Throwable ex) { |
| 45 |
Exceptions.throwIfFatal(ex); |
| 46 |
if (!d.isDisposed()) { |
| 47 |
observer.onError(ex); |
| 48 |
} else { |
| 49 |
RxJavaPlugins.onError(ex); |
| 50 |
} |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
if (!d.isDisposed()) { |
| 55 |
observer.onSuccess(value); |
| 56 |
} |
| 57 |
} |
| 58 |
} |