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 package com.android.quickstep.util;
17 
18 import com.android.systemui.shared.recents.model.Task;
19 
20 import java.util.function.Predicate;
21 
22 /**
23  * An interface for caching task id and its corresponding object (e.g. thumbnail, task icon)
24  *
25  * @param <V> Type of object stored in the cache
26  */
27 public interface TaskKeyCache<V> {
28 
29     /**
30      * Removes all entries from the cache.
31      */
evictAll()32     void evictAll();
33 
34     /**
35      * Removes a particular entry from the cache.
36      */
remove(Task.TaskKey key)37     void remove(Task.TaskKey key);
38 
39     /**
40      * Removes all entries matching keyCheck.
41      */
removeAll(Predicate<Task.TaskKey> keyCheck)42     void removeAll(Predicate<Task.TaskKey> keyCheck);
43 
44     /**
45      * Gets the entry if it is still valid.
46      */
getAndInvalidateIfModified(Task.TaskKey key)47     V getAndInvalidateIfModified(Task.TaskKey key);
48 
49     /**
50      * Adds an entry to the cache, optionally evicting the last accessed entry.
51      */
put(Task.TaskKey key, V value)52     void put(Task.TaskKey key, V value);
53 
54     /**
55      * Updates the cache entry if it is already present in the cache.
56      */
updateIfAlreadyInCache(int taskId, V data)57     void updateIfAlreadyInCache(int taskId, V data);
58 
59     /**
60      * Updates cache size and remove excess if the number of existing entries is larger than new
61      * cache size.
62      */
updateCacheSizeAndRemoveExcess(int cacheSize)63     default void updateCacheSizeAndRemoveExcess(int cacheSize) { }
64 
65     /**
66      * Gets maximum size of the cache.
67      */
getMaxSize()68     int getMaxSize();
69 
70     /**
71      * Gets current size of the cache.
72      */
getSize()73     int getSize();
74 
75     class Entry<V> {
76 
77         final Task.TaskKey mKey;
78         V mValue;
79 
Entry(Task.TaskKey key, V value)80         Entry(Task.TaskKey key, V value) {
81             mKey = key;
82             mValue = value;
83         }
84 
85         @Override
hashCode()86         public int hashCode() {
87             return mKey.id;
88         }
89     }
90 }
91