Package org.simpleframework.xml.util
Class WeakCache<T>
- java.lang.Object
-
- org.simpleframework.xml.util.WeakCache<T>
-
- All Implemented Interfaces:
Cache<T>
public class WeakCache<T> extends java.lang.Object implements Cache<T>
TheWeakCache
object is an implementation of a cache that holds on to cached items only if the key remains in memory. This is effectively like a concurrent hash map with weak keys, it ensures that multiple threads can concurrently access weak hash maps in a way that lowers contention for the locks used.- Author:
- Niall Gallagher
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
cache(java.lang.Object key, T value)
This method is used to insert a key value mapping in to the cache.boolean
contains(java.lang.Object key)
This is used to determine whether the specified key exists with in the cache.T
fetch(java.lang.Object key)
This method is used to get the value from the cache that is mapped to the specified key.boolean
isEmpty()
This method is used to determine if the cache is empty.T
take(java.lang.Object key)
This is used to exclusively take the value mapped to the specified key from the cache.
-
-
-
Constructor Detail
-
WeakCache
public WeakCache()
Constructor for theWeakCache
object. This is used to create a cache that stores values in such a way that when the key is garbage collected the value is removed from the map. This is similar to the concurrent hash map.
-
WeakCache
public WeakCache(int size)
Constructor for theWeakCache
object. This is used to create a cache that stores values in such a way that when the key is garbage collected the value is removed from the map. This is similar to the concurrent hash map.- Parameters:
size
- this is the number of segments within the cache
-
-
Method Detail
-
isEmpty
public boolean isEmpty()
Description copied from interface:Cache
This method is used to determine if the cache is empty. This is done by checking if there are any elements in the cache. If anything has been cached this will return false.
-
cache
public void cache(java.lang.Object key, T value)
This method is used to insert a key value mapping in to the cache. The value can later be retrieved or removed from the cache if desired. If the value associated with the key is null then nothing is stored within the cache.
-
take
public T take(java.lang.Object key)
This is used to exclusively take the value mapped to the specified key from the cache. Invoking this is effectively removing the value from the cache.
-
fetch
public T fetch(java.lang.Object key)
This method is used to get the value from the cache that is mapped to the specified key. If there is no value mapped to the specified key then this method will return a null.
-
contains
public boolean contains(java.lang.Object key)
This is used to determine whether the specified key exists with in the cache. Typically this can be done using the fetch method, which will acquire the object.
-
-