org.dasein.util
Class Cache<T>

java.lang.Object
  extended by org.dasein.util.Cache<T>
Type Parameters:
T - the type of object being stored in the cache
All Implemented Interfaces:
Serializable, Iterable<T>, Collection<T>

public class Cache<T>
extends Object
implements Collection<T>, Serializable

Provides a cache of objects that will expire its contents as those contents fail to be used. This cache uses SoftReference to guarantee that cached items will be removed when they have not been referenced for a long time.

This class is not synchronized and therefore should be synchronized by the application for multi-threaded use.

Last modified $Date: 2005/08/15 16:19:34 $

Version:
$Revision: 1.5 $
Author:
George Reese
See Also:
Serialized Form

Constructor Summary
Cache()
           
 
Method Summary
 boolean add(T ob)
          Unsupported.
 boolean addAll(Collection<? extends T> coll)
          Unsupported.
 void cache(Object key, T val)
          Caches the specified object identified by the specified key.
 void clear()
          Clears the entire cache.
 boolean contains(Object ob)
          Checks the specified object against the cache and verifies that it is in the cache.
 boolean containsAll(Collection<?> coll)
          Checks the passed in collection and determines if all elements of that collection are contained within this cache.
 boolean containsKey(Object key)
          Checks if an object with the specified key is in the cache.
 T get(Object key)
          Provides the cached object identified by the specified key.
 boolean isEmpty()
           
 Iterator<T> iterator()
          Provides all of the valid objects in the cache.
 void release(Object key)
          Releases the specified object from the cache.
 boolean remove(Object ob)
          Unsupported.
 boolean removeAll(Collection<?> coll)
          Unsupported.
 boolean retainAll(Collection<?> coll)
          Unsupported.
 int size()
           
 Object[] toArray()
           
<E> E[]
toArray(E[] arr)
           
 String toString()
          Displays the current contents of the cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Collection
equals, hashCode
 

Constructor Detail

Cache

public Cache()
Method Detail

add

public boolean add(T ob)
Unsupported.

Specified by:
add in interface Collection<T>
Parameters:
ob - ignored
Returns:
never returns
Throws:
UnsupportedOperationException - always

addAll

public boolean addAll(Collection<? extends T> coll)
Unsupported.

Specified by:
addAll in interface Collection<T>
Parameters:
coll - ignored
Returns:
never returns
Throws:
UnsupportedOperationException - always\

cache

public void cache(Object key,
                  T val)
Caches the specified object identified by the specified key.

Parameters:
key - a unique key for this object
val - the object to be cached

clear

public void clear()
Clears the entire cache.

Specified by:
clear in interface Collection<T>

contains

public boolean contains(Object ob)
Checks the specified object against the cache and verifies that it is in the cache. This method will return false if the object was once in the cache but has expired due to inactivity.

Specified by:
contains in interface Collection<T>
Parameters:
ob - the object to check for in the cache
Returns:
true if the object is in the cache

containsAll

public boolean containsAll(Collection<?> coll)
Checks the passed in collection and determines if all elements of that collection are contained within this cache. Care should be taken in reading too much into a failure. If one of the elements was once in this cache but has expired due to inactivity, this method will return false.

Specified by:
containsAll in interface Collection<T>
Parameters:
coll - the collection to test
Returns:
true if all elements of the tested collection are in the cache

containsKey

public boolean containsKey(Object key)
Checks if an object with the specified key is in the cache.

Parameters:
key - the object's identifier
Returns:
true if the object is in the cache

get

public T get(Object key)
Provides the cached object identified by the specified key. This method will return null if the specified object is not in the cache.

Parameters:
key - the unique identifier of the desired object
Returns:
the cached object or null

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Collection<T>
Returns:
true if the cache is empty

iterator

public Iterator<T> iterator()
Provides all of the valid objects in the cache. This method will not be the snappiest method in the world.

Specified by:
iterator in interface Iterable<T>
Specified by:
iterator in interface Collection<T>
Returns:
all valid objects in the cache

release

public void release(Object key)
Releases the specified object from the cache.

Parameters:
key - the unique identified for the item to release

remove

public boolean remove(Object ob)
Unsupported.

Specified by:
remove in interface Collection<T>
Parameters:
ob - ignored
Returns:
never returns
Throws:
UnsupportedOperationException - always

removeAll

public boolean removeAll(Collection<?> coll)
Unsupported.

Specified by:
removeAll in interface Collection<T>
Parameters:
coll - ignored
Returns:
never returns
Throws:
UnsupportedOperationException - always

retainAll

public boolean retainAll(Collection<?> coll)
Unsupported.

Specified by:
retainAll in interface Collection<T>
Parameters:
coll - ignored
Returns:
never returns
Throws:
UnsupportedOperationException - always

size

public int size()
Specified by:
size in interface Collection<T>
Returns:
the number of elements in the cache

toArray

public Object[] toArray()
Specified by:
toArray in interface Collection<T>
Returns:
the cache as an array

toArray

public <E> E[] toArray(E[] arr)
Specified by:
toArray in interface Collection<T>
Returns:
the cache as an array

toString

public String toString()
Displays the current contents of the cache.

Overrides:
toString in class Object
Returns:
a string representation of the current cache contents


Copyright © 2011 enStratus Networks LLC. All Rights Reserved.