1 /*
2  * Copyright (C) 2019 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.car.connecteddevice.util;
18 
19 import android.annotation.CallbackExecutor;
20 import android.annotation.NonNull;
21 
22 import java.util.concurrent.ConcurrentHashMap;
23 import java.util.concurrent.Executor;
24 import java.util.function.Consumer;
25 
26 /**
27  * Class for invoking thread-safe callbacks.
28  *
29  * @param <T> Callback type.
30  */
31 public class ThreadSafeCallbacks<T> {
32 
33     private final ConcurrentHashMap<T, Executor> mCallbacks = new ConcurrentHashMap<>();
34 
35     /** Add a callback to be notified on its executor. */
add(@onNull T callback, @NonNull @CallbackExecutor Executor executor)36     public void add(@NonNull T callback, @NonNull @CallbackExecutor Executor executor) {
37         mCallbacks.put(callback, executor);
38     }
39 
40     /** Remove a callback from the collection. */
remove(@onNull T callback)41     public void remove(@NonNull T callback) {
42         mCallbacks.remove(callback);
43     }
44 
45     /** Clear all callbacks from the collection. */
clear()46     public void clear() {
47         mCallbacks.clear();
48     }
49 
50     /** Return the number of callbacks in collection. */
size()51     public int size() {
52         return mCallbacks.size();
53     }
54 
55     /** Invoke notification on all callbacks with their supplied {@link Executor}. */
invoke(Consumer<T> notification)56     public void invoke(Consumer<T> notification) {
57         mCallbacks.forEach((callback, executor) ->
58                 executor.execute(() -> notification.accept(callback)));
59     }
60 }
61