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