1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.archive.util;
24
25 import java.util.LinkedHashMap;
26 import java.util.Map;
27
28
29 /***
30 * A least-recently used cache. As new entries are added to the map, the
31 * least-recently accessed entries are removed.
32 *
33 * @author pjack
34 *
35 * @param <K> The key type of the LRU
36 * @param <V> The value type of the LRU
37 */
38 public class LRU<K,V> extends LinkedHashMap<K,V> {
39
40
41 /***
42 * Generated by Eclipse.
43 */
44 private static final long serialVersionUID = 1032420936705267913L;
45
46
47 /***
48 * The maximum number of entries to store in the cache.
49 */
50 private int max;
51
52
53 /***
54 * Constructor.
55 *
56 * @param max the maximum number of entries to cache
57 */
58 public LRU(int max) {
59 super(max, (float)0.75, true);
60 this.max = max;
61 }
62
63
64 @Override
65 protected boolean removeEldestEntry(Map.Entry<K,V> entry) {
66 return size() >= max;
67 }
68
69 }