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.
 
Field Summary
protected static int DEFAULT_SPACELIMIT
          Default amount of space in the cache
protected  int fCurrentSpace
          Amount of cache space used so far
protected  LRUCache.LRUCacheEntry fEntryQueue
          Start of queue (most recently used entry)
protected  LRUCache.LRUCacheEntry fEntryQueueTail
          End of queue (least recently used entry)
protected  java.util.Hashtable fEntryTable
          Hash table for fast random access to cache entries
protected  int fSpaceLimit
          Maximum space allowed in cache
protected  int fTimestampCounter
          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.
 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

fCurrentSpace

protected int fCurrentSpace
Amount of cache space used so far


fSpaceLimit

protected int fSpaceLimit
Maximum space allowed in cache


fTimestampCounter

protected int fTimestampCounter
Counter for handing out sequential timestamps


fEntryTable

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


fEntryQueue

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


fEntryQueueTail

protected LRUCache.LRUCacheEntry fEntryQueueTail
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

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.


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