1 /*
2  * Copyright (C) 2024 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 com.android.settings.notification.modes;
18 
19 import android.util.Log;
20 
21 import com.google.common.util.concurrent.FutureCallback;
22 import com.google.common.util.concurrent.Futures;
23 import com.google.common.util.concurrent.ListenableFuture;
24 
25 import java.util.concurrent.Executor;
26 import java.util.function.Consumer;
27 
28 class FutureUtil {
29 
30     private static final String TAG = "ZenFutureUtil";
31 
whenDone(ListenableFuture<V> future, Consumer<V> consumer, Executor executor)32     static <V> void whenDone(ListenableFuture<V> future, Consumer<V> consumer, Executor executor) {
33         whenDone(future, consumer, executor, "Error in future");
34     }
35 
whenDone(ListenableFuture<V> future, Consumer<V> consumer, Executor executor, String errorLogMessage, Object... errorLogMessageArgs)36     static <V> void whenDone(ListenableFuture<V> future, Consumer<V> consumer, Executor executor,
37             String errorLogMessage, Object... errorLogMessageArgs) {
38         Futures.addCallback(future, new FutureCallback<V>() {
39             @Override
40             public void onSuccess(V v) {
41                 consumer.accept(v);
42             }
43 
44             @Override
45             public void onFailure(Throwable throwable) {
46                 Log.e(TAG, String.format(errorLogMessage, errorLogMessageArgs), throwable);
47             }
48         }, executor);
49     }
50 }
51