edu.brandeis.cs.steele.util
Class LRUCache

java.lang.Object
  extended by edu.brandeis.cs.steele.util.LRUCache
All Implemented Interfaces:
Cache

public class LRUCache
extends java.lang.Object
implements Cache

A fixed-capacity Cache that stores the n most recently used keys.

Author:
Oliver Steele, steele@cs.brandeis.edu

Field Summary
protected  int capacity
           
protected  java.util.Vector<java.lang.Object> keys
           
protected  java.util.Hashtable<java.lang.Object,java.lang.Object> map
           
 
Constructor Summary
LRUCache(int capacity)
           
 
Method Summary
 void clear()
          Remove all values stored in this cache.
 java.lang.Object get(java.lang.Object key)
          If key was used in a previous call to put, this call may return the value of that call.
 void put(java.lang.Object key, java.lang.Object value)
          Store value in the cache, indexed by key.
 void remove(java.lang.Object key)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

capacity

protected int capacity

keys

protected java.util.Vector<java.lang.Object> keys

map

protected java.util.Hashtable<java.lang.Object,java.lang.Object> map
Constructor Detail

LRUCache

public LRUCache(int capacity)
Method Detail

put

public void put(java.lang.Object key,
                java.lang.Object value)
Description copied from interface: Cache
Store value in the cache, indexed by key. This operation makes it likely, although not certain, that a subsquent call to get with the same (equal) key will retrieve the same (==) value.

Multiple calls to put with the same key and value are idempotent. A set of calls to put with the same key but different values has only the affect of the last call (assuming there were no intervening calls to get).

Specified by:
put in interface Cache

get

public java.lang.Object get(java.lang.Object key)
Description copied from interface: Cache
If key was used in a previous call to put, this call may return the value of that call. Otherwise it returns null.

Specified by:
get in interface Cache

remove

public void remove(java.lang.Object key)

clear

public void clear()
Description copied from interface: Cache
Remove all values stored in this cache. Subsequent calls to get will return null.

Specified by:
clear in interface Cache