wt.templateutil.processor
Class MostRecentlyUsedCache

java.lang.Object
  extended bywt.templateutil.processor.MostRecentlyUsedCache
All Implemented Interfaces:
Externalizable, Serializable
Direct Known Subclasses:
ServletSessionCookieManager.MostRecentlyUsedCookieIds

public abstract class MostRecentlyUsedCache
extends Object
implements Externalizable

This class is intended to be used as a most-recently-used cache. Extend this class with an anonymous inner class and provide the method. The values stored in the cache can be null, but the key used to store the values cannot be null.

This class does not do any synchronization.

See Also:
Serialized Form

Nested Class Summary
private  class MostRecentlyUsedCache.LeastRecentlyUsedIterator
           
private static class MostRecentlyUsedCache.ValueHolder
           
 
Field Summary
private  Map cache
          Map that stores MostRecentlyUsedCache.ValueHolders keyed by MostRecentlyUsedCache.ValueHolder.key.
private  MostRecentlyUsedCache.ValueHolder listHead
          Dummy head node of the doubly-linked list.
 
Constructor Summary
MostRecentlyUsedCache()
          Default constructor.
MostRecentlyUsedCache(int initialCapacity)
          Constructor.
MostRecentlyUsedCache(int initialCapacity, float loadFactor)
          Constructor.
 
Method Summary
private  void addMostRecentlyUsed(MostRecentlyUsedCache.ValueHolder holder)
           
 Map.Entry cache(Object key, Object value)
          Add the value to the cache, storing it under the specified key.
 void clear()
          Remove all entries from the cache.
private  void clearList()
           
private  MostRecentlyUsedCache.ValueHolder getLeastRecentlyUsed()
           
private  void insertAfter(MostRecentlyUsedCache.ValueHolder previous, MostRecentlyUsedCache.ValueHolder holder)
           
private  boolean isListEmpty()
           
 Iterator iterator()
          Returns an MostRecentlyUsedCache.LeastRecentlyUsedIterator that iterates on the Map.Entry elements stored in this cache, in order of least recently used to most recently used.
 Object lookup(Object key)
          Look in the cache to find a cached value.
 void readExternal(ObjectInput in)
           
protected abstract  boolean removeEldestEntry(Map.Entry eldest)
          Override this method as described in LinkedHashMap.removeEldestEntry(java.util.Map.Entry).
private  void removeFromList(MostRecentlyUsedCache.ValueHolder holder)
           
 int size()
          Get the number of entries currently in the cache.
 Object uncache(Object key)
          Remove a cached value from the cache if there is a value is cached under key.
 void writeExternal(ObjectOutput out)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cache

private Map cache
Map that stores MostRecentlyUsedCache.ValueHolders keyed by MostRecentlyUsedCache.ValueHolder.key.


listHead

private MostRecentlyUsedCache.ValueHolder listHead
Dummy head node of the doubly-linked list. listHead.previous ==> most recently used. listHead.next ==> least recently used.

Constructor Detail

MostRecentlyUsedCache

public MostRecentlyUsedCache()
Default constructor.


MostRecentlyUsedCache

public MostRecentlyUsedCache(int initialCapacity)
Constructor.

See Also:
HashMap.HashMap(int)

MostRecentlyUsedCache

public MostRecentlyUsedCache(int initialCapacity,
                             float loadFactor)
Constructor.

See Also:
HashMap.HashMap(int, float)
Method Detail

removeEldestEntry

protected abstract boolean removeEldestEntry(Map.Entry eldest)
Override this method as described in LinkedHashMap.removeEldestEntry(java.util.Map.Entry).


size

public int size()
Get the number of entries currently in the cache.

Returns:
The number of entries currently in the cache.

iterator

public Iterator iterator()
Returns an MostRecentlyUsedCache.LeastRecentlyUsedIterator that iterates on the Map.Entry elements stored in this cache, in order of least recently used to most recently used. If this cache is modified, even by a call to lookup(java.lang.Object), any iterators are invalidated and must not be used. But unlike the standard Java collection iterators, this iterator does try and catch this error.


cache

public Map.Entry cache(Object key,
                       Object value)
Add the value to the cache, storing it under the specified key. removeEldestEntry(java.util.Map.Entry) will be called after this cache has been updated and can determine if the eldest entry in this cache should be removed. If removeEldestEntry(java.util.Map.Entry) returns true, the entry will be removed from this cache and returned by this method. If removeEldestEntry(java.util.Map.Entry) returns false, this method will not remove the entry and will return null.

Parameters:
key - The key value under which the value is stored.
value - The value to store in the cache.

lookup

public Object lookup(Object key)
Look in the cache to find a cached value.

Parameters:
key - The key value to use to lookup the cached value.
Returns:
The cached value or null if the cache does not hold any value under key.

uncache

public Object uncache(Object key)
Remove a cached value from the cache if there is a value is cached under key.

Parameters:
key - The key whose cached value is removed from the cache.
Returns:
The value removed from the cache or null if no value was cached under key.

clear

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


isListEmpty

private boolean isListEmpty()

clearList

private void clearList()

getLeastRecentlyUsed

private MostRecentlyUsedCache.ValueHolder getLeastRecentlyUsed()

addMostRecentlyUsed

private void addMostRecentlyUsed(MostRecentlyUsedCache.ValueHolder holder)

insertAfter

private void insertAfter(MostRecentlyUsedCache.ValueHolder previous,
                         MostRecentlyUsedCache.ValueHolder holder)

removeFromList

private void removeFromList(MostRecentlyUsedCache.ValueHolder holder)

writeExternal

public void writeExternal(ObjectOutput out)
                   throws IOException
Specified by:
writeExternal in interface Externalizable
Throws:
IOException

readExternal

public void readExternal(ObjectInput in)
                  throws IOException,
                         ClassNotFoundException
Specified by:
readExternal in interface Externalizable
Throws:
IOException
ClassNotFoundException