FileDocCategorySizeDatePackage
Cache.javaAPI DocExample6662Sun Jun 23 17:40:22 BST 2002org.dasein.util

Cache

public class Cache extends Object implements Serializable, Collection

Provides a cache of objects that will expire its contents as those contents fail to be used. This cache uses java.lang.ref.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.

modified
$Date$
version
$Revision$
author
George Reese/george.reese@jwt.com

Fields Summary
private HashMap
cache
A hash map indexing references by unique keys.
Constructors Summary
public Cache()


    /*
     * Constructs a new empty cache.
     */
      
        super();
    
Methods Summary
public booleanadd(java.lang.Object ob)
Unsupported.

param
ob ignored
return
never returns
throws
java.lang.UnsupportedOperationException always

        throw new UnsupportedOperationException();
    
public booleanaddAll(java.util.Collection coll)
Unsupported.

param
coll ignored
return
never returns
throws
java.lang.UnsupportedOperationException always

        throw new UnsupportedOperationException();
    
public voidcache(java.lang.Object key, java.lang.Object val)
Caches the specified object identified by the specified key.

param
key a unique key for this object
param
val the object to be cached

        cache.put(key, new SoftReference(val));
    
public voidclear()
Clears the entire cache.

        cache.clear();
    
public booleancontains(java.lang.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.

param
ob the object to check for in the cache
return
true if the object is in the cache

        Iterator it = cache.values().iterator();

        while( it.hasNext() ) {
            SoftReference ref = (SoftReference)it.next();
            Object item = ref.get();

            if( item != null && ob.equals(item) ) {
                return true;
            }
        }
        return false;
    
public booleancontainsAll(java.util.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.

param
coll the collection to test
return
true if all elements of the tested collection are in the cache

        Iterator it = coll.iterator();

        while( it.hasNext() ) {
            if( !contains(it.next()) ) {
                return false;
            }
        }
        return true;
    
public booleancontainsKey(java.lang.Object key)
Checks if an object with the specified key is in the cache.

param
key the object's identifier
return
true if the object is in the cache

        if( !cache.containsKey(key) ) {
            return false;
        }
        else {
            SoftReference ref = (SoftReference)cache.get(key);

            if( ref.get() == null ) {
                release(key);
                return false;
            }
            return true;
        }
    
public java.lang.Objectget(java.lang.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.

param
key the unique identifier of the desired object
return
the cached object or null

        SoftReference ref = (SoftReference)cache.get(key);
        Object ob;

        if( ref == null ) {
            return null;
        }
        ob = ref.get();
        if( ob == null ) {
            release(key);
        }
        return ob;
    
public booleanisEmpty()

return
true if the cache is empty

        return cache.isEmpty();
    
public java.util.Iteratoriterator()
Provides all of the valid objects in the cache. This method will not be the snappiest method in the world.

return
all valid objects in the cache

        return toList().iterator();
    
public voidrelease(java.lang.Object key)
Releases the specified object from the cache.

param
key the unique identified for the item to release

        cache.remove(key);
    
public booleanremove(java.lang.Object ob)
Unsupported.

param
ob ignored
return
never returns
throws
java.lang.UnsupportedOperationException always

        throw new UnsupportedOperationException();
    
public booleanremoveAll(java.util.Collection coll)
Unsupported.

param
coll ignored
return
never returns
throws
java.lang.UnsupportedOperationException always

        throw new UnsupportedOperationException();
    
public booleanretainAll(java.util.Collection coll)
Unsupported.

param
coll ignored
return
never returns
throws
java.lang.UnsupportedOperationException always

        throw new UnsupportedOperationException();
    
public intsize()

return
the number of elements in the cache

        return toList().size();
    
public java.lang.Object[]toArray()

return
the cache as an array

        return toList().toArray();
    
public java.lang.Object[]toArray(java.lang.Object[] arr)

return
the cache as an array

        return toList().toArray(arr);
    
private java.util.ArrayListtoList()

return
the cache as an array

        Iterator it = cache.values().iterator();
        ArrayList tmp = new ArrayList();

        while( it.hasNext() ) {
            SoftReference ref = (SoftReference)it.next();
            Object ob = ref.get();
            
            if( ob != null ) {
                tmp.add(ob);
            }
        }
        return tmp;