org.gjt.rec.util
Class ObjectPool

java.lang.Object
  |
  +--org.gjt.rec.util.ObjectPool

public final class ObjectPool
extends Object

This class implements an object pool. The pool is configured with an object factory and a capacity, although the capacity may be reconfigured at any time. Client code borrows objects from the pool and then returns them later. During this time, the client code has exclusive access to the object.

See Also:
ObjectFactory, ObjectPool.Listener

Nested Class Summary
static interface ObjectPool.Listener
          The interface implemented by objects that wish to be notified when they are removed from an object pool.
static class ObjectPool.NotPooledException
          Thrown to indicate that an object that was supposed to be contained in this object pool is in fact not.
 
Field Summary
static String COPYRIGHT
           
static String LICENSE
           
static String REVISION
           
 
Constructor Summary
ObjectPool(ObjectFactory fact)
          Constructs a new object pool with a particular object factory and the default capacity of 1.
 
Method Summary
 Object borrowObject()
          Borrows an object from this object pool.
 Collection getAvailableObjects()
          Determines the objects that are available for loan.
 int getCapacity()
          Determines the current capacity of this object pool.
 Collection getLoanedObjects()
          Determines the objects that are out on loan.
 void remove(Object obj)
          Removes a particular object from this object pool.
 void returnObject(Object obj)
          Returns a particular object to this object pool.
 void setCapacity(int newCapacity)
          Sets the capacity of this object pool to a particular size.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COPYRIGHT

public static final String COPYRIGHT
See Also:
Constant Field Values

LICENSE

public static final String LICENSE
See Also:
Constant Field Values

REVISION

public static final String REVISION
See Also:
Constant Field Values
Constructor Detail

ObjectPool

public ObjectPool(ObjectFactory fact)
Constructs a new object pool with a particular object factory and the default capacity of 1.

Parameters:
fact - the object factory with which this object pool will instantiate objects to populate itself
Throws:
NullPointerException - if (fact) is null
Method Detail

getCapacity

public int getCapacity()
Determines the current capacity of this object pool.

Returns:
the current capacity of this object pool

setCapacity

public void setCapacity(int newCapacity)
Sets the capacity of this object pool to a particular size. The default capacity of a pool is 1. The pool will sometimes appear to run over capacity if this value is adjusted downwards and too many objects are out on loan.

Parameters:
newCapacity - the new capacity of this object pool
Throws:
IllegalArgumentException - if the specified capacity is less than one

getAvailableObjects

public Collection getAvailableObjects()
Determines the objects that are available for loan.

Returns:
a collection of the objects that are available for loan.

getLoanedObjects

public Collection getLoanedObjects()
Determines the objects that are out on loan.

Returns:
a collection of the objects that are out on loan.

borrowObject

public Object borrowObject()
Borrows an object from this object pool. the object must be returned to the pool with the returnObject(Object) method if the jamming of this pool with permanently loaned out objects is to be avoided. This method will block until an object is available.

Returns:
an object from the pool

returnObject

public void returnObject(Object obj)
Returns a particular object to this object pool.

Parameters:
obj - the object to return to this pool
Throws:
ObjectPool.NotPooledException - if the supplied object was never part of this object pool

remove

public void remove(Object obj)
Removes a particular object from this object pool. The object will no longer be available for loan from this object pool.

Parameters:
obj - the object that will no longer be loaned out
Throws:
ObjectPool.NotPooledException - if the supplied object is not part of this pool