public final class WeakCache extends AbstractBaseCache implements Serializable
This implementation of the Cache interface uses
a WeakHashMap to store the keys. The values are stored
as WeakReferences.
Note that this implementation is synchronized. Multiple threads can access this map concurrently.
WeakHashMap,
WeakReference,
Serialized Form| Constructor and Description |
|---|
WeakCache()
Creates a new WeakCache object.
|
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Removes all mappings from this map (optional operation).
|
boolean |
containsKey(Object key)
Checks if a given key exists in the cache.
|
protected Map |
createMap()
Creates the map to be used with the cache.
|
Object |
get(Object key)
Returns an object with a given key from the cache.
|
Set |
keySet()
Returns a set view of the keys contained in this cache.
|
void |
put(Object key,
Object value)
Puts an object with a key into the cache.
|
Object |
remove(Object key)
Removes the mapping for this key from this cache if present (optional
operation).
|
Collection |
values()
Returns a collection view of the values contained in this cache.
|
getMap, sizeprotected Map createMap()
Concrete classes must implement this method and create a concrete map for the cache.
createMap in class AbstractBaseCacheAbstractBaseCache.createMap()public void put(Object key, Object value)
put in interface Cachekey - the keyvalue - the objectCache.put(java.lang.Object, java.lang.Object),
WeakReference,
Cache.put(java.lang.Object, java.lang.Object)public Object get(Object key)
get in interface Cachekey - the keyCache.get(java.lang.Object)public boolean containsKey(Object key)
containsKey in interface Cachekey - the keytrue if the given key exists in the cache, else
falseCache.containsKey(java.lang.Object)public Object remove(Object key)
This implementation iterates over entrySet() searching for an entry with the specified key. If such an entry is found, its value is obtained with its getValue operation, the entry is removed from the Collection (and the backing cache) with the iterator's remove operation, and the saved value is returned. If the iteration terminates without finding such an entry, null is returned. Note that this implementation requires linear time in the size of the cache; many implementations will override this method.
remove in interface Cachekey - key whose mapping is to be removed from the cache.Cache.remove(java.lang.Object)public void clear()
clear in interface CacheCache.clear()public Set keySet()
keySet in interface CacheCache.keySet()public Collection values()
values in interface CacheCache.values()Copyright © 2005–2020. All rights reserved.