|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.sourceforge.groboutils.util.datastruct.v1.ObjectCache
An object cache which allows for objects to be added and removed. If the cache is empty when an object is requested, the object type is created and returned. There can be a maximum size specified for the pending object list - if an object is retrieved by the cache, and the list is beyond its size, then the object is thrown away. By default, the maximum size is unlimited.
If the cache should not create objects, then a ObjectCreator should not be given to the cache.
Alternatively, you can specify that the cache not create objects and instead wait for the objects to be retrieved.
Care has been taken to keep this synchronized across threads.
Nested Class Summary | |
static class |
ObjectCache.DefaultObjectCreator
A default object creator - given a Class object, it attempts to create a new Object using the default constructor. |
static interface |
ObjectCache.ObjectCreator
An interface which needs to be implemented and given to the cache in order to create new instances. |
Field Summary | |
static int |
UNLIMITED_SIZE
The size to use when you want to specify an unlimited cache size. |
Constructor Summary | |
ObjectCache()
Create a new ObjectCache without an object creator. |
|
ObjectCache(java.lang.Class creator)
Create a new ObjectCache. |
|
ObjectCache(java.lang.Class creator,
int maxSize)
Create a new ObjectCache. |
|
ObjectCache(java.lang.Class creator,
int maxSize,
boolean fill)
Create a new ObjectCache. |
|
ObjectCache(int maxSize)
Create a new ObjectCache without an object creator, and sets the maximum number of objects to keep waiting in the cache. |
|
ObjectCache(ObjectCache.ObjectCreator creator)
Create a new ObjectCache. |
|
ObjectCache(ObjectCache.ObjectCreator creator,
int maxSize)
Create a new ObjectCache. |
|
ObjectCache(ObjectCache.ObjectCreator creator,
int maxSize,
boolean fill)
Create a new ObjectCache. |
Method Summary | |
void |
addObject()
Create a new object and put it into the cache. |
protected java.lang.Object |
createObject()
Generates an Object for the cache. |
void |
fillCache()
Fills the cache to its maximum. |
java.lang.Object |
get()
Retrieves a cached element. |
java.lang.Object |
get(long millisWaitTime)
Retrieves a cached element. |
int |
getMaxSize()
|
int |
getOverflows()
Retrieves the number of "overflows" encountered. |
int |
getUnderflows()
Retrieves the number of "underflows" encountered. |
void |
putBack(java.lang.Object o)
Adds an element to the end of the queue. |
void |
setClassCreator(java.lang.Class creator)
Creates a new DefaultObjectCreator based on the given class. |
void |
setMaxSize(int size)
Resets the internal maximum number of objects that the cache can hold. |
void |
setObjectCreator(ObjectCache.ObjectCreator creator)
Sets the internal cache-underflow Object creator. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final int UNLIMITED_SIZE
Constructor Detail |
public ObjectCache()
public ObjectCache(int maxSize)
public ObjectCache(ObjectCache.ObjectCreator creator)
public ObjectCache(java.lang.Class creator)
public ObjectCache(ObjectCache.ObjectCreator creator, int maxSize)
public ObjectCache(java.lang.Class creator, int maxSize)
public ObjectCache(ObjectCache.ObjectCreator creator, int maxSize, boolean fill)
fill
- true if the cache should be filled at
construction time, or false if it should be empty
initially.public ObjectCache(java.lang.Class creator, int maxSize, boolean fill)
fill
- true if the cache should be filled at
construction time, or false if it should be empty
initially.Method Detail |
public void putBack(java.lang.Object o)
o
- the object to place at the end of the list.public java.lang.Object get()
Synchronized so that the time between the isEmpty check and the pull does not have another thread pulling out an instance. Only the get needs to be synchronized, so as to not mess with the checks.
public java.lang.Object get(long millisWaitTime) throws java.lang.InterruptedException
get()
with
a null creator.
putBack( Object )
into the cache.
putBack( Object )
. If by that time the cache is still
empty, then null is returned.
Important parts of the code are synchronized.
java.lang.InterruptedException
public int getOverflows()
putBack( Object )
is called.
public int getUnderflows()
get()
is called.
public void setMaxSize(int size)
putBack( Object )
ignoring
overflows.
public int getMaxSize()
public void setObjectCreator(ObjectCache.ObjectCreator creator)
public void setClassCreator(java.lang.Class creator)
public void addObject()
putBack( Object )
.
public void fillCache()
protected java.lang.Object createObject()
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |