1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package android.car.util.concurrent;
17 
18 import android.annotation.NonNull;
19 
20 import com.android.internal.infra.AndroidFuture;
21 
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.Executor;
24 import java.util.concurrent.TimeUnit;
25 import java.util.concurrent.TimeoutException;
26 import java.util.function.BiConsumer;
27 
28 /**
29  * Implements {@link AsyncFuture} by wrapping a {@link AndroidFuture}.
30  *
31  * @hide
32  */
33 public final class AndroidAsyncFuture<T> implements AsyncFuture<T> {
34 
35     @NonNull
36     private final AndroidFuture<T> mFuture;
37 
AndroidAsyncFuture(@onNull AndroidFuture<T> future)38     public AndroidAsyncFuture(@NonNull AndroidFuture<T> future) {
39         mFuture = future;
40     }
41     @Override
get()42     public T get() throws InterruptedException, ExecutionException {
43         return mFuture.get();
44     }
45 
46     @Override
get(long timeout, TimeUnit unit)47     public T get(long timeout, TimeUnit unit)
48             throws InterruptedException, ExecutionException, TimeoutException {
49         return mFuture.get(timeout, unit);
50     }
51 
52     @Override
whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action, Executor executor)53     public AsyncFuture<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action,
54             Executor executor) {
55         mFuture.whenCompleteAsync(action, executor);
56         return this;
57     }
58 }
59