Class SoftReferenceCache

  • Direct Known Subclasses:
    NamedProfileCache, URLImageCache

    public class SoftReferenceCache
    extends java.lang.Object
    This class manages a cache of soft references to objects that may take some time to load or create, such as images loaded from the network.

    Adding an object is two fold:

    • First you add the key, this lets the cache know that someone is working on that key.
    • Then when the completed object is ready you put it into the cache.

    If someone requests a key after it has been added but before it has been put they will be blocked until the put.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.Map map
      The map of cached objects.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected SoftReferenceCache()
      Let people create their own caches.
      protected SoftReferenceCache​(boolean synchronous)
      Constructs a soft reference cache.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void clearImpl​(java.lang.Object key)
      Clear the entry for key.
      void flush()
      Let people flush the cache (remove any cached data).
      protected boolean isDoneImpl​(java.lang.Object key)
      Check if request(key) will return immediately with the Object.
      protected boolean isPresentImpl​(java.lang.Object key)
      Check if request(key) will return with an Object (not putting you on the hook for it).
      protected void putImpl​(java.lang.Object key, java.lang.Object object)
      Associate object with key.
      protected java.lang.Object requestImpl​(java.lang.Object key)
      If this returns null then you are now 'on the hook'.
      • Methods inherited from class java.lang.Object

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

      • map

        protected final java.util.Map map
        The map of cached objects. Must not change after creation, so it's final.
    • Constructor Detail

      • SoftReferenceCache

        protected SoftReferenceCache()
        Let people create their own caches.
      • SoftReferenceCache

        protected SoftReferenceCache​(boolean synchronous)
        Constructs a soft reference cache.
        Parameters:
        synchronous - true to enable synchronous mode, false to enable waiting on requestImpl() until another thread adds the missing object
    • Method Detail

      • flush

        public void flush()
        Let people flush the cache (remove any cached data). Pending requests will be treated as though clear() was called on the key, this should cause them to go and re-read the data.
      • isPresentImpl

        protected final boolean isPresentImpl​(java.lang.Object key)
        Check if request(key) will return with an Object (not putting you on the hook for it). Note that it is possible that this will return true but between this call and the call to request the soft-reference will be cleared. So it is still possible for request to return NULL, just much less likely (you can always call 'clear' in that case).
      • isDoneImpl

        protected final boolean isDoneImpl​(java.lang.Object key)
        Check if request(key) will return immediately with the Object. Note that it is possible that this will return true but between this call and the call to request the soft-reference will be cleared.
      • requestImpl

        protected final java.lang.Object requestImpl​(java.lang.Object key)
        If this returns null then you are now 'on the hook'. to put the Object associated with key into the cache.
      • clearImpl

        protected final void clearImpl​(java.lang.Object key)
        Clear the entry for key. This is the easiest way to 'get off the hook'. if you didn't intend to get on it.
      • putImpl

        protected final void putImpl​(java.lang.Object key,
                                     java.lang.Object object)
        Associate object with key. 'object' is only referenced through a soft reference so don't rely on the cache to keep it around. If the map no longer contains our url it was probably cleared or flushed since we were put on the hook for it, so in that case we will do nothing.