edu.toronto.cs.util.caching
Class NaiveMapCache

java.lang.Object
  extended by edu.toronto.cs.util.caching.AbstractMapCache
      extended by edu.toronto.cs.util.caching.NaiveMapCache
All Implemented Interfaces:
Cache

public class NaiveMapCache
extends AbstractMapCache

Fairly simplistic caching.


Field Summary
 int DEFAULT_SIZE
           
protected  java.util.List keylist
           
protected  int maxsize
           
 
Fields inherited from class edu.toronto.cs.util.caching.AbstractMapCache
cache
 
Constructor Summary
NaiveMapCache()
          Create a new cache.
NaiveMapCache(int numitems)
          Create a new cache with a specified # of items it can store.
 
Method Summary
protected  void adjustSize()
          Adjust the cache size to the specified value.
 java.lang.Object get(java.lang.Object key)
          Retrieves a value with the specified key from the cache.
 boolean isFull()
          Determines if the cache is at its maximum capacity.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Stores a value and assotiates a key with it.
 java.lang.Object remove(java.lang.Object key)
          Removes a value with the specified key from the cache.
 void resize(int size)
          Sets the maximum cache size.
 
Methods inherited from class edu.toronto.cs.util.caching.AbstractMapCache
clear, containsKey, containsValue, entrySet, isEmpty, keySet, size, values
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

keylist

protected java.util.List keylist

maxsize

protected int maxsize

DEFAULT_SIZE

public final int DEFAULT_SIZE
See Also:
Constant Field Values
Constructor Detail

NaiveMapCache

public NaiveMapCache()
Create a new cache.


NaiveMapCache

public NaiveMapCache(int numitems)
Create a new cache with a specified # of items it can store.

Method Detail

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Stores a value and assotiates a key with it. If the key already has a value associated with it, the old value is replaced by the supplied new value and returned.

Specified by:
put in interface Cache
Specified by:
put in class AbstractMapCache
Parameters:
key - -- the key associated with the value.
value - -- value to store.
Returns:
-- the value that has been previously associated with the key or null if none exists.

get

public java.lang.Object get(java.lang.Object key)
Retrieves a value with the specified key from the cache.

Specified by:
get in interface Cache
Specified by:
get in class AbstractMapCache
Parameters:
key - -- the key associated with the value.
Returns:
-- the value associated with the key or null if not found.

remove

public java.lang.Object remove(java.lang.Object key)
Removes a value with the specified key from the cache. If there's no mapping for the specified key, does nothing.

Specified by:
remove in interface Cache
Specified by:
remove in class AbstractMapCache
Parameters:
key - -- the key associated with the value.
Returns:
-- the value being removed or null if no such key exists.

isFull

public boolean isFull()
Determines if the cache is at its maximum capacity.

Specified by:
isFull in class AbstractMapCache
Returns:
-- true if maximum capacity has been reached and false otherwise.

resize

public void resize(int size)
Description copied from class: AbstractMapCache
Sets the maximum cache size. If the cache is currently bigger than specified size, the excess elements get purged.

Specified by:
resize in interface Cache
Specified by:
resize in class AbstractMapCache
Parameters:
size - -- the maximum cache size.

adjustSize

protected void adjustSize()
Adjust the cache size to the specified value.