01: package fr.aliacom.util;
02:
03: import java.util.LinkedHashMap;
04:
05: /**
06: * This hashmap implements LRU cache policy.
07: * The eldest entries will be automagically removed
08: * so that the number of elements is always lesser or equal
09: * to the specified maxsize.
10: *
11: * @author tom
12: *
13: * (C) 2001, 2002 Thomas Cataldo
14: */
15: public class LRUCache extends LinkedHashMap {
16: private int maxsize;
17:
18: /**
19: * Creates an hashmap that will cache <code>maxsize</code> elements.
20: * The eldest elements are automagically removed (LRU eviction policy).
21: *
22: * @param maxsize
23: */
24: public LRUCache(int maxsize) {
25: super (maxsize * 4 / 3 + 1, 0.75f, true);
26: this .maxsize = maxsize;
27: }
28:
29: protected boolean removeEldestEntry() {
30: return size() > maxsize;
31: }
32: }
|