public class LRUMap<K,V>
extends java.util.AbstractMap<K,V>
implements java.lang.Cloneable, java.io.Serializable
An LRUMap implements a Map of a fixed maximum size that enforces a least recently used discard policy. When the LRUMap is full (i.e., contains the maximum number of entries), any attempt to insert a new entry causes one of the least recently used entries to be discarded.
Note:
Map m = Collections.synchronizedMap (new LRUMap (...));
There are other, similar implementations. For instance, see the LRUMap class in the Apache Jakarta Commons Collection API. (This leads to the obvious question: Why write another one? The primary answer is that I did not want to add another third-party library dependency. Plus, I wanted to experiment with this algorithm.)
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_INITIAL_CAPACITY
The default initial capacity, if one isn't specified to the
constructor
|
static float |
DEFAULT_LOAD_FACTOR
The default load factor, if one isn't specified to the constructor
|
Constructor and Description |
---|
LRUMap(int maxCapacity)
Construct a new empty map with a default capacity and load factor,
and the specified maximum capacity.
|
LRUMap(int initialCapacity,
float loadFactor,
int maxCapacity)
Constructs a new, empty map with the specified initial capacity,
load factor, and maximum capacity.
|
LRUMap(int initialCapacity,
int maxCapacity)
Construct a new empty map with the specified initial capacity,
a default load factor, and the specified maximum capacity.
|
LRUMap(LRUMap<? extends K,? extends V> map)
Constructs a new map with the same mappings and parameters as the
given LRUMap.
|
Modifier and Type | Method and Description |
---|---|
void |
addRemovalListener(ObjectRemovalListener listener,
boolean automaticOnly)
Add an EventListener that will be called whenever an
object is removed from the cache.
|
void |
clear()
Remove all mappings from this map.
|
protected java.lang.Object |
clone()
Returns a shallow copy of this instance.
|
boolean |
containsKey(java.lang.Object key)
Determine whether this map contains a mapping for a given key.
|
boolean |
containsValue(java.lang.Object value)
Determine whether this map contains a given value.
|
java.util.Set<java.util.Map.Entry<K,V>> |
entrySet()
Get a set view of the mappings in this map.
|
V |
get(java.lang.Object key)
Retrieve an object from the map.
|
int |
getInitialCapacity()
Get the initial capacity of this LRUMap.
|
float |
getLoadFactor()
Get the load factor for this LRUMap.
|
int |
getMaximumCapacity()
Get the maximum capacity of this LRUMap.
|
boolean |
isEmpty()
Determine whether this map is empty or not.
|
java.util.Set<K> |
keySet()
Return a Set view of the keys in the map.
|
V |
put(K key,
V value)
Associates the specified value with the specified key in this map.
|
void |
putAll(java.util.Map<? extends K,? extends V> map)
Copies all of the mappings from a specified map to this one.
|
V |
remove(java.lang.Object key)
Removes the mapping for a key, if there is one.
|
boolean |
removeRemovalListener(ObjectRemovalListener listener)
Remove an EventListener from the set of listeners to be invoked
when an object is removed from the cache.
|
int |
setMaximumCapacity(int newCapacity)
Set or change the maximum capacity of this LRUMap.
|
int |
size()
Get the number of entries in the map.
|
java.util.Collection<V> |
values()
Returns a collection view of the values contained in this map.
|
public static final float DEFAULT_LOAD_FACTOR
public static final int DEFAULT_INITIAL_CAPACITY
public LRUMap(int maxCapacity)
maxCapacity
- the maximum number of entries permitted in the
map. Must not be negative.public LRUMap(int initialCapacity, int maxCapacity)
initialCapacity
- the initial capacitymaxCapacity
- the maximum number of entries permitted in the
map. Must not be negative.public LRUMap(int initialCapacity, float loadFactor, int maxCapacity)
initialCapacity
- the initial capacityloadFactor
- the load factormaxCapacity
- the maximum number of entries permitted in the
map. Must not be negative.public LRUMap(LRUMap<? extends K,? extends V> map)
map
- the map whose mappings are to be copiedpublic void addRemovalListener(ObjectRemovalListener listener, boolean automaticOnly)
Add an EventListener that will be called whenever an
object is removed from the cache. If automaticOnly is
true, then the listener is only notified for objects that
are removed automatically when the cache needs to be cleared to make
room for new objects. If automaticOnly is false,
then the listener is notified whenever an object is removed for any
reason, include a call to the remove()
method.
Note that when this map announces the removal of an object, it
passes an ObjectRemovalEvent
that contains a
java.util.Map.Entry object that wraps the actual object
that was removed. That way, both the key and the value are available
for the removed object.
listener
- the listener to addautomaticOnly
- see aboveremoveRemovalListener(org.clapper.util.misc.ObjectRemovalListener)
public boolean removeRemovalListener(ObjectRemovalListener listener)
listener
- the listener to addaddRemovalListener(org.clapper.util.misc.ObjectRemovalListener, boolean)
public void clear()
public boolean containsKey(java.lang.Object key)
public boolean containsValue(java.lang.Object value)
public java.util.Set<java.util.Map.Entry<K,V>> entrySet()
public V get(java.lang.Object key)
public int getInitialCapacity()
getLoadFactor()
,
getMaximumCapacity()
public float getLoadFactor()
getInitialCapacity()
,
getMaximumCapacity()
public int getMaximumCapacity()
setMaximumCapacity(int)
,
getLoadFactor()
,
getInitialCapacity()
public boolean isEmpty()
public java.util.Set<K> keySet()
Return a Set view of the keys in the map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. The set does supports element removal, which removes the corresponding mapping from this map; however, the Iterator returned by the set currently does not support element removal. The set does not support the add or addAll operations.
public V put(K key, V value)
put
in interface java.util.Map<K,V>
put
in class java.util.AbstractMap<K,V>
key
- the key with which the specified value is to be associatedvalue
- the value to associate with the specified keypublic void putAll(java.util.Map<? extends K,? extends V> map)
public V remove(java.lang.Object key)
public int setMaximumCapacity(int newCapacity)
newCapacity
- the new maximum capacitygetMaximumCapacity()
public int size()
public java.util.Collection<V> values()
Returns a collection view of the values contained in this map. The returned collection is a "thin" view of the values contained in this map. The collection contains proxies for the actual disk-resident values; the values themselves are not loaded until a Collection method such as contains() is called.
The collection is backed by the map, so changes to the map are reflected in the set. If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined. The set does not support any of the add() methods.
Warning:: The toArray() methods can be dangerous, since they will attempt to load every value from the data file into an in-memory array.
protected java.lang.Object clone() throws java.lang.CloneNotSupportedException