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 java.util.concurrent.ExecutionException;
21 import java.util.concurrent.Executor;
22 import java.util.concurrent.TimeUnit;
23 import java.util.concurrent.TimeoutException;
24 import java.util.function.BiConsumer;
25 
26 /**
27  * Implements {@link AsyncFuture} by wrapping a {@link AndroidFuture}.
28  *
29  * @hide
30  */
31 public final class AndroidAsyncFuture<T> implements AsyncFuture<T> {
32 
33     @NonNull
34     private final AndroidFuture<T> mFuture;
35 
AndroidAsyncFuture(@onNull AndroidFuture<T> future)36     public AndroidAsyncFuture(@NonNull AndroidFuture<T> future) {
37         mFuture = future;
38     }
39     @Override
get()40     public T get() throws InterruptedException, ExecutionException {
41         return mFuture.get();
42     }
43 
44     @Override
get(long timeout, TimeUnit unit)45     public T get(long timeout, TimeUnit unit)
46             throws InterruptedException, ExecutionException, TimeoutException {
47         return mFuture.get(timeout, unit);
48     }
49 
50     @Override
whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action, Executor executor)51     public AsyncFuture<T> whenCompleteAsync(BiConsumer<? super T, ? super Throwable> action,
52             Executor executor) {
53         mFuture.whenCompleteAsync(action, executor);
54         return this;
55     }
56 }
57