Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableFromFuture.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.*;
17
18 import io.reactivex.rxjava3.core.*;
19 import io.reactivex.rxjava3.exceptions.Exceptions;
20 import io.reactivex.rxjava3.internal.observers.DeferredScalarDisposable;
21 import io.reactivex.rxjava3.internal.util.ExceptionHelper;
22
23 public final class ObservableFromFuture<T> extends Observable<T> {
24 final Future<? extends T> future;
25 final long timeout;
26 final TimeUnit unit;
27
28 public ObservableFromFuture(Future<? extends T> future, long timeout, TimeUnit unit) {
29 this.future = future;
30 this.timeout = timeout;
31 this.unit = unit;
32 }
33
34 @Override
35 public void subscribeActual(Observer<? super T> observer) {
36 DeferredScalarDisposable<T> d = new DeferredScalarDisposable<>(observer);
37 observer.onSubscribe(d);
38 if (!d.isDisposed()) {
39 T v;
40 try {
41 v = ExceptionHelper.nullCheck(unit != null ? future.get(timeout, unit) : future.get(), "Future returned a null value.");
42 } catch (Throwable ex) {
43 Exceptions.throwIfFatal(ex);
44 if (!d.isDisposed()) {
45 observer.onError(ex);
46 }
47 return;
48 }
49 d.complete(v);
50 }
51 }
52 }