com.ibm.wbi.util
Class LRUCache

java.lang.Object
  |
  +--java.util.Dictionary
        |
        +--com.ibm.wbi.util.Cache
              |
              +--com.ibm.wbi.util.LRUCache

public class LRUCache
extends Cache

A simple LRU Cache implementation. Basically this is a Hashtable where entries expire on a LRU basis.

See Also:
Cache, Hashtable, Dictionary

Constructor Summary
LRUCache()
          Constructs an LRUCache with a default capacity of 10.
LRUCache(int maxsize)
          Constructs an LRUCache with the specified capacity.
 
Method Summary
 void clear()
          Clears the cache so that it contains no keys or values.
 java.util.Enumeration elements()
          Returns an enumeration of the values in this LRUCache.
 java.lang.Object get(java.lang.Object key)
          Returns the value to which the key is mapped in the cache.
 boolean isEmpty()
          Tests whether this LRUCache contains no keys.
 java.util.Enumeration keys()
          Returns an enumeration of the keys in this LRUCache.
static void main(java.lang.String[] argv)
          Test driver.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Maps the specified key to the specified value in this cache.
 java.lang.Object remove(java.lang.Object key)
          Removes the specified key and the value to which it is mapped from the cache.
 int size()
          Returns the number of keys which are mapped to values in this cache.
 java.lang.String toString()
          Returns a string representation of this cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LRUCache

public LRUCache()
Constructs an LRUCache with a default capacity of 10.

LRUCache

public LRUCache(int maxsize)
Constructs an LRUCache with the specified capacity.
Parameters:
maxsize - The capacity of the cache.
Method Detail

elements

public java.util.Enumeration elements()
Returns an enumeration of the values in this LRUCache.
Overrides:
elements in class Cache
Returns:
an enumeration of the values in this LRUCache.

get

public java.lang.Object get(java.lang.Object key)
Returns the value to which the key is mapped in the cache.
Overrides:
get in class Cache
Parameters:
key - A key in the cache.
Returns:
The value to which the key is mapped, or null if the key is not mapped to any value.

isEmpty

public boolean isEmpty()
Tests whether this LRUCache contains no keys.
Overrides:
isEmpty in class Cache
Returns:
true if the cache contains no keys; false otherwise.

keys

public java.util.Enumeration keys()
Returns an enumeration of the keys in this LRUCache.
Overrides:
keys in class Cache
Returns:
an enumeration of the keys in this LRUCache.

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Maps the specified key to the specified value in this cache. Neither the key nor the value may be null.
Overrides:
put in class Cache
Parameters:
key - The key
value - The value
Returns:
The previous value to which key was mapped, or null if key was not mapped to any value.
Throws:
NullPointerException - If key or value is null.

remove

public java.lang.Object remove(java.lang.Object key)
Removes the specified key and the value to which it is mapped from the cache. This method does nothing if the key is not mapped to any value.
Overrides:
remove in class Cache
Parameters:
key - The key to be removed
Returns:
The value to which key was mapped, or null if key was not mapped to any value.

size

public int size()
Returns the number of keys which are mapped to values in this cache.
Overrides:
size in class Cache
Returns:
the number of keys which are mapped to values in this cache.

clear

public void clear()
Clears the cache so that it contains no keys or values.
Overrides:
clear in class Cache

toString

public java.lang.String toString()
Returns a string representation of this cache.
Overrides:
toString in class java.lang.Object
Returns:
a string representation of this cache.

main

public static void main(java.lang.String[] argv)
Test driver.