24:
25: import org.apache.commons.collections.BoundedMap;
26:
27: /**
28: * A
Map implementation with a fixed maximum size which removes29: * the least recently used entry if an entry is added when full.
30: *
31: * The least recently used algorithm works on the get and put operations only.
32: * Iteration of any kind, including setting the value by iteration, does not
33: * change the order. Queries such as containsKey and containsValue or access
34: * via views also do not change the order.
35: *
36: * The map implements OrderedMap and entries may be queried using
37: * the bidirectional OrderedMapIterator. The order returned is
38: * least recently used to most recently used. Iterators from map views can
39: * also be cast to OrderedIterator if required.
40: *
41: * All the available iterators can be reset back to the start by casting to
42: * ResettableIterator and calling reset().
43: *
44: * Note that LRUMap is not synchronized and is not thread-safe.
45: * If you wish to use this map from multiple threads concurrently, you must use
46: * appropriate synchronization. The simplest approach is to wrap this map
47: * using {@link java.util.Collections#synchronizedMap(Map)}. This class may throw
48: * NullPointerException's when accessed by concurrent threads.
49: *
50: * @since Commons Collections 3.0 (previously in main package v1.0)
51: * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
52: *
53: * @author James Strachan
54: * @author Morgan Delagrange
55: * @author Stephen Colebourne
56: * @author Mike Pettypiece
57: * @author Mario Ivankovits
58: */
59: public class LRUMap
60: extends AbstractLinkedMap implements BoundedMap, Serializable, Cloneable {
61:
62: /** Serialisation version */
63: private static final long serialVersionUID = -612114643488955218L;
64: /** Default maximum size */
66:
67: /** Maximum size */
68: private transient int maxSize;
69: /** Scan behaviour */
70: private boolean scanUntilRemovable;
71:
72: /**
73: * Constructs a new empty map with a maximum size of 100.
74: */
75: public LRUMap() {
76: this(DEFAULT_MAX_SIZE, DEFAULT_LOAD_FACTOR, false);
77: }
78:
79: /**
80: * Constructs a new, empty map with the specified maximum size.
81: *
82: * @param maxSize the maximum size of the map
83: * @throws IllegalArgumentException if the maximum size is less than one
84: */
85: public LRUMap(int maxSize) {
86: this(maxSize, DEFAULT_LOAD_FACTOR);
87: }
88:
89: /**
90: * Constructs a new, empty map with the specified maximum size.
91: *
92: * @param maxSize the maximum size of the map
93: * @param scanUntilRemovable scan until a removeable entry is found, default false
94: * @throws IllegalArgumentException if the maximum size is less than one
95: * @since Commons Collections 3.1
96: */
97: public LRUMap(int maxSize, boolean scanUntilRemovable) {
98: this(maxSize, DEFAULT_LOAD_FACTOR, scanUntilRemovable);
99: }
100:
101: /**
102: * Constructs a new, empty map with the specified initial capacity and
103: * load factor.
104: *
105: * @param maxSize the maximum size of the map, -1 for no limit,
106: * @param loadFa