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 import android.annotation.Nullable;
20 import android.annotation.TestApi;
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  * Custom {@code Future} that provides a way to be handled asynchronously.
30  *
31  * <p><b>Note: it doesn't extend {@link java.util.concurrent.Future} because it's implemented by
32  * {@code AndroidFuture}, which doesn't propagate
33  * {@link java.util.concurrent.Future#cancel(boolean)} to the remote object.
34  *
35  * @param <T> type returned by the {@code Future}.
36  *
37  * @hide
38  */
39 @TestApi
40 public interface AsyncFuture<T> {
41 
42     /**
43      * See {@link java.util.concurrent.Future#get()}.
44      */
45     @Nullable
get()46     T get() throws InterruptedException, ExecutionException;
47 
48     /**
49      * See {@link java.util.concurrent.Future#get(long, TimeUnit).
50      */
51     @Nullable
get(long timeout, @NonNull TimeUnit unit)52     T get(long timeout, @NonNull TimeUnit unit)
53             throws InterruptedException, ExecutionException, TimeoutException;
54 
55     /**
56      * See {@link java.util.concurrent.CompletableFuture#whenCompleteAsync(BiConsumer, Executor).
57      */
58     @NonNull
whenCompleteAsync(@onNull BiConsumer<? super T, ? super Throwable> action, @NonNull Executor executor)59     AsyncFuture<T> whenCompleteAsync(@NonNull BiConsumer<? super T, ? super Throwable> action,
60             @NonNull Executor executor);
61 }
62