1 /*
2  * Copyright (C) 2023 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 
17 package android.car;
18 
19 import android.annotation.Nullable;
20 import android.annotation.SystemApi;
21 
22 import java.util.concurrent.CountDownLatch;
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.TimeoutException;
25 import java.util.concurrent.atomic.AtomicReference;
26 
27 /**
28  * Synchronous implementation for {@link ResultCallback}.
29  *
30  * <p>Can be used to get the results synchronously where {@link ResultCallback} is required.
31  * {@link #get()} and {@link #get(long, TimeUnit)} methods can be used to get the result.
32  *
33  * @param <V> refer to a Parcelable object.
34  *
35  * @hide
36  */
37 @SystemApi
38 public final class SyncResultCallback<V> implements ResultCallback<V> {
39 
40     private final CountDownLatch mLatch = new CountDownLatch(1);
41 
42     private AtomicReference<V> mResult = new AtomicReference<V>(null);
43 
44     /**
45      * Waits if necessary for the computation to complete, and then
46      * retrieves its result.
47      *
48      * @return the computed result
49      * @throws InterruptedException if the current thread was interrupted
50      * while waiting
51      */
52     @Nullable
get()53     public V get() throws InterruptedException {
54         mLatch.await();
55         return mResult.get();
56     }
57 
58     /**
59      * Waits if necessary for at most the given time for the computation
60      * to complete, and then retrieves its result, if available.
61      *
62      * @param timeout the maximum time to wait
63      * @param unit the time unit of the timeout argument
64      * @return the computed result
65      * @throws InterruptedException if the current thread was interrupted
66      * while waiting
67      * @throws TimeoutException if the wait timed out
68      */
69     @Nullable
get(long timeout, @Nullable TimeUnit unit)70     public V get(long timeout, @Nullable TimeUnit unit)
71             throws InterruptedException, TimeoutException {
72         if (mLatch.await(timeout, unit)) {
73             return mResult.get();
74         }
75 
76         throw new TimeoutException("Failed to receive result after " + timeout + " " + unit);
77     }
78 
79     /**
80      * {@inheritDoc}
81      *
82      * <p>
83      * This method should be called internally only.
84      */
85     @Override
onResult(V result)86     public void onResult(V result) {
87         mResult.set(result);
88         mLatch.countDown();
89     }
90 }
91