net.tomp2p.utils
Class CacheMap<K,V>
java.lang.Object
java.util.AbstractMap<K,V>
java.util.HashMap<K,V>
java.util.LinkedHashMap<K,V>
net.tomp2p.utils.CacheMap<K,V>
- Type Parameters:
K
- The keyV
- The value
- All Implemented Interfaces:
- Serializable, Cloneable, Map<K,V>
public class CacheMap<K,V>
- extends LinkedHashMap<K,V>
The CacheMap is a LRU cache with a given capacity. The elements that do not
fit into the cache will be removed. The flag updateEntryOnInsert will
determine if put(Object, Object)
or
putIfAbsent(Object, Object)
will be used. This is useful for entries
that have timing information and that should not be updated if the same key
is going to be used. This class extends LinkedHashMap
, which means
that this class is not thread safe.
- Author:
- Thomas Bocek
- See Also:
- Serialized Form
Constructor Summary |
CacheMap(int maxEntries,
boolean updateEntryOnInsert)
Creates a new CacheMap with a fixed capacity |
CacheMap
public CacheMap(int maxEntries,
boolean updateEntryOnInsert)
- Creates a new CacheMap with a fixed capacity
- Parameters:
maxEntries
- The number of entries that can be stored in this mapupdateEntryOnInsert
- Set to true to update (overwrite) values. Set
false to not overwrite the values if there is a value present.
put
public V put(K key,
V value)
- Specified by:
put
in interface Map<K,V>
- Overrides:
put
in class HashMap<K,V>
putIfAbsent
public V putIfAbsent(K key,
V value)
- If the key is not associated with a value, associate it
with the value. This is the same as:
if (!map.containsKey(key))
{
return map.put(key, value);
}
else
{
return map.get(key);
}
- Parameters:
key
- key with which the value is to be associated.value
- value to be associated with the key.
- Returns:
- previous value associated with key, or null if there
was no mapping for this key.
removeEldestEntry
protected boolean removeEldestEntry(Map.Entry<K,V> eldest)
- Overrides:
removeEldestEntry
in class LinkedHashMap<K,V>
Copyright © 2012. All Rights Reserved.