org.aspectj.org.eclipse.jdt.internal.core.util
Class LRUCache

java.lang.Object
  extended by org.aspectj.org.eclipse.jdt.internal.core.util.LRUCache
All Implemented Interfaces:
java.lang.Cloneable
Direct Known Subclasses:
OverflowingLRUCache

public class LRUCache
extends java.lang.Object
implements java.lang.Cloneable

The LRUCache is a hashtable that stores a finite number of elements. When an attempt is made to add values to a full cache, the least recently used values in the cache are discarded to make room for the new values as necessary.

The data structure is based on the LRU virtual memory paging scheme.

Objects can take up a variable amount of cache space by implementing the ILRUCacheable interface.

This implementation is NOT thread-safe. Synchronization wrappers would have to be added to ensure atomic insertions and deletions from the cache.

See Also:
ILRUCacheable

Nested Class Summary
protected static class LRUCache.LRUCacheEntry
          This type is used internally by the LRUCache to represent entries stored in the cache.
 class LRUCache.Stats
           
 
Field Summary
protected  int currentSpace
          Amount of cache space used so far
protected static int DEFAULT_SPACELIMIT
          Default amount of space in the cache
protected  LRUCache.LRUCacheEntry entryQueue
          Start of queue (most recently used entry)
protected  LRUCache.LRUCacheEntry entryQueueTail
          End of queue (least recently used entry)
protected  java.util.Hashtable entryTable
          Hash table for fast random access to cache entries
protected  int spaceLimit
          Maximum space allowed in cache
protected  int timestampCounter
          Counter for handing out sequential timestamps
 
Constructor Summary
LRUCache()
          Creates a new cache.
LRUCache(int size)
          Creates a new cache.
 
Method Summary
 java.lang.Object clone()
          Returns a new cache containing the same contents.
 double fillingRatio()
           
 void flush()
          Flushes all entries from the cache.
 void flush(java.lang.Object key)
          Flushes the given entry from the cache.
 java.lang.Object get(java.lang.Object key)
          Answers the value in the cache at the given key.
 int getCurrentSpace()
          Returns the amount of space that is current used in the cache.
 java.lang.Object getKey(java.lang.Object key)
           
 int getNewestTimestampCounter()
          Returns the timestamps of the most recently used element in the cache.
 java.lang.Object getOldestElement()
          Returns the lest recently used element in the cache
 int getOldestTimestampCounter()
          Returns the timestamps of the least recently used element in the cache.
 int getSpaceLimit()
          Returns the maximum amount of space available in the cache.
 java.util.Enumeration keys()
          Returns an Enumeration of the keys currently in the cache.
 ICacheEnumeration keysAndValues()
          Returns an enumeration that iterates over all the keys and values currently in the cache.
protected  boolean makeSpace(int space)
          Ensures there is the specified amount of free space in the receiver, by removing old entries if necessary.
protected  LRUCache newInstance(int size)
          Returns a new LRUCache instance
 java.lang.Object peek(java.lang.Object key)
          Answers the value in the cache at the given key.
protected  void privateAdd(java.lang.Object key, java.lang.Object value, int space)
          Adds an entry for the given key/value/space.
protected  void privateAddEntry(LRUCache.LRUCacheEntry entry, boolean shuffle)
          Adds the given entry from the receiver.
protected  void privateRemoveEntry(LRUCache.LRUCacheEntry entry, boolean shuffle)
          Removes the entry from the entry queue.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Sets the value in the cache at the given key.
 java.lang.Object removeKey(java.lang.Object key)
          Removes and returns the value in the cache for the given key.
 void setSpaceLimit(int limit)
          Sets the maximum amount of space that the cache can store
protected  int spaceFor(java.lang.Object value)
          Returns the space taken by the given value.
 java.lang.String toString()
          Returns a String that represents the value of this object.
protected  java.lang.String toStringContents()
          Returns a String that represents the contents of this object.
 java.lang.String toStringFillingRation(java.lang.String cacheName)
           
protected  void updateTimestamp(LRUCache.LRUCacheEntry entry)
          Updates the timestamp for the given entry, ensuring that the queue is kept in correct order.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

currentSpace

protected int currentSpace
Amount of cache space used so far


spaceLimit

protected int spaceLimit
Maximum space allowed in cache


timestampCounter

protected int timestampCounter
Counter for handing out sequential timestamps


entryTable

protected java.util.Hashtable entryTable
Hash table for fast random access to cache entries


entryQueue

protected LRUCache.LRUCacheEntry entryQueue
Start of queue (most recently used entry)


entryQueueTail

protected LRUCache.LRUCacheEntry entryQueueTail
End of queue (least recently used entry)


DEFAULT_SPACELIMIT

protected static final int DEFAULT_SPACELIMIT
Default amount of space in the cache

See Also:
Constant Field Values
Constructor Detail

LRUCache

public LRUCache()
Creates a new cache. Size of cache is defined by DEFAULT_SPACELIMIT.


LRUCache

public LRUCache(int size)
Creates a new cache.

Parameters:
size - Size of Cache
Method Detail

clone

public java.lang.Object clone()
Returns a new cache containing the same contents.

Overrides:
clone in class java.lang.Object
Returns:
New copy of object.

fillingRatio

public double fillingRatio()

flush

public void flush()
Flushes all entries from the cache.


flush

public void flush(java.lang.Object key)
Flushes the given entry from the cache. Does nothing if entry does not exist in cache.

Parameters:
key - Key of object to flush

getKey

public java.lang.Object getKey(java.lang.Object key)

get

public java.lang.Object get(java.lang.Object key)
Answers the value in the cache at the given key. If the value is not in the cache, returns null

Parameters:
key - Hash table key of object to retrieve
Returns:
Retreived object, or null if object does not exist

getCurrentSpace

public int getCurrentSpace()
Returns the amount of space that is current used in the cache.


getNewestTimestampCounter

public int getNewestTimestampCounter()
Returns the timestamps of the most recently used element in the cache.


getOldestTimestampCounter

public int getOldestTimestampCounter()
Returns the timestamps of the least recently used element in the cache.


getOldestElement

public java.lang.Object getOldestElement()
Returns the lest recently used element in the cache


getSpaceLimit

public int getSpaceLimit()
Returns the maximum amount of space available in the cache.


keys

public java.util.Enumeration keys()
Returns an Enumeration of the keys currently in the cache.


keysAndValues

public ICacheEnumeration keysAndValues()
Returns an enumeration that iterates over all the keys and values currently in the cache.


makeSpace

protected boolean makeSpace(int space)
Ensures there is the specified amount of free space in the receiver, by removing old entries if necessary. Returns true if the requested space was made available, false otherwise.

Parameters:
space - Amount of space to free up

newInstance

protected LRUCache newInstance(int size)
Returns a new LRUCache instance


peek

public java.lang.Object peek(java.lang.Object key)
Answers the value in the cache at the given key. If the value is not in the cache, returns null This function does not modify timestamps.


privateAdd

protected void privateAdd(java.lang.Object key,
                          java.lang.Object value,
                          int space)
Adds an entry for the given key/value/space.


privateAddEntry

protected void privateAddEntry(LRUCache.LRUCacheEntry entry,
                               boolean shuffle)
Adds the given entry from the receiver.

Parameters:
shuffle - Indicates whether we are just shuffling the queue (in which case, the entry table is not modified).

privateRemoveEntry

protected void privateRemoveEntry(LRUCache.LRUCacheEntry entry,
                                  boolean shuffle)
Removes the entry from the entry queue.

Parameters:
shuffle - indicates whether we are just shuffling the queue (in which case, the entry table is not modified).

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Sets the value in the cache at the given key. Returns the value.

Parameters:
key - Key of object to add.
value - Value of object to add.
Returns:
added value.

removeKey

public java.lang.Object removeKey(java.lang.Object key)
Removes and returns the value in the cache for the given key. If the key is not in the cache, returns null.

Parameters:
key - Key of object to remove from cache.
Returns:
Value removed from cache.

setSpaceLimit

public void setSpaceLimit(int limit)
Sets the maximum amount of space that the cache can store

Parameters:
limit - Number of units of cache space

spaceFor

protected int spaceFor(java.lang.Object value)
Returns the space taken by the given value.


toString

public java.lang.String toString()
Returns a String that represents the value of this object. This method is for debugging purposes only.

Overrides:
toString in class java.lang.Object

toStringContents

protected java.lang.String toStringContents()
Returns a String that represents the contents of this object. This method is for debugging purposes only.


toStringFillingRation

public java.lang.String toStringFillingRation(java.lang.String cacheName)

updateTimestamp

protected void updateTimestamp(LRUCache.LRUCacheEntry entry)
Updates the timestamp for the given entry, ensuring that the queue is kept in correct order. The entry must exist