edu.toronto.cs.util.caching
Class AbstractMapCache

java.lang.Object
  extended by edu.toronto.cs.util.caching.AbstractMapCache
All Implemented Interfaces:
Cache
Direct Known Subclasses:
NaiveMapCache, UnboundedMapCache

public abstract class AbstractMapCache
extends java.lang.Object
implements Cache

Abstract cache class.


Field Summary
protected  java.util.Map cache
           
 
Constructor Summary
AbstractMapCache(java.util.Map map)
          Initializes the AbstractMapCache with a particular Map.
 
Method Summary
 void clear()
          Clears the cache.
 boolean containsKey(java.lang.Object key)
          Checks whether the cache contains a value associated with the key.
 boolean containsValue(java.lang.Object value)
          Checks whether the cache contains the specified value.
 java.util.Set entrySet()
          Returns a Set representation of the mapping.
abstract  java.lang.Object get(java.lang.Object key)
          Retrieves a value with the specified key from the cache.
 boolean isEmpty()
          Determines if the cache is empty.
abstract  boolean isFull()
          Determines if the cache is at its maximum capacity.
 java.util.Set keySet()
          Gets the set of all the keys registered in the cahce.
abstract  java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Stores a value and assotiates a key with it.
abstract  java.lang.Object remove(java.lang.Object key)
          Removes a value with the specified key from the cache.
abstract  void resize(int size)
          Sets the maximum cache size.
 int size()
          Returns the number of elements in cache.
 java.util.Collection values()
          Gets a collection view of the values contained in this map.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

cache

protected java.util.Map cache
Constructor Detail

AbstractMapCache

public AbstractMapCache(java.util.Map map)
Initializes the AbstractMapCache with a particular Map.

Method Detail

clear

public void clear()
Clears the cache.

Specified by:
clear in interface Cache

containsKey

public boolean containsKey(java.lang.Object key)
Checks whether the cache contains a value associated with the key.

Specified by:
containsKey in interface Cache
Parameters:
key - -- the key to look for.
Returns:
-- true if the key is in the cache; false otherwise.

containsValue

public boolean containsValue(java.lang.Object value)
Checks whether the cache contains the specified value.

Specified by:
containsValue in interface Cache
Parameters:
value - -- the value to look for.
Returns:
-- true if the value is in the cache; false otherwise.

put

public abstract 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
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 abstract java.lang.Object get(java.lang.Object key)
Retrieves a value with the specified key from the cache.

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

remove

public abstract 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
Parameters:
key - -- the key associated with the value.
Returns:
-- the value being removed or null if no such key exists.

size

public int size()
Returns the number of elements in cache.

Specified by:
size in interface Cache
Returns:
-- the number of elements in cashe.

isEmpty

public boolean isEmpty()
Determines if the cache is empty.

Specified by:
isEmpty in interface Cache
Returns:
-- true if th cache is empty and false otherwise.

isFull

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

Returns:
-- true if maximum capacity has been reached and false otherwise.

values

public java.util.Collection values()
Gets a collection view of the values contained in this map.

Specified by:
values in interface Cache
Returns:
-- collection view of the values contained in this map.

keySet

public java.util.Set keySet()
Gets the set of all the keys registered in the cahce.

Specified by:
keySet in interface Cache
Returns:
-- the Set of all keys in cache.

entrySet

public java.util.Set entrySet()
Returns a Set representation of the mapping.

Specified by:
entrySet in interface Cache
Returns:
-- a Set representing the cache.

resize

public abstract void resize(int size)
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
Parameters:
size - -- the maximum cache size.