The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Tom Taylor | 5e342fa | 2010-01-28 15:54:15 -0800 | [diff] [blame^] | 18 | package com.android.mmscommon.mms.util; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | |
| 20 | import android.util.Config; |
| 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 = DEBUG ? Config.LOGD : Config.LOGV; |
| 29 | |
| 30 | private static final int MAX_CACHED_ITEMS = 500; |
| 31 | |
| 32 | private final HashMap<K, CacheEntry<V>> mCacheMap; |
| 33 | |
| 34 | protected AbstractCache() { |
| 35 | mCacheMap = new HashMap<K, CacheEntry<V>>(); |
| 36 | } |
| 37 | |
| 38 | public boolean put(K key, V value) { |
| 39 | if (LOCAL_LOGV) { |
| 40 | Log.v(TAG, "Trying to put " + key + " into cache."); |
| 41 | } |
| 42 | |
| 43 | if (mCacheMap.size() >= MAX_CACHED_ITEMS) { |
| 44 | // TODO Should remove the oldest or least hit cached entry |
| 45 | // and then cache the new one. |
| 46 | if (LOCAL_LOGV) { |
| 47 | Log.v(TAG, "Failed! size limitation reached."); |
| 48 | } |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | if (key != null) { |
| 53 | CacheEntry<V> cacheEntry = new CacheEntry<V>(); |
| 54 | cacheEntry.value = value; |
| 55 | mCacheMap.put(key, cacheEntry); |
| 56 | |
| 57 | if (LOCAL_LOGV) { |
| 58 | Log.v(TAG, key + " cached, " + mCacheMap.size() + " items total."); |
| 59 | } |
| 60 | return true; |
| 61 | } |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | public V get(K key) { |
| 66 | if (LOCAL_LOGV) { |
| 67 | Log.v(TAG, "Trying to get " + key + " from cache."); |
| 68 | } |
| 69 | |
| 70 | if (key != null) { |
| 71 | CacheEntry<V> cacheEntry = mCacheMap.get(key); |
| 72 | if (cacheEntry != null) { |
| 73 | cacheEntry.hit++; |
| 74 | if (LOCAL_LOGV) { |
| 75 | Log.v(TAG, key + " hit " + cacheEntry.hit + " times."); |
| 76 | } |
| 77 | return cacheEntry.value; |
| 78 | } |
| 79 | } |
| 80 | return null; |
| 81 | } |
| 82 | |
| 83 | public V purge(K key) { |
| 84 | if (LOCAL_LOGV) { |
| 85 | Log.v(TAG, "Trying to purge " + key); |
| 86 | } |
| 87 | |
| 88 | CacheEntry<V> v = mCacheMap.remove(key); |
| 89 | |
| 90 | if (LOCAL_LOGV) { |
| 91 | Log.v(TAG, mCacheMap.size() + " items cached."); |
| 92 | } |
| 93 | |
| 94 | return v != null ? v.value : null; |
| 95 | } |
| 96 | |
| 97 | public void purgeAll() { |
| 98 | if (LOCAL_LOGV) { |
| 99 | Log.v(TAG, "Purging cache, " + mCacheMap.size() |
| 100 | + " items dropped."); |
| 101 | } |
| 102 | mCacheMap.clear(); |
| 103 | } |
| 104 | |
| 105 | public int size() { |
| 106 | return mCacheMap.size(); |
| 107 | } |
| 108 | |
| 109 | private static class CacheEntry<V> { |
| 110 | int hit; |
| 111 | V value; |
| 112 | } |
| 113 | } |