| 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.flowable; |
| 15 |
|
| 16 |
import java.util.concurrent.*; |
| 17 |
|
| 18 |
import org.reactivestreams.Subscriber; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.core.Flowable; |
| 21 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 22 |
import io.reactivex.rxjava3.internal.subscriptions.DeferredScalarSubscription; |
| 23 |
import io.reactivex.rxjava3.internal.util.ExceptionHelper; |
| 24 |
|
| 25 |
public final class FlowableFromFuture<T> extends Flowable<T> { |
| 26 |
final Future<? extends T> future; |
| 27 |
final long timeout; |
| 28 |
final TimeUnit unit; |
| 29 |
|
| 30 |
public FlowableFromFuture(Future<? extends T> future, long timeout, TimeUnit unit) { |
| 31 |
this.future = future; |
| 32 |
this.timeout = timeout; |
| 33 |
this.unit = unit; |
| 34 |
} |
| 35 |
|
| 36 |
@Override |
| 37 |
public void subscribeActual(Subscriber<? super T> s) { |
| 38 |
DeferredScalarSubscription<T> deferred = new DeferredScalarSubscription<>(s); |
| 39 |
s.onSubscribe(deferred); |
| 40 |
|
| 41 |
T v; |
| 42 |
try { |
| 43 |
v = unit != null ? future.get(timeout, unit) : future.get(); |
| 44 |
} catch (Throwable ex) { |
| 45 |
Exceptions.throwIfFatal(ex); |
| 46 |
if (!deferred.isCancelled()) { |
| 47 |
s.onError(ex); |
| 48 |
} |
| 49 |
return; |
| 50 |
} |
| 51 |
if (v == null) { |
| 52 |
s.onError(ExceptionHelper.createNullPointerException("The future returned a null value.")); |
| 53 |
} else { |
| 54 |
deferred.complete(v); |
| 55 |
} |
| 56 |
} |
| 57 |
} |