Class WeakCache<T>

java.lang.Object
org.simpleframework.xml.util.WeakCache<T>
All Implemented Interfaces:
Cache<T>
Direct Known Subclasses:
ReadState, WriteState

public class WeakCache<T> extends Object implements Cache<T>
The WeakCache 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.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private class 
    The segment is effectively a synchronized weak hash map.
    private class 
    This is used to maintain a list of segments.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    This is used to store a list of segments for the cache.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor for the WeakCache object.
    WeakCache(int size)
    Constructor for the WeakCache object.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    cache(Object key, T value)
    This method is used to insert a key value mapping in to the cache.
    boolean
    This is used to determine whether the specified key exists with in the cache.
    This method is used to get the value from the cache that is mapped to the specified key.
    boolean
    This method is used to determine if the cache is empty.
    map(Object key)
    This method is used to acquire a Segment using the keys has code.
    take(Object key)
    This is used to exclusively take the value mapped to the specified key from the cache.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • WeakCache

      public WeakCache()
      Constructor for the WeakCache 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 the WeakCache 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 Details

    • 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.
      Specified by:
      isEmpty in interface Cache<T>
      Returns:
      this returns true if the cache is empty
    • cache

      public void cache(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.
      Specified by:
      cache in interface Cache<T>
      Parameters:
      key - this is the key to cache the provided value to
      value - this is the value that is to be cached
    • take

      public T take(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.
      Specified by:
      take in interface Cache<T>
      Parameters:
      key - this is the key to acquire the cache value with
      Returns:
      this returns the value mapped to the specified key
    • fetch

      public T fetch(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.
      Specified by:
      fetch in interface Cache<T>
      Parameters:
      key - this is the key to acquire the cache value with
      Returns:
      this returns the value mapped to the specified key
    • contains

      public boolean contains(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.
      Specified by:
      contains in interface Cache<T>
      Parameters:
      key - this is the key to check within this segment
      Returns:
      true if the specified key is within the cache
    • map

      private WeakCache<T>.Segment map(Object key)
      This method is used to acquire a Segment using the keys has code. This method effectively uses the hash to find a specific segment within the fixed list of segments.
      Parameters:
      key - this is the key used to acquire the segment
      Returns:
      this returns the segment used to get acquire value