Package com.luciad.util
Class TLcdCache
java.lang.Object
com.luciad.util.TLcdCache
- All Implemented Interfaces:
ILcdCache
Implementation of
ILcdCache
. By default, it automatically removes cached key-value
pairs that are no longer used. It does so by using WeakReference
s 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 Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Clears the cache.getCachedObject
(Object aKey) Looks up and returns the cached Object corresponding to the given key.void
insertIntoCache
(Object aKey, Object aValue) Inserts a cache Object corresponding to the given key Object.static boolean
Returns true if soft references for the values are used.static boolean
Returns true if weak references for the keys are used.removeCachedObject
(Object aKey) Looks up and removes the cached Object corresponding to the given key.static void
setUseSoftValueReferences
(boolean aUseSoftValueReferences) Sets if soft references for the values are used.static void
setUseWeakKeyReferences
(boolean aUseWeakKeyReferences) Sets if weak references for the keys are used.int
size()
Returns the amount of key-value pairs in thisTLcdCache
object.
-
Constructor Details
-
TLcdCache
public TLcdCache()Creates a newTLcdCache
instance. It refers the keys weakly ifisUseWeakKeyReferences()
returnstrue
. It refers the values softly ifisUseSoftValueReferences()
returnstrue
. Please refer toTLcdCache
for more information on the handling of keys and values. -
TLcdCache
public TLcdCache(boolean aUseWeakKeyReferences) Creates a newTLcdCache
instance. It refers the values softly ifisUseSoftValueReferences()
returnstrue
.- Parameters:
aUseWeakKeyReferences
- True if the keys should be referred weakly, false otherwise. Please refer toTLcdCache
for more information on the handling of keys and values.
-
TLcdCache
public TLcdCache(boolean aUseWeakKeyReferences, boolean aUseSoftValueReferences) Creates a newTLcdCache
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 toTLcdCache
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 thisTLcdCache
object.- Returns:
- the amount of key-value pairs in this
TLcdCache
object.
-
insertIntoCache
Description copied from interface:ILcdCache
Inserts a cache Object corresponding to the given key Object.- Specified by:
insertIntoCache
in interfaceILcdCache
- 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
Description copied from interface:ILcdCache
Looks up and removes the cached Object corresponding to the given key.- Specified by:
removeCachedObject
in interfaceILcdCache
- 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 interfaceILcdCache
-
getCachedObject
Description copied from interface:ILcdCache
Looks up and returns the cached Object corresponding to the given key.- Specified by:
getCachedObject
in interfaceILcdCache
- 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.
-