Class TLcdCache

java.lang.Object
com.luciad.util.TLcdCache
All Implemented Interfaces:
ILcdCache

public class TLcdCache extends Object implements ILcdCache
Implementation of ILcdCache. By default, it automatically removes cached key-value pairs that are no longer used. It does so by using WeakReferences to refer the keys. This means that if the key is no longer strongly reachable, and therefore set to null by the garbage collector, the value will be removed from the TLcdCache on the next access. This is particularly useful if some long term object such as an ILcdGXYPainter or an ILcdGXYLayer is used as a key: when the object is garbage collected, the cached values will be removed automatically.

The values are however not weakly referenced, so care must be taken that no (indirect) strong reference exists from the value to the key. If such a strong reference from the value to the key exists, it will prevent the key from being weakly reachable, and therefore the key-value pair may never be removed automatically.

The weak behavior can be turned off, by either setting setUseWeakKeyReferences(boolean) to false, or by passing false to TLcdCache(boolean).

Values are softly referenced by default, and hence may be garbage collected when memory needs to be reclaimed.

Please refer to java.util.ref for definitions of 'strongly reachable', 'weakly reachable', WeakReference etc.

  • Constructor Details

    • TLcdCache

      public TLcdCache()
      Creates a new TLcdCache instance. It refers the keys weakly if isUseWeakKeyReferences() returns true. It refers the values softly if isUseSoftValueReferences() returns true. Please refer to TLcdCache for more information on the handling of keys and values.
    • TLcdCache

      public TLcdCache(boolean aUseWeakKeyReferences)
      Creates a new TLcdCache instance. It refers the values softly if isUseSoftValueReferences() returns true.
      Parameters:
      aUseWeakKeyReferences - True if the keys should be referred weakly, false otherwise. Please refer to TLcdCache for more information on the handling of keys and values.
    • TLcdCache

      public TLcdCache(boolean aUseWeakKeyReferences, boolean aUseSoftValueReferences)
      Creates a new TLcdCache instance.
      Parameters:
      aUseWeakKeyReferences - True if the keys should be referred weakly, false otherwise.
      aUseSoftValueReferences - True if the values should be referred softly, false otherwise. Please refer to TLcdCache for more information on the handling of keys and values.
  • Method Details

    • isUseWeakKeyReferences

      public static boolean isUseWeakKeyReferences()
      Returns true if weak references for the keys are used. The default is true.
      Returns:
      true if weak references for the keys are used.
      See Also:
    • setUseWeakKeyReferences

      public static void setUseWeakKeyReferences(boolean aUseWeakKeyReferences)
      Sets if weak references for the keys are used.

      This property can also be set by assigning 'true' or 'false' to the system property 'com.luciad.util.TLcdCache.useWeakKeyReferences'. A system property can be set using the command line option -Dkey=value to java.

      Parameters:
      aUseWeakKeyReferences - True to allow weak references for the keys, false otherwise.
      See Also:
    • isUseSoftValueReferences

      public static boolean isUseSoftValueReferences()
      Returns true if soft references for the values are used. The default is true.
      Returns:
      true if soft references for the values are used.
    • setUseSoftValueReferences

      public static void setUseSoftValueReferences(boolean aUseSoftValueReferences)
      Sets if soft references for the values are used.

      This property can also be set by assigning 'true' or 'false' to the system property 'com.luciad.util.TLcdCache.useSoftValueReferences'. A system property can be set using the command line option -Dkey=value to java.

      Parameters:
      aUseSoftValueReferences - True to allow soft references for the values, false otherwise.
    • size

      public int size()
      Returns the amount of key-value pairs in this TLcdCache object.
      Returns:
      the amount of key-value pairs in this TLcdCache object.
    • insertIntoCache

      public void insertIntoCache(Object aKey, Object aValue)
      Description copied from interface: ILcdCache
      Inserts a cache Object corresponding to the given key Object.
      Specified by:
      insertIntoCache in interface ILcdCache
      Parameters:
      aKey - the key Object that will be used to identify the Object. The key must therefore be a unique identifier, typically the caller itself: insertIntoCache(this, ...).
      aValue - the Object to be cached.
    • removeCachedObject

      public Object removeCachedObject(Object aKey)
      Description copied from interface: ILcdCache
      Looks up and removes the cached Object corresponding to the given key.
      Specified by:
      removeCachedObject in interface ILcdCache
      Parameters:
      aKey - the key Object that was used for storing the cache Object.
      Returns:
      the cached Object, or null if there was no Object corresponding to the given key.
    • clearCache

      public void clearCache()
      Description copied from interface: ILcdCache
      Clears the cache.
      Specified by:
      clearCache in interface ILcdCache
    • getCachedObject

      public Object getCachedObject(Object aKey)
      Description copied from interface: ILcdCache
      Looks up and returns the cached Object corresponding to the given key.
      Specified by:
      getCachedObject in interface ILcdCache
      Parameters:
      aKey - the key Object that was used for storing the cache Object.
      Returns:
      the cached Object, or null if there is no Object corresponding to the given key.