public class ReferenceObjectCache extends Object implements ObjectCache
WeakReference
or SoftReference
to them. Java Garbage
Collection can then free these objects if no one has a "strong" reference to the object (weak) or if it runs out of
memory (soft).Constructor and Description |
---|
ReferenceObjectCache(boolean useWeak) |
Modifier and Type | Method and Description |
---|---|
<T> void |
cleanNullReferences(Class<T> clazz)
Run through the map and remove any references that have been null'd out by the GC.
|
<T> void |
cleanNullReferencesAll()
Run through all maps and remove any references that have been null'd out by the GC.
|
<T> void |
clear(Class<T> clazz)
Remove all entries from the cache of a certain class.
|
void |
clearAll()
Remove all entries from the cache of all classes.
|
<T,ID> T |
get(Class<T> clazz,
ID id)
Lookup in the cache for an object of a certain class that has a certain id.
|
static ReferenceObjectCache |
makeSoftCache()
Create and return an object cache using
SoftReference . |
static ReferenceObjectCache |
makeWeakCache()
Create and return an object cache using
WeakReference . |
<T,ID> void |
put(Class<T> clazz,
ID id,
T data)
Put an object in the cache that has a certain class and id.
|
<T> void |
registerClass(Class<T> clazz)
Register a class for use with this class.
|
<T,ID> void |
remove(Class<T> clazz,
ID id)
Delete from the cache an object of a certain class that has a certain id.
|
<T> int |
size(Class<T> clazz)
Return the number of elements in the cache.
|
int |
sizeAll()
Return the number of elements in all of the caches.
|
<T,ID> T |
updateId(Class<T> clazz,
ID oldId,
ID newId)
Change the id in the cache for an object of a certain class from an old-id to a new-id.
|
public ReferenceObjectCache(boolean useWeak)
useWeak
- Set to true if you want the cache to use WeakReference
. If false then the cache will use
SoftReference
.public static ReferenceObjectCache makeWeakCache()
WeakReference
.public static ReferenceObjectCache makeSoftCache()
SoftReference
.public <T> void registerClass(Class<T> clazz)
ObjectCache
registerClass
in interface ObjectCache
public <T,ID> T get(Class<T> clazz, ID id)
ObjectCache
get
in interface ObjectCache
public <T,ID> void put(Class<T> clazz, ID id, T data)
ObjectCache
put
in interface ObjectCache
public <T> void clear(Class<T> clazz)
ObjectCache
clear
in interface ObjectCache
public void clearAll()
ObjectCache
clearAll
in interface ObjectCache
public <T,ID> void remove(Class<T> clazz, ID id)
ObjectCache
remove
in interface ObjectCache
public <T,ID> T updateId(Class<T> clazz, ID oldId, ID newId)
ObjectCache
updateId
in interface ObjectCache
public <T> int size(Class<T> clazz)
ObjectCache
size
in interface ObjectCache
public int sizeAll()
ObjectCache
sizeAll
in interface ObjectCache
public <T> void cleanNullReferences(Class<T> clazz)
public <T> void cleanNullReferencesAll()
This documentation is licensed by Gray Watson under the Creative Commons Attribution-Share Alike 3.0 License.