org.codehaus.plexus.util

Class SweeperPool

public class SweeperPool extends Object

Pools a bunch of objects . Runs a sweeper periodically to keep it down to size. The objects in the pool first get disposed first.

Author: Bert van Brakel

Nested Class Summary
static classSweeperPool.Sweeper
Periodically at sweepInterval goes through and tests if the pool should be trimmed.
Field Summary
static booleanDEBUG
intmaxSize
Absolute maxiumuim size of the pool.
intminSize
The size the pool gets trimmed down to
ArrayListpooledObjects
Holds the pooled objects
booleanshuttingDown
Flag indicating this pool is shuting down
SweeperPool.Sweepersweeper
Sweeps the pool periodically to trim it's size
inttriggerSize
When the sweeper runs and the pool is over this size, then the pool is trimmed
Constructor Summary
SweeperPool(int maxSize, int minSize, int intialCapacity, int sweepInterval, int triggerSize)

There are a number of settings to control how the pool operates.

Method Summary
voiddispose()
Dispose of this pool.
Objectget()
Return the pooled object
intgetSize()
Return the number of pooled objects.
voidobjectAdded(Object obj)
Override this to be notified of object addition.
voidobjectDisposed(Object obj)
Override this to be notified of object disposal.
voidobjectRetrieved(Object obj)
Override this to be notified of object retrieval.
booleanput(Object obj)
Add an object to the pool
intsaneConvert(int value)
voidtrim()
Trim the pool down to min size

Field Detail

DEBUG

private static final boolean DEBUG

maxSize

private transient int maxSize
Absolute maxiumuim size of the pool.

minSize

private transient int minSize
The size the pool gets trimmed down to

pooledObjects

private ArrayList pooledObjects
Holds the pooled objects

shuttingDown

private boolean shuttingDown
Flag indicating this pool is shuting down

sweeper

private transient SweeperPool.Sweeper sweeper
Sweeps the pool periodically to trim it's size

triggerSize

private int triggerSize
When the sweeper runs and the pool is over this size, then the pool is trimmed

Constructor Detail

SweeperPool

public SweeperPool(int maxSize, int minSize, int intialCapacity, int sweepInterval, int triggerSize)

There are a number of settings to control how the pool operates.

Any value less than 0 is automatically converted to 0

Method Detail

dispose

public void dispose()
Dispose of this pool. Stops the sweeper and disposes each object in the pool

get

public Object get()
Return the pooled object

getSize

public int getSize()
Return the number of pooled objects. This is never greater than t maximuim size of the pool

Returns: the number of pooled objects

objectAdded

public void objectAdded(Object obj)
Override this to be notified of object addition. Called before object is to be added.

Parameters: obj

objectDisposed

public void objectDisposed(Object obj)
Override this to be notified of object disposal. Called after the object has been removed. Occurs when the pool is trimmed.

Parameters: obj

objectRetrieved

public void objectRetrieved(Object obj)
Override this to be notified of object retrieval. Called after object removed from the pool, but before returned to the client.

Parameters: obj

put

public boolean put(Object obj)
Add an object to the pool

Parameters: obj the object to pool. Can be null.

Returns: true if the object was added to the pool, false if it was disposed or null

saneConvert

private int saneConvert(int value)

trim

public void trim()
Trim the pool down to min size