• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.car.internal.common;
18 
19 import android.car.builtin.util.Slogf;
20 import android.util.ArrayMap;
21 import android.util.Log;
22 
23 import java.util.ArrayList;
24 import java.util.List;
25 
26 /**
27  * A dispatch list that stores events for each client and then dispatch them to each client.
28  *
29  * This class is not thread-safe.
30  *
31  * Subclass must override {@code dispatchToClient}.
32  *
33  * @param <ClientType> The type for the client.
34  * @param <EventType> The type for the event.
35  */
36 public abstract class DispatchList<ClientType, EventType> {
37     private static final String TAG = "DispatchList";
38     private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
39 
40     private ArrayMap<ClientType, List<EventType>> mEventsByClient = new ArrayMap<>();
41 
42     /**
43      * Dispatch events to one client.
44      *
45      * Must be implemented by subclass.
46      */
dispatchToClient(ClientType client, List<EventType> events)47     protected abstract void dispatchToClient(ClientType client, List<EventType> events);
48 
49     /**
50      * Adds an event to be dispatched to a client.
51      */
addEvent(ClientType client, EventType event)52     public void addEvent(ClientType client, EventType event) {
53         if (DBG) {
54             Slogf.d(TAG, "addEvent with client: %s, event: %s", client, event);
55         }
56         var eventsToNotify = mEventsByClient.get(client);
57         if (eventsToNotify == null) {
58             eventsToNotify = new ArrayList<EventType>();
59             mEventsByClient.put(client, eventsToNotify);
60         }
61         eventsToNotify.add(event);
62     }
63 
64     /**
65      * Adds events to be dispatched to a client.
66      */
addEvents(ClientType client, List<EventType> events)67     public void addEvents(ClientType client, List<EventType> events) {
68         if (DBG) {
69             Slogf.d(TAG, "addEvent with client: %s, events: %s", client, events);
70         }
71         var eventsToNotify = mEventsByClient.get(client);
72         if (eventsToNotify == null) {
73             eventsToNotify = new ArrayList<EventType>();
74             mEventsByClient.put(client, eventsToNotify);
75         }
76         eventsToNotify.addAll(events);
77     }
78 
79     /**
80      * Dispatches all the added events to clients.
81      *
82      * This clears all the added events after dispatching.
83      */
dispatchToClients()84     public void dispatchToClients() {
85         for (int i = 0; i < mEventsByClient.size(); i++) {
86             if (DBG) {
87                 Slogf.d(TAG, "dispatch to client: %s, events: %s",
88                         mEventsByClient.valueAt(i), mEventsByClient.valueAt(i));
89             }
90 
91             dispatchToClient(mEventsByClient.keyAt(i), mEventsByClient.valueAt(i));
92         }
93         mEventsByClient.clear();
94     }
95 }
96