org.enhydra.xml.xmlc.deferredparsing
Class Cache

java.lang.Object
  |
  +--org.enhydra.xml.xmlc.deferredparsing.Cache
All Implemented Interfaces:
Map

public class Cache
extends Object
implements Map

A Cache for arbitrary objects. It holds its objects with soft references. Because broken handling of soft references in current JVM implementations, we also hold additional hard references to the most recently used entries.

Version:
$Id: Cache.java,v 1.3 2005/01/26 08:29:24 jkjome Exp $
Author:
Ole Arndt

Inner Class Summary
protected static class Cache.MRUCache
          A simple class for holding references to the most recently used entries.
 
Inner classes inherited from class java.util.Map
Map.Entry
 
Field Summary
static int DEFAULT_NUM_MRU_ENTRIES
          the default size for the MRU cache
 
Constructor Summary
Cache()
          Creates a new Cache instance with the default value for most recently used entries.
Cache(int numMRUEntries)
          Creates a new Cache instance with an explicit value for most recently used entries cache.
Cache(int numMRUEntries, Map other)
          Creates a new Cache instance.
Cache(Map other)
          Creates a new Cache instance.
 
Method Summary
 void clear()
          Describe clear method here.
 boolean containsKey(Object key)
          Describe containsKey method here.
 boolean containsValue(Object obj)
          Describe containsValue method here.
 Set entrySet()
          Returns a set view of the mappings contained in this cache.
 boolean equals(Object cache)
          Compare two Cache instances.
 Object get(Object key)
          Returns the value to which this cache maps the specified key.
 int hashCode()
          Get hash code of map.
 boolean isEmpty()
          Map interface, isEmpty method.
 Set keySet()
          Return a Set of all keys.
 Object put(Object key, Object value)
          Associates the specified value with the specified key in this cache.
 void putAll(Map cache)
          Describe putAll method here.
 Object remove(Object key)
          Removes the mapping for this key from this cache.
 int size()
          Get the current size of the cache.
 Collection values()
          Return a collection of all values this cache contains.
 
Methods inherited from class java.lang.Object
, clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_NUM_MRU_ENTRIES

public static final int DEFAULT_NUM_MRU_ENTRIES
the default size for the MRU cache
Constructor Detail

Cache

public Cache()
Creates a new Cache instance with the default value for most recently used entries.

Cache

public Cache(int numMRUEntries)
Creates a new Cache instance with an explicit value for most recently used entries cache.
Parameters:
numMRUEntries - an int value

Cache

public Cache(Map other)
Creates a new Cache instance. The give Map will be copied.
Parameters:
other - a Map value

Cache

public Cache(int numMRUEntries,
             Map other)
Creates a new Cache instance. The give Map will be copied.
Parameters:
other - a Map value
Method Detail

hashCode

public int hashCode()
Get hash code of map.
Specified by:
hashCode in interface Map
Overrides:
hashCode in class Object
Returns:
an int value

equals

public boolean equals(Object cache)
Compare two Cache instances.
Specified by:
equals in interface Map
Overrides:
equals in class Object
Parameters:
cache - other cache to compare to.
Returns:
true if caches are equal, false otherwise.

size

public int size()
Get the current size of the cache.
Specified by:
size in interface Map
Returns:
the size of the cache.

clear

public void clear()
Describe clear method here.
Specified by:
clear in interface Map

put

public Object put(Object key,
                  Object value)
Associates the specified value with the specified key in this cache. If the cache previously contained a mapping for this key, the old value is replaced. Note that this cache can not map null values.
Specified by:
put in interface Map
Parameters:
key - key with which the specified value is to be associated.
value - value to be associated with the specified key.
Returns:
previous value associated with specified key, or null if there was no mapping for key.
Throws:
NullPointerException - if value is null.

get

public Object get(Object key)
Returns the value to which this cache maps the specified key. If the value is not found, returns null
Specified by:
get in interface Map
Parameters:
key - key whose associated value is to be returned.
Returns:
the value to which this cache maps the specified key, or null if the cache contains no mapping for this key.

values

public Collection values()
Return a collection of all values this cache contains. It is not guaranteed, that an iterator from the collection will return only non null values. It is likely some soft (weak) references will go away while traversing the Collection. The iterator will return null in this case.
Specified by:
values in interface Map
Returns:
a Collection containing a values of the cache.

remove

public Object remove(Object key)
Removes the mapping for this key from this cache.
Specified by:
remove in interface Map
Parameters:
key - key whose mapping is to be removed from the cache.
Returns:
previous value associated with specified key, or null if there was no mapping for key.

keySet

public Set keySet()
Return a Set of all keys.
Specified by:
keySet in interface Map
Returns:
a Set of all keys.

entrySet

public Set entrySet()
Returns a set view of the mappings contained in this cache. Each element in the returned set is a Map.Entry. The set is backed by the cache, so changes to the cache are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.
Specified by:
entrySet in interface Map
Returns:
a Set view of all values.

isEmpty

public boolean isEmpty()
Map interface, isEmpty method.
Specified by:
isEmpty in interface Map
Returns:
true if map is empty, false otherwise
See Also:
Map.isEmpty()

containsValue

public boolean containsValue(Object obj)
Describe containsValue method here.
Specified by:
containsValue in interface Map
Parameters:
obj - an Object value
Returns:
a boolean value

containsKey

public boolean containsKey(Object key)
Describe containsKey method here.
Specified by:
containsKey in interface Map
Parameters:
key - an Object value
Returns:
a boolean value

putAll

public void putAll(Map cache)
Describe putAll method here.
Specified by:
putAll in interface Map
Parameters:
cache - a Map value


Copyright © 1999-2002 enhydra.org (Mark Diekhans, David Li, Richard Kunze). All Rights reserved.