jrun.servlet.session
Class LRUTable

java.lang.Object
  |
  +--jrun.servlet.session.LRUTable

public class LRUTable
extends java.lang.Object

LRUTable

A table that maintains the Least Recently Used items. Consumers can add an object to be maintained (such as a unique key) and get a LRU key back. Whenever the owning object is used the table can be 'touched' using the LRU key. The least recently used object can always be returned by calling getLRU().

There is no limit to the number of items maintained in the table; the table will grow as needed.

This table is considered thread-safe

Author:
Karl Moss

Constructor Summary
LRUTable()
          Construct a new LRUTable using the default initial size of the table (1000)
LRUTable(int initialSize)
          Construct a new LRUTable using the given initial size
 
Method Summary
 int add(java.lang.Object key)
          Add a new object to the LRU cache.
protected  int getElementIndex(int id)
          Given the list id get the element in the vector where this id can be found
 java.lang.Object getKey(int id)
          Returns the object for the given LRU object id
 int getLRU()
          Returns the object id of the least recently used object
protected  int getRelativeIndex(int id)
          Given the list id get the index into the vector object where the id is located
protected  void makeRoomForAdd()
          Make sure we have a cache available for adding a new object
 void remove(int id)
          Removes the given id from the table
 void removeAll()
          Removes all elements from the table
protected  void setKey(int id, java.lang.Object key)
          Sets the key
protected  void setLinks(int id, int prev, int next)
          Set the previous and next pointers for the given list id
 int size()
          Returns the number of items in the table
 java.lang.String toString()
           
 void touch(int id)
          Touch the given id.
protected  void unplug(int elementIndex, int relativeIndex, int[] l)
          Unplug the element from the linked list
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LRUTable

public LRUTable()
Construct a new LRUTable using the default initial size of the table (1000)

LRUTable

public LRUTable(int initialSize)
Construct a new LRUTable using the given initial size
Parameters:
initialSize - The initial size of the table
Method Detail

add

public int add(java.lang.Object key)
Add a new object to the LRU cache.
Parameters:
key - The object or object key
Returns:
The LRU object id

touch

public void touch(int id)
Touch the given id. This move the id to the end of the list, making it the most recently used
Parameters:
id - The LRU object id

size

public int size()
Returns the number of items in the table
Returns:
The size

getLRU

public int getLRU()
Returns the object id of the least recently used object
Returns:
The LRU object id, or -1 if the list is empty

getKey

public java.lang.Object getKey(int id)
Returns the object for the given LRU object id
Parameters:
id - The LRU object id
Returns:
The object, or null if an invalid object id

removeAll

public void removeAll()
Removes all elements from the table

remove

public void remove(int id)
Removes the given id from the table
Parameters:
id - The LRU object id

makeRoomForAdd

protected void makeRoomForAdd()
Make sure we have a cache available for adding a new object

setLinks

protected void setLinks(int id,
                        int prev,
                        int next)
Set the previous and next pointers for the given list id

setKey

protected void setKey(int id,
                      java.lang.Object key)
Sets the key

getElementIndex

protected int getElementIndex(int id)
Given the list id get the element in the vector where this id can be found

getRelativeIndex

protected int getRelativeIndex(int id)
Given the list id get the index into the vector object where the id is located

unplug

protected void unplug(int elementIndex,
                      int relativeIndex,
                      int[] l)
Unplug the element from the linked list

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright � 2002 Macromedia Corporation. All Rights Reserved.