1/*
2 * Copyright (C) 2014 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
17package com.android.systemui.recents.model;
18
19import android.util.LruCache;
20
21import java.util.HashMap;
22
23/**
24 * An LRU cache that internally support querying the keys as well as values.  We use this to keep
25 * track of the task metadata to determine when to invalidate the cache when tasks have been
26 * updated. Generally, this cache will return the last known cache value for the requested task
27 * key.
28 */
29public class KeyStoreLruCache<V> {
30    // We keep a set of keys that are associated with the LRU cache, so that we can find out
31    // information about the Task that was previously in the cache.
32    HashMap<Integer, Task.TaskKey> mTaskKeys = new HashMap<Integer, Task.TaskKey>();
33    // The cache implementation, mapping task id -> value
34    LruCache<Integer, V> mCache;
35
36    public KeyStoreLruCache(int cacheSize) {
37        mCache = new LruCache<Integer, V>(cacheSize) {
38
39            @Override
40            protected void entryRemoved(boolean evicted, Integer taskId, V oldV, V newV) {
41                mTaskKeys.remove(taskId);
42            }
43        };
44    }
45
46    /** Gets a specific entry in the cache. */
47    final V get(Task.TaskKey key) {
48        return mCache.get(key.id);
49    }
50
51    /**
52     * Returns the value only if the Task has not updated since the last time it was in the cache.
53     */
54    final V getAndInvalidateIfModified(Task.TaskKey key) {
55        Task.TaskKey lastKey = mTaskKeys.get(key.id);
56        if (lastKey != null && (lastKey.lastActiveTime < key.lastActiveTime)) {
57            // The task has updated (been made active since the last time it was put into the
58            // LRU cache) so invalidate that item in the cache
59            remove(key);
60            return null;
61        }
62        // Either the task does not exist in the cache, or the last active time is the same as
63        // the key specified, so return what is in the cache
64        return mCache.get(key.id);
65    }
66
67    /** Puts an entry in the cache for a specific key. */
68    final void put(Task.TaskKey key, V value) {
69        mCache.put(key.id, value);
70        mTaskKeys.put(key.id, key);
71    }
72
73    /** Removes a cache entry for a specific key. */
74    final void remove(Task.TaskKey key) {
75        mCache.remove(key.id);
76        mTaskKeys.remove(key.id);
77    }
78
79    /** Removes all the entries in the cache. */
80    final void evictAll() {
81        mCache.evictAll();
82        mTaskKeys.clear();
83    }
84
85    /** Returns the size of the cache. */
86    final int size() {
87        return mCache.size();
88    }
89
90    /** Trims the cache to a specific size */
91    final void trimToSize(int cacheSize) {
92        mCache.resize(cacheSize);
93    }
94}
95