Class sun.server.misc.Cache
All Packages Class Hierarchy This Package Previous Next Index
Class sun.server.misc.Cache
java.lang.Object
|
+----java.util.Dictionary
|
+----sun.server.misc.Cache
- public class Cache
- extends Dictionary
The Cache class. Maps keys to values. Any object can be used as
a key and/or value. This is very similar to the Hashtable
class, except that after putting an object into the Cache,
it is not guaranteed that a subsequent get will return it.
The Cache will automatically remove entries if memory is
getting tight and if the entry is not referenced from outside
the Cache.
To sucessfully store and retrieve objects from a hash table the
object used as the key must implement the hashCode() and equals()
methods.
This example creates a Cache of numbers. It uses the names of
the numbers as keys:
Cache numbers = new Cache();
numbers.put("one", new Integer(1));
numbers.put("two", new Integer(1));
numbers.put("three", new Integer(1));
To retrieve a number use:
Integer n = (Integer)numbers.get("two");
if (n != null) {
System.out.println("two = " + n);
}
- See Also:
- hashCode, equals, Ref
-
Cache()
- Constructs a new, empty Cache.
-
Cache(int)
- Constructs a new, empty Cache with the specified initial
capacity.
-
Cache(int, float)
- Constructs a new, empty Cache with the specified initial
capacity and the specified load factor.
-
clear()
- Clears the cache so that it has no more entries in it.
-
elements()
- Returns an enumeration of the elements.
-
get(Object)
- Gets the object associated with the specified key in the Cache.
-
isEmpty()
- Returns true if the Cache contains no elements.
-
keys()
- Returns an enumeration of the Cache's keys.
-
put(Object, Object)
- Puts the specified element into the Cache, using the specified
key.
-
rehash()
- Rehashes the contents of the table into a bigger table.
-
remove(Object)
- Removes the element corresponding to the key.
-
size()
- Returns the number of elements contained within the Cache.
Cache
public Cache(int initialCapacity,
float loadFactor)
- Constructs a new, empty Cache with the specified initial
capacity and the specified load factor.
- Parameters:
- initialCapacity - the initial number of buckets
- loadFactor - a number between 0.0 and 1.0, it defines
the threshold for rehashing the Cache into
a bigger one.
- Throws: IllegalArgumentException
- If the initial capacity
is less than or equal to zero.
- Throws: IllegalArgumentException
- If the load factor is
less than or equal to zero.
Cache
public Cache(int initialCapacity)
- Constructs a new, empty Cache with the specified initial
capacity.
- Parameters:
- initialCapacity - the initial number of buckets
Cache
public Cache()
- Constructs a new, empty Cache. A default capacity and load factor
is used. Note that the Cache will automatically grow when it gets
full.
size
public int size()
- Returns the number of elements contained within the Cache.
- Overrides:
- size in class Dictionary
isEmpty
public boolean isEmpty()
- Returns true if the Cache contains no elements.
- Overrides:
- isEmpty in class Dictionary
keys
public synchronized Enumeration keys()
- Returns an enumeration of the Cache's keys.
- Overrides:
- keys in class Dictionary
- See Also:
- elements, Enumeration
elements
public synchronized Enumeration elements()
- Returns an enumeration of the elements. Use the Enumeration methods
on the returned object to fetch the elements sequentially.
- Overrides:
- elements in class Dictionary
- See Also:
- keys, Enumeration
get
public synchronized Object get(Object key)
- Gets the object associated with the specified key in the Cache.
- Parameters:
- key - the key in the hash table
- Returns:
- s the element for the key or null if the key
is not defined in the hash table.
- Overrides:
- get in class Dictionary
- See Also:
- put
rehash
protected void rehash()
- Rehashes the contents of the table into a bigger table.
This is method is called automatically when the Cache's
size exceeds the threshold.
put
public synchronized Object put(Object key,
Object value)
- Puts the specified element into the Cache, using the specified
key. The element may be retrieved by doing a get() with the same
key. The key and the element cannot be null.
- Parameters:
- key - the specified hashtable key
- value - the specified element
- Returns:
- the old value of the key, or null if it did not have one.
- Throws: NullPointerException
- If the value of the specified
element is null.
- Overrides:
- put in class Dictionary
- See Also:
- get
remove
public synchronized Object remove(Object key)
- Removes the element corresponding to the key. Does nothing if the
key is not present.
- Parameters:
- key - the key that needs to be removed
- Returns:
- the value of key, or null if the key was not found.
- Overrides:
- remove in class Dictionary
clear
public synchronized void clear()
- Clears the cache so that it has no more entries in it.
All Packages Class Hierarchy This Package Previous Next Index