|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.tapestry.util.pool.Pool
public class Pool
A Pool is used to pool instances of a useful class. It uses
keys, much like a Map
, to identify a list of pooled objects.
Retrieving an object from the Pool atomically removes it from the
pool. It can then be stored again later. In this way, a single
Pool instance can manage many different types of pooled objects,
filed under different keys.
Unlike traditional Pools, this class does not create new instances of
the objects it stores (with the exception of simple Java Beans,
via retrieve(Class)
. The usage pattern is to retrieve an instance
from the Pool, and if the instance is null, create a new instance.
The implementation of Pool is threadsafe.
Pool implements ICleanable
, with a goal of
only keeping pooled objects that have been needed within
a recent time frame. A generational system is used, where each
pooled object is assigned a generation count. executeCleanup()
discards objects whose generation count is too old (outside of a
window
).
Objects in the pool can receive two notifications: one notification
when they are stored
into the pool,
and one when they are discarded from the pool.
Classes that implement IPoolable
receive notifications directly, as per the two methods
of that interface.
Alternately, an adaptor for the other classes can be
registerered (using registerAdaptor(Class, IPoolableAdaptor)
.
The adaptor will be invoked to handle the notification when a
pooled object is stored or discarded.
Constructor Summary | |
---|---|
Pool()
Creates a new Pool using the default map size. |
|
Pool(boolean useSharedJanitor)
|
|
Pool(int mapSize)
Deprecated. Use Pool() instead. |
|
Pool(int mapSize,
boolean useSharedJanitor)
Deprecated. Use Pool(boolean) instead. |
Method Summary | |
---|---|
void |
clear()
Removes all previously pooled objects from this Pool. |
void |
executeCleanup()
Peforms culling of unneeded pooled objects. |
IPoolableAdaptor |
getAdaptor(Object object)
Returns an adaptor appropriate to the object. |
int |
getKeyCount()
Returns the number of keys within the pool. |
int |
getPooledCount()
Returns the number of object pooled, the sum of the number of objects in pooled under each key. |
int |
getWindow()
Returns the window used to cull pooled objects during a cleanup. |
void |
registerAdaptor(Class registrationClass,
IPoolableAdaptor adaptor)
Registers an adaptor for a particular class (or interface). |
protected void |
registerAdaptors()
Invoked from the constructor to register the default set of IPoolableAdaptor . |
void |
renderDescription(IMarkupWriter writer)
Object should render a description of itself to the writer. |
Object |
retrieve(Class objectClass)
Retrieves an instance of the named class. |
Object |
retrieve(Object key)
Returns a previously pooled object with the given key, or null if no such object exists. |
void |
setWindow(int value)
Sets the window, or number of generations that an object may stay in the pool before being culled. |
void |
store(Object object)
Stores an object using its class as a key. |
void |
store(Object key,
Object object)
Stores an object in the pool for later retrieval, resetting the object for storage within the pool. |
String |
toString()
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public Pool()
public Pool(int mapSize)
Pool()
instead.
public Pool(boolean useSharedJanitor)
useSharedJanitor
- if true, then the Pool is added to
the shared janitor
.public Pool(int mapSize, boolean useSharedJanitor)
Pool(boolean)
instead.
mapSize
- initial size of the map.useSharedJanitor
- if true, then the Pool is added to
the shared janitor
.Method Detail |
---|
public int getWindow()
public void setWindow(int value)
IllegalArgumentException
- if value is less than 1.public Object retrieve(Object key)
store(Object,Object)
.
public Object retrieve(Class objectClass)
However, don't be fooled by false economies. Unless
an object is very expensive to create, pooling is
more expensive than simply instantiating temporary
instances and letting the garbage collector deal with it
(this is counter intuitive, but true). For example,
this method was originally created to allow pooling
of StringBuffer
, but testing showed that it
was a net defecit.
public void store(Object object)
retrieve(Class)
public void store(Object key, Object object)
public void clear()
public int getPooledCount()
public int getKeyCount()
public void executeCleanup()
executeCleanup
in interface ICleanable
public String toString()
toString
in class Object
public void renderDescription(IMarkupWriter writer)
IRenderDescription
renderDescription
in interface IRenderDescription
protected void registerAdaptors()
IPoolableAdaptor
. Subclasses
may override this to register a different set.
Registers:
NullPoolableAdaptor
for class Object
DefaultPoolableAdaptor
for interface IPoolable
StringBufferAdaptor
for StringBuffer
public void registerAdaptor(Class registrationClass, IPoolableAdaptor adaptor)
public IPoolableAdaptor getAdaptor(Object object)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |