Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeFromFuture.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.maybe;
15
16 import java.util.concurrent.*;
17
18 import io.reactivex.rxjava3.core.*;
19 import io.reactivex.rxjava3.disposables.*;
20 import io.reactivex.rxjava3.exceptions.Exceptions;
21
22 /**
23 * Waits until the source Future completes or the wait times out; treats a {@code null}
24 * result as indication to signal {@code onComplete} instead of {@code onSuccess}.
25 *
26 * @param <T> the value type
27 */
28 public final class MaybeFromFuture<T> extends Maybe<T> {
29
30 final Future<? extends T> future;
31
32 final long timeout;
33
34 final TimeUnit unit;
35
36 public MaybeFromFuture(Future<? extends T> future, long timeout, TimeUnit unit) {
37 this.future = future;
38 this.timeout = timeout;
39 this.unit = unit;
40 }
41
42 @Override
43 protected void subscribeActual(MaybeObserver<? super T> observer) {
44 Disposable d = Disposable.empty();
45 observer.onSubscribe(d);
46 if (!d.isDisposed()) {
47 T v;
48 try {
49 if (timeout <= 0L) {
50 v = future.get();
51 } else {
52 v = future.get(timeout, unit);
53 }
54 } catch (Throwable ex) {
55 Exceptions.throwIfFatal(ex);
56 if (ex instanceof ExecutionException) {
57 ex = ex.getCause();
58 }
59 Exceptions.throwIfFatal(ex);
60 if (!d.isDisposed()) {
61 observer.onError(ex);
62 }
63 return;
64 }
65 if (!d.isDisposed()) {
66 if (v == null) {
67 observer.onComplete();
68 } else {
69 observer.onSuccess(v);
70 }
71 }
72 }
73 }
74 }