summaryrefslogtreecommitdiffstats
path: root/core/java/android/util
diff options
context:
space:
mode:
authorJesse Wilson <jessewilson@google.com>2011-02-11 13:32:04 -0800
committerJesse Wilson <jessewilson@google.com>2011-02-11 15:54:28 -0800
commit56b6ad3e28f9f86fb3186c96ddd8754e190afdf0 (patch)
tree30582f159c6d5c350daeb72434ffa7799acc6735 /core/java/android/util
parentb3103093a449ba5504b255a584f4fe9968cfda71 (diff)
downloadframeworks_base-56b6ad3e28f9f86fb3186c96ddd8754e190afdf0.zip
frameworks_base-56b6ad3e28f9f86fb3186c96ddd8754e190afdf0.tar.gz
frameworks_base-56b6ad3e28f9f86fb3186c96ddd8754e190afdf0.tar.bz2
Add a new method, LruCache.remove
Change-Id: Iae78a2ed4d719d4f14a4677ecb6fe5bc823bb660 http://b/3184897
Diffstat (limited to 'core/java/android/util')
-rw-r--r--core/java/android/util/LruCache.java28
1 files changed, 25 insertions, 3 deletions
diff --git a/core/java/android/util/LruCache.java b/core/java/android/util/LruCache.java
index 2122050..5578e6a 100644
--- a/core/java/android/util/LruCache.java
+++ b/core/java/android/util/LruCache.java
@@ -51,6 +51,10 @@ import java.util.Map;
* cache.put(key, value);
* }
* }}</pre>
+ *
+ * <p>This class does not allow null to be used as a key or value. A return
+ * value of null from {@link #get}, {@link #put} or {@link #remove} is
+ * unambiguous: the key was not in the cache.
*/
public class LruCache<K, V> {
private final LinkedHashMap<K, V> map;
@@ -86,7 +90,7 @@ public class LruCache<K, V> {
*/
public synchronized final V get(K key) {
if (key == null) {
- throw new NullPointerException();
+ throw new NullPointerException("key == null");
}
V result = map.get(key);
@@ -118,7 +122,7 @@ public class LruCache<K, V> {
*/
public synchronized final V put(K key, V value) {
if (key == null || value == null) {
- throw new NullPointerException();
+ throw new NullPointerException("key == null || value == null");
}
putCount++;
@@ -133,7 +137,7 @@ public class LruCache<K, V> {
private void trimToSize(int maxSize) {
while (size > maxSize) {
- Map.Entry<K, V> toEvict = map.eldest();
+ Map.Entry<K, V> toEvict = map.eldest(); // equal to map.entrySet().iterator().next();
if (toEvict == null) {
break; // map is empty; if size is not 0 then throw an error below
}
@@ -155,6 +159,24 @@ public class LruCache<K, V> {
}
/**
+ * Removes the entry for {@code key} if it exists.
+ *
+ * @return the previous value mapped by {@code key}. Although that entry is
+ * no longer cached, it has not been passed to {@link #entryEvicted}.
+ */
+ public synchronized final V remove(K key) {
+ if (key == null) {
+ throw new NullPointerException("key == null");
+ }
+
+ V previous = map.remove(key);
+ if (previous != null) {
+ size -= safeSizeOf(key, previous);
+ }
+ return previous;
+ }
+
+ /**
* Called for entries that have reached the tail of the least recently used
* queue and are be removed. The default implementation does nothing.
*/