| java.lang.Object net.sourceforge.groboutils.util.datastruct.v1.ObjectCache
ObjectCache | public class ObjectCache (Code) | | 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.
author: Matt Albrecht groboclown@users.sourceforge.net since: April 11, 2001 (0.9.0 Alpha) version: $Date: 2003/05/23 20:55:43 $ |
Inner Class :public static interface ObjectCreator | |
Inner Class :public static class DefaultObjectCreator implements ObjectCreator | |
Field Summary | |
final public static int | UNLIMITED_SIZE The size to use when you want to specify an unlimited cache size. |
Constructor Summary | |
public | ObjectCache() Create a new ObjectCache without an object creator. | public | ObjectCache(int maxSize) Create a new ObjectCache without an object creator, and
sets the maximum number of objects to keep waiting in the cache. | public | ObjectCache(ObjectCreator creator) Create a new ObjectCache. | public | ObjectCache(Class creator) Create a new ObjectCache. | public | ObjectCache(ObjectCreator creator, int maxSize) Create a new ObjectCache. | public | ObjectCache(Class creator, int maxSize) Create a new ObjectCache. | public | ObjectCache(ObjectCreator creator, int maxSize, boolean fill) Create a new ObjectCache. | public | ObjectCache(Class creator, int maxSize, boolean fill) Create a new ObjectCache. |
Method Summary | |
public void | addObject() Create a new object and put it into the cache. | protected Object | createObject() Generates an Object for the cache. | public void | fillCache() Fills the cache to its maximum. | public Object | get() Retrieves a cached element. | public Object | get(long millisWaitTime) Retrieves a cached element. | public int | getMaxSize() | public int | getOverflows() Retrieves the number of "overflows" encountered. | public int | getUnderflows() Retrieves the number of "underflows" encountered. | public void | putBack(Object o) Adds an element to the end of the queue. | public void | setClassCreator(Class creator) Creates a new DefaultObjectCreator based on the given class. | public void | setMaxSize(int size) Resets the internal maximum number of objects that the cache can
hold. | public void | setObjectCreator(ObjectCreator creator) Sets the internal cache-underflow Object creator. |
UNLIMITED_SIZE | final public static int UNLIMITED_SIZE(Code) | | The size to use when you want to specify an unlimited cache size.
|
ObjectCache | public ObjectCache()(Code) | | Create a new ObjectCache without an object creator.
|
ObjectCache | public ObjectCache(int maxSize)(Code) | | Create a new ObjectCache without an object creator, and
sets the maximum number of objects to keep waiting in the cache.
|
ObjectCache | public ObjectCache(ObjectCreator creator)(Code) | | Create a new ObjectCache. This uses the given creator to
create new objects for the cache.
|
ObjectCache | public ObjectCache(Class creator)(Code) | | Create a new ObjectCache. This uses the given Class to
create new objects for the cache, using its default constructor.
|
ObjectCache | public ObjectCache(ObjectCreator creator, int maxSize)(Code) | | Create a new ObjectCache. This uses the given creator to
create new objects for the cache, and sets the internal maximum
number of elements to keep waiting.
|
ObjectCache | public ObjectCache(Class creator, int maxSize)(Code) | | Create a new ObjectCache. This uses the given Class to
create new objects for the cache, using its default constructor,
and sets the internal maximum
number of elements to keep waiting.
|
ObjectCache | public ObjectCache(ObjectCreator creator, int maxSize, boolean fill)(Code) | | Create a new ObjectCache. This uses the given creator to
create new objects for the cache, and sets the internal maximum
number of elements to keep waiting.
Parameters: fill - true if the cache should be filled atconstruction time, or false if it should be emptyinitially. |
ObjectCache | public ObjectCache(Class creator, int maxSize, boolean fill)(Code) | | Create a new ObjectCache. This uses the given Class to
create new objects for the cache, using its default constructor,
and sets the internal maximum
number of elements to keep waiting.
Parameters: fill - true if the cache should be filled atconstruction time, or false if it should be emptyinitially. |
createObject | protected Object createObject()(Code) | | Generates an Object for the cache. May return null.
|
fillCache | public void fillCache()(Code) | | Fills the cache to its maximum. If there is no maximum or there
is no creator, then nothing is done.
|
get | public Object get()(Code) | | Retrieves a cached element. If the cache is empty, and no
creator is known, then null is returned. If the cache is
empty, and a creator is known, then a new object is created and
returned.
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.
|
get | public Object get(long millisWaitTime) throws InterruptedException(Code) | | Retrieves a cached element. If the cache is empty, then one of several
things can happen, based on the time passed in:
- < 0: null is immediately returned. This is
the identical behavior to calling
ObjectCache.get() with
a null creator.
- 0: the routine will wait indefinitely until
an object is
ObjectCache.putBack(Object) into the cache.
- > 0: the routine will wait up to the given number
of milliseconds for another object to be
ObjectCache.putBack(Object) . If by that time the cache is still
empty, then null is returned.
Important parts of the code are synchronized.
|
getMaxSize | public int getMaxSize()(Code) | | |
getOverflows | public int getOverflows()(Code) | | Retrieves the number of "overflows" encountered. An overflow occurs
when the cache is full and a
ObjectCache.putBack(Object) is called.
|
getUnderflows | public int getUnderflows()(Code) | | Retrieves the number of "underflows" encountered. An underflow occurs
when the cache is empty and a
ObjectCache.get() is called.
|
putBack | public void putBack(Object o)(Code) | | Adds an element to the end of the queue. If the list is empty,
then the next waiting thread is woken up. If the list has one or
fewer elements, this this method will block any access to the queue,
otherwise this only blocks access to adding to the list.
Parameters: o - the object to place at the end of the list. |
setClassCreator | public void setClassCreator(Class creator)(Code) | | Creates a new DefaultObjectCreator based on the given class.
|
setMaxSize | public void setMaxSize(int size)(Code) | | Resets the internal maximum number of objects that the cache can
hold. Note that it does not immediately clear out the extra objects -
that is naturally cleared by the
ObjectCache.putBack(Object) ignoring
overflows.
|
setObjectCreator | public void setObjectCreator(ObjectCreator creator)(Code) | | Sets the internal cache-underflow Object creator.
|
|
|