org.sadun.util
Class Cache

java.lang.Object
  extended byorg.sadun.util.Cache

public class Cache
extends java.lang.Object

A simple cache object, which holds at most n references.

Author:
Cristiano Sadun

Constructor Summary
Cache(int max)
          Constructor for Cache.
 
Method Summary
 boolean containsKey(java.lang.Object key)
          Return true if the cache contains the given key.
 java.lang.Object get(java.lang.Object key)
          Find an object into the cache.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Put an object in the cache.
 java.lang.Object remove(java.lang.Object key)
          Remove an object from the cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Cache

public Cache(int max)
Constructor for Cache.

Parameters:
max - the maximum number of references to hold.
Method Detail

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Put an object in the cache. If the cache size exceed the maximum size, the least accessed object will be removed.

Returns:
the object just inserted.

remove

public java.lang.Object remove(java.lang.Object key)
Remove an object from the cache.

Returns:
null or the object corresponding to the key.

get

public java.lang.Object get(java.lang.Object key)
Find an object into the cache.

Parameters:
key - the key of the object to get.
Returns:
null or the object corresponding to the key.

containsKey

public boolean containsKey(java.lang.Object key)
Return true if the cache contains the given key.

Note that this not guarantees that the corresponding object will actually exist in the cache in a later call to get(Object).

Parameters:
key - the value to verify
Returns:
true if the key exist in the cache