LruCache.java revision 8e63c6994ea91c4a9ab3e68a60fcf6de7aab5565
1/*
2 * Copyright (C) 2011 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 android.support.v4.util;
18
19import java.util.LinkedHashMap;
20import java.util.Map;
21
22// STOPSHIP replace "Honeycomb MR1" with numbered release 2x below
23
24/**
25 * Static library version of {@link android.util.LruCache}. Used to write apps
26 * that run on platforms prior to Android Honeycomb MR1. When running on
27 * Honeycomb MR1 or above, this implementation is still used; it does not try to
28 * switch to the framework's implementation.  See the framework SDK
29 * documentation for a class overview.
30 */
31public class LruCache<K, V> {
32    private final LinkedHashMap<K, V> map;
33
34    /** Size of this cache in units. Not necessarily the number of elements. */
35    private int size;
36    private int maxSize;
37
38    private int putCount;
39    private int createCount;
40    private int evictionCount;
41    private int hitCount;
42    private int missCount;
43
44    /**
45     * @param maxSize for caches that do not override {@link #sizeOf}, this is
46     *     the maximum number of entries in the cache. For all other caches,
47     *     this is the maximum sum of the sizes of the entries in this cache.
48     */
49    public LruCache(int maxSize) {
50        if (maxSize <= 0) {
51            throw new IllegalArgumentException("maxSize <= 0");
52        }
53        this.maxSize = maxSize;
54        this.map = new LinkedHashMap<K, V>(0, 0.75f, true);
55    }
56
57    /**
58     * Returns the value for {@code key} if it exists in the cache or can be
59     * created by {@code #create}. If a value was returned, it is moved to the
60     * head of the queue. This returns null if a value is not cached and cannot
61     * be created.
62     */
63    public final V get(K key) {
64        if (key == null) {
65            throw new NullPointerException("key == null");
66        }
67
68        V mapValue;
69        synchronized (this) {
70            mapValue = map.get(key);
71            if (mapValue != null) {
72                hitCount++;
73                return mapValue;
74            }
75            missCount++;
76        }
77
78        /*
79         * Attempt to create a value. This may take a long time, and the map
80         * may be different when create() returns. If a conflicting value was
81         * added to the map while create() was working, we leave that value in
82         * the map and release the created value.
83         */
84
85        V createdValue = create(key);
86        if (createdValue == null) {
87            return null;
88        }
89
90        synchronized (this) {
91            createCount++;
92            mapValue = map.put(key, createdValue);
93
94            if (mapValue != null) {
95                // There was a conflict so undo that last put
96                map.put(key, mapValue);
97            } else {
98                size += safeSizeOf(key, createdValue);
99            }
100        }
101
102        if (mapValue != null) {
103            entryRemoved(false, key, createdValue, mapValue);
104            return mapValue;
105        } else {
106            trimToSize(maxSize);
107            return createdValue;
108        }
109    }
110
111    /**
112     * Caches {@code value} for {@code key}. The value is moved to the head of
113     * the queue.
114     *
115     * @return the previous value mapped by {@code key}.
116     */
117    public final V put(K key, V value) {
118        if (key == null || value == null) {
119            throw new NullPointerException("key == null || value == null");
120        }
121
122        V previous;
123        synchronized (this) {
124            putCount++;
125            size += safeSizeOf(key, value);
126            previous = map.put(key, value);
127            if (previous != null) {
128                size -= safeSizeOf(key, previous);
129            }
130        }
131
132        if (previous != null) {
133            entryRemoved(false, key, previous, value);
134        }
135
136        trimToSize(maxSize);
137        return previous;
138    }
139
140    /**
141     * @param maxSize the maximum size of the cache before returning. May be -1
142     *     to evict even 0-sized elements.
143     */
144    private void trimToSize(int maxSize) {
145        while (true) {
146            K key;
147            V value;
148            synchronized (this) {
149                if (size < 0 || (map.isEmpty() && size != 0)) {
150                    throw new IllegalStateException(getClass().getName()
151                            + ".sizeOf() is reporting inconsistent results!");
152                }
153
154                if (size <= maxSize || map.isEmpty()) {
155                    break;
156                }
157
158                Map.Entry<K, V> toEvict = map.entrySet().iterator().next();
159                key = toEvict.getKey();
160                value = toEvict.getValue();
161                map.remove(key);
162                size -= safeSizeOf(key, value);
163                evictionCount++;
164            }
165
166            entryRemoved(true, key, value, null);
167        }
168    }
169
170    /**
171     * Removes the entry for {@code key} if it exists.
172     *
173     * @return the previous value mapped by {@code key}.
174     */
175    public final V remove(K key) {
176        if (key == null) {
177            throw new NullPointerException("key == null");
178        }
179
180        V previous;
181        synchronized (this) {
182            previous = map.remove(key);
183            if (previous != null) {
184                size -= safeSizeOf(key, previous);
185            }
186        }
187
188        if (previous != null) {
189            entryRemoved(false, key, previous, null);
190        }
191
192        return previous;
193    }
194
195    /**
196     * Called for entries that have been evicted or removed. This method is
197     * invoked when a value is evicted to make space, removed by a call to
198     * {@link #remove}, or replaced by a call to {@link #put}. The default
199     * implementation does nothing.
200     *
201     * <p>The method is called without synchronization: other threads may
202     * access the cache while this method is executing.
203     *
204     * @param evicted true if the entry is being removed to make space, false
205     *     if the removal was caused by a {@link #put} or {@link #remove}.
206     * @param newValue the new value for {@code key}, if it exists. If non-null,
207     *     this removal was caused by a {@link #put}. Otherwise it was caused by
208     *     an eviction or a {@link #remove}.
209     */
210    protected void entryRemoved(boolean evicted, K key, V oldValue, V newValue) {}
211
212    /**
213     * Called after a cache miss to compute a value for the corresponding key.
214     * Returns the computed value or null if no value can be computed. The
215     * default implementation returns null.
216     *
217     * <p>The method is called without synchronization: other threads may
218     * access the cache while this method is executing.
219     *
220     * <p>If a value for {@code key} exists in the cache when this method
221     * returns, the created value will be released with {@link #entryRemoved}
222     * and discarded. This can occur when multiple threads request the same key
223     * at the same time (causing multiple values to be created), or when one
224     * thread calls {@link #put} while another is creating a value for the same
225     * key.
226     */
227    protected V create(K key) {
228        return null;
229    }
230
231    private int safeSizeOf(K key, V value) {
232        int result = sizeOf(key, value);
233        if (result < 0) {
234            throw new IllegalStateException("Negative size: " + key + "=" + value);
235        }
236        return result;
237    }
238
239    /**
240     * Returns the size of the entry for {@code key} and {@code value} in
241     * user-defined units.  The default implementation returns 1 so that size
242     * is the number of entries and max size is the maximum number of entries.
243     *
244     * <p>An entry's size must not change while it is in the cache.
245     */
246    protected int sizeOf(K key, V value) {
247        return 1;
248    }
249
250    /**
251     * Clear the cache, calling {@link #entryRemoved} on each removed entry.
252     */
253    public final void evictAll() {
254        trimToSize(-1); // -1 will evict 0-sized elements
255    }
256
257    /**
258     * For caches that do not override {@link #sizeOf}, this returns the number
259     * of entries in the cache. For all other caches, this returns the sum of
260     * the sizes of the entries in this cache.
261     */
262    public synchronized final int size() {
263        return size;
264    }
265
266    /**
267     * For caches that do not override {@link #sizeOf}, this returns the maximum
268     * number of entries in the cache. For all other caches, this returns the
269     * maximum sum of the sizes of the entries in this cache.
270     */
271    public synchronized final int maxSize() {
272        return maxSize;
273    }
274
275    /**
276     * Returns the number of times {@link #get} returned a value.
277     */
278    public synchronized final int hitCount() {
279        return hitCount;
280    }
281
282    /**
283     * Returns the number of times {@link #get} returned null or required a new
284     * value to be created.
285     */
286    public synchronized final int missCount() {
287        return missCount;
288    }
289
290    /**
291     * Returns the number of times {@link #create(Object)} returned a value.
292     */
293    public synchronized final int createCount() {
294        return createCount;
295    }
296
297    /**
298     * Returns the number of times {@link #put} was called.
299     */
300    public synchronized final int putCount() {
301        return putCount;
302    }
303
304    /**
305     * Returns the number of values that have been evicted.
306     */
307    public synchronized final int evictionCount() {
308        return evictionCount;
309    }
310
311    /**
312     * Returns a copy of the current contents of the cache, ordered from least
313     * recently accessed to most recently accessed.
314     */
315    public synchronized final Map<K, V> snapshot() {
316        return new LinkedHashMap<K, V>(map);
317    }
318
319    @Override public synchronized final String toString() {
320        int accesses = hitCount + missCount;
321        int hitPercent = accesses != 0 ? (100 * hitCount / accesses) : 0;
322        return String.format("LruCache[maxSize=%d,hits=%d,misses=%d,hitRate=%d%%]",
323                maxSize, hitCount, missCount, hitPercent);
324    }
325}
326