net.tomp2p.utils
Class CacheMap<K,V>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap<K,V>
          extended by java.util.LinkedHashMap<K,V>
              extended by net.tomp2p.utils.CacheMap<K,V>
Type Parameters:
K - The key
V - 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

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
 
Constructor Summary
CacheMap(int maxEntries, boolean updateEntryOnInsert)
          Creates a new CacheMap with a fixed capacity
 
Method Summary
 V put(K key, V value)
           
 V putIfAbsent(K key, V value)
          If the key is not associated with a value, associate it with the value.
protected  boolean removeEldestEntry(Map.Entry<K,V> eldest)
           
 
Methods inherited from class java.util.LinkedHashMap
clear, containsValue, get
 
Methods inherited from class java.util.HashMap
clone, containsKey, entrySet, isEmpty, keySet, putAll, remove, size, values
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
containsKey, entrySet, equals, hashCode, isEmpty, keySet, putAll, remove, size, values
 

Constructor Detail

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 map
updateEntryOnInsert - Set to true to update (overwrite) values. Set false to not overwrite the values if there is a value present.
Method Detail

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 © 2013. All Rights Reserved.