org.apache.felix.eventadmin.impl.util
Class LeastRecentlyUsedCacheMap

java.lang.Object
  extended by org.apache.felix.eventadmin.impl.util.LeastRecentlyUsedCacheMap
All Implemented Interfaces:
CacheMap

public class LeastRecentlyUsedCacheMap
extends java.lang.Object
implements CacheMap

This class implements a least recently used cache map. It will hold a given size of key-value pairs and drop the least recently used entry once this size is reached. This class is thread safe.

Author:
Felix Project Team
See Also:
CacheMap

Constructor Summary
LeastRecentlyUsedCacheMap(int maxSize)
          The constructor of the cache.
 
Method Summary
 void add(java.lang.Object key, java.lang.Object value)
          Add the key-value pair to the cache.
 void clear()
          Remove all entries from the cache.
 java.lang.Object get(java.lang.Object key)
          Returns the value for the key in case there is one.
 java.lang.Object remove(java.lang.Object key)
          Remove the entry denoted by key from the cache and return its value.
 int size()
          Return the current size of the cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LeastRecentlyUsedCacheMap

public LeastRecentlyUsedCacheMap(int maxSize)
The constructor of the cache. The given max size will be used to determine the size of the cache that triggers replacing least recently used entries with new ones.

Parameters:
maxSize - The max number of entries in the cache
Method Detail

get

public java.lang.Object get(java.lang.Object key)
Returns the value for the key in case there is one. Additionally, the LRU counter for the key is updated.

Specified by:
get in interface CacheMap
Parameters:
key - The key for the value to return
Returns:
The value of the key in case there is one, null otherwise
See Also:
CacheMap.get(java.lang.Object)

add

public void add(java.lang.Object key,
                java.lang.Object value)
Add the key-value pair to the cache. The key will be come the most recently used entry. In case max size is (or has been) reached this will remove the least recently used entry in the cache. In case that the cache already contains this specific key-value pair it LRU counter is updated only.

Specified by:
add in interface CacheMap
Parameters:
key - The key for the value
value - The value for the key
See Also:
CacheMap.add(java.lang.Object, java.lang.Object)

remove

public java.lang.Object remove(java.lang.Object key)
Remove the entry denoted by key from the cache and return its value.

Specified by:
remove in interface CacheMap
Parameters:
key - The key of the entry to be removed
Returns:
The value of the entry removed, null if none
See Also:
CacheMap.remove(java.lang.Object)

size

public int size()
Return the current size of the cache.

Specified by:
size in interface CacheMap
Returns:
The number of entries currently in the cache.
See Also:
CacheMap.size()

clear

public void clear()
Remove all entries from the cache.

Specified by:
clear in interface CacheMap
See Also:
CacheMap.clear()