net.sourceforge.rcache.decorator
Class MruGuard<K,V>

java.lang.Object
  extended by net.sourceforge.rcache.decorator.MruGuard<K,V>
Type Parameters:
K - the type of keys maintained by this cache
V - the type of cached values
All Implemented Interfaces:
Cache<K,V>

public final class MruGuard<K,V>
extends java.lang.Object
implements Cache<K,V>

Decorator for Cache instances that adds an MRU list containing hard references to the most recently used entries.

This decorator prevents the most recently used entries from being garbage collected.

Author:
Rodrigo Ruiz

Nested Class Summary
 
Nested classes/interfaces inherited from interface net.sourceforge.rcache.Cache
Cache.Operation
 
Field Summary
 
Fields inherited from interface net.sourceforge.rcache.Cache
DEFAULT_CONCURRENCY_LEVEL, DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR
 
Constructor Summary
MruGuard(Cache<K,V> cache, int mruSize)
          Creates an instance.
 
Method Summary
 void clear()
          Removes all entries from this cache.
 V get(java.lang.Object key)
          Returns the value to which this cache maps the specified key.
 int getMaxSize()
          Gets the maximum size of the MRU list.
protected  java.util.Iterator<V> mruIterator()
          Gets an iterator for the internal MRU list.
 V put(K key, V value)
          Associates the specified value with the specified key in this cache, but only if no value is associated yet.
 V remove(java.lang.Object key)
          Removes the entry for this key from this cache if present.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MruGuard

public MruGuard(Cache<K,V> cache,
                int mruSize)
Creates an instance.

Parameters:
cache - The cache instance to wrap
mruSize - The number of items to maintain in the MRU list
Method Detail

get

public V get(java.lang.Object key)

Returns the value to which this cache maps the specified key. Returns null if the cache contains no entry for this key. A return value of null does not necessarily indicate that the cache contains no entry for the key; it's also possible that the cache explicitly maps the key to null

.

Although this method should use K for the key type, it is left as Object to keep this interface compatible with Map.

Specified by:
get in interface Cache<K,V>
Parameters:
key - key whose associated value is to be returned.
Returns:
the value to which this cache associates the specified key.

put

public V put(K key,
             V value)

Associates the specified value with the specified key in this cache, but only if no value is associated yet. If the cache previously contained an entry for this key, the old value is returned, but not replaced.

This method is equivalent to the putIfAbsent from the ConcurrentMap class, as it is more appropriate for a cache than the original one from Map.

Specified by:
put in interface Cache<K,V>
Parameters:
key - key with which the specified value is to be associated.
value - value to be associated with the specified key.
Returns:
the value associated with specified key. If the key was already associated with a value, the old value is returned. Otherwise, value is returned

remove

public V remove(java.lang.Object key)

Removes the entry for this key from this cache if present.

Although this method should use K for the key type, it is left as Object to keep this interface compatible with Map.

Specified by:
remove in interface Cache<K,V>
Parameters:
key - key whose entry is to be removed from the cache.
Returns:
previous value associated with specified key, or null if there was no entry for key. (A null return can also indicate that the cache previously associated null with the specified key)

clear

public void clear()

Removes all entries from this cache.

This method must be handled very carefully, as it will break the assumption of unique instances for cached factories.

Specified by:
clear in interface Cache<K,V>

getMaxSize

public int getMaxSize()
Gets the maximum size of the MRU list.

Returns:
the MRU list max size

mruIterator

protected java.util.Iterator<V> mruIterator()
Gets an iterator for the internal MRU list.

This method is for test purposes only.

Returns:
An iterator for the MRU list


© 2007-2009 Rodrigo Ruiz
This site is hosted by