public class TLcdCache extends Object implements ILcdCache
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 and Description |
---|
TLcdCache()
Creates a new
TLcdCache instance. |
TLcdCache(boolean aUseWeakKeyReferences)
Creates a new
TLcdCache instance. |
TLcdCache(boolean aUseWeakKeyReferences,
boolean aUseSoftValueReferences)
Creates a new
TLcdCache instance. |
Modifier and Type | Method and Description |
---|---|
void |
clearCache()
Clears the cache.
|
Object |
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 |
isUseSoftValueReferences()
Returns true if soft references for the values are used.
|
static boolean |
isUseWeakKeyReferences()
Returns true if weak references for the keys are used.
|
Object |
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 this
TLcdCache object. |
public TLcdCache()
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.public TLcdCache(boolean aUseWeakKeyReferences)
TLcdCache
instance.
It refers the values softly if isUseSoftValueReferences()
returns true
.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.public TLcdCache(boolean aUseWeakKeyReferences, boolean aUseSoftValueReferences)
TLcdCache
instance.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.public static boolean isUseWeakKeyReferences()
TLcdCache()
public static void setUseWeakKeyReferences(boolean aUseWeakKeyReferences)
aUseWeakKeyReferences
- True to allow weak references for the keys, false otherwise.TLcdCache()
public static boolean isUseSoftValueReferences()
public static void setUseSoftValueReferences(boolean aUseSoftValueReferences)
aUseSoftValueReferences
- True to allow soft references for the values, false otherwise.public int size()
TLcdCache
object.TLcdCache
object.public void insertIntoCache(Object aKey, Object aValue)
ILcdCache
insertIntoCache
in interface ILcdCache
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.public Object removeCachedObject(Object aKey)
ILcdCache
removeCachedObject
in interface ILcdCache
aKey
- the key Object that was used for storing the cache Object.public void clearCache()
ILcdCache
clearCache
in interface ILcdCache
public Object getCachedObject(Object aKey)
ILcdCache
getCachedObject
in interface ILcdCache
aKey
- the key Object that was used for storing the cache Object.