1 /*
2  * Copyright (C) 2008 Esmertec AG.
3  * Copyright (C) 2008 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package com.google.android.mms.util;
19 
20 import android.compat.annotation.UnsupportedAppUsage;
21 import android.util.Log;
22 
23 import java.util.HashMap;
24 
25 public abstract class AbstractCache<K, V> {
26     private static final String TAG = "AbstractCache";
27     private static final boolean DEBUG = false;
28     private static final boolean LOCAL_LOGV = false;
29 
30     private static final int MAX_CACHED_ITEMS  = 500;
31 
32     private final HashMap<K, CacheEntry<V>> mCacheMap;
33 
34     @UnsupportedAppUsage
AbstractCache()35     protected AbstractCache() {
36         mCacheMap = new HashMap<K, CacheEntry<V>>();
37     }
38 
39     @UnsupportedAppUsage
put(K key, V value)40     public boolean put(K key, V value) {
41         if (LOCAL_LOGV) {
42             Log.v(TAG, "Trying to put " + key + " into cache.");
43         }
44 
45         if (mCacheMap.size() >= MAX_CACHED_ITEMS) {
46             // TODO Should remove the oldest or least hit cached entry
47             // and then cache the new one.
48             if (LOCAL_LOGV) {
49                 Log.v(TAG, "Failed! size limitation reached.");
50             }
51             return false;
52         }
53 
54         if (key != null) {
55             CacheEntry<V> cacheEntry = new CacheEntry<V>();
56             cacheEntry.value = value;
57             mCacheMap.put(key, cacheEntry);
58 
59             if (LOCAL_LOGV) {
60                 Log.v(TAG, key + " cached, " + mCacheMap.size() + " items total.");
61             }
62             return true;
63         }
64         return false;
65     }
66 
67     @UnsupportedAppUsage
get(K key)68     public V get(K key) {
69         if (LOCAL_LOGV) {
70             Log.v(TAG, "Trying to get " + key + " from cache.");
71         }
72 
73         if (key != null) {
74             CacheEntry<V> cacheEntry = mCacheMap.get(key);
75             if (cacheEntry != null) {
76                 cacheEntry.hit++;
77                 if (LOCAL_LOGV) {
78                     Log.v(TAG, key + " hit " + cacheEntry.hit + " times.");
79                 }
80                 return cacheEntry.value;
81             }
82         }
83         return null;
84     }
85 
86     @UnsupportedAppUsage
purge(K key)87     public V purge(K key) {
88         if (LOCAL_LOGV) {
89             Log.v(TAG, "Trying to purge " + key);
90         }
91 
92         CacheEntry<V> v = mCacheMap.remove(key);
93 
94         if (LOCAL_LOGV) {
95             Log.v(TAG, mCacheMap.size() + " items cached.");
96         }
97 
98         return v != null ? v.value : null;
99     }
100 
101     @UnsupportedAppUsage
purgeAll()102     public void purgeAll() {
103         if (LOCAL_LOGV) {
104             Log.v(TAG, "Purging cache, " + mCacheMap.size()
105                     + " items dropped.");
106         }
107         mCacheMap.clear();
108     }
109 
size()110     public int size() {
111         return mCacheMap.size();
112     }
113 
114     private static class CacheEntry<V> {
115         int hit;
116         V value;
117     }
118 }
119