beowulf.util.pooling
Class ReflectionPool

java.lang.Object
  extended bybeowulf.util.pooling.ReflectionPool
All Implemented Interfaces:
Pool

public class ReflectionPool
extends Object
implements Pool

This implementation of Pool uses reflection to generate new instances of a particular object. All of the methods are synchronized making it thread safe, and there is very little overhead involved in checking an instance in or out. The pool size will never shrink, so this implementation is not recommended for server programs or daemon programs. The checkIn() method does not provide any type checking, so it is up to the user to ensure proper use. There may be substantial overhead for the first few calls to checkOut, but provided the user checks these objects back in, this will not happen again.

Version:
1.0 11/7/2003
Author:
Andy Scukanec (ags at cs dot cornell dot edu)

Field Summary
protected  Vector freeObjects
          The list of unused instances.
protected  Class poolClass
          The class of objects to be instantianted.
 
Constructor Summary
ReflectionPool(Class newPoolClass)
          This will create a new Pool that will pool instances of the Class given to the constructor.
ReflectionPool(String classname)
          This will create a new Pool that will pool instances of the Class that is represented by the String given to the constructor.
 
Method Summary
 void checkIn(Object returnedObject)
          This implementation of Pool will simply add the returned object to an instance of java.util.Vector.
 Object checkOut()
          This Pool implementation will first look in the java.util.Vector of unused objects.
 Class getPoolClass()
          This will return a reference to the class that this Pool instance is associated with.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

poolClass

protected Class poolClass
The class of objects to be instantianted.


freeObjects

protected Vector freeObjects
The list of unused instances.

Constructor Detail

ReflectionPool

public ReflectionPool(String classname)
               throws ClassNotFoundException
This will create a new Pool that will pool instances of the Class that is represented by the String given to the constructor. If this String does not represent a class that the JVM can find, a ClassNotFoundException will be thrown.

Parameters:
classname - The fully qualified path name of the class to be associated with this Pool.
Throws:
ClassNotFoundException - This will be thrown if classname is not a fully qualified path name of a valid class.

ReflectionPool

public ReflectionPool(Class newPoolClass)
This will create a new Pool that will pool instances of the Class given to the constructor.

Parameters:
newPoolClass - The class that will be associated with this Pool.
Method Detail

getPoolClass

public Class getPoolClass()
This will return a reference to the class that this Pool instance is associated with.

Specified by:
getPoolClass in interface Pool
Returns:
The associated object class.

checkOut

public Object checkOut()
This Pool implementation will first look in the java.util.Vector of unused objects. If one exists, it will be removed from the java.util.Vector and returned in constant time with very little overhead. If not, reflection will be used to instantiate a new instance of the associated class. This instance will be returned.

Specified by:
checkOut in interface Pool
Returns:
A "new" instance of the object that this pool creates.

checkIn

public void checkIn(Object returnedObject)
This implementation of Pool will simply add the returned object to an instance of java.util.Vector. The operation will be done in constant time with very little overhead. This implementation does not do any checking to ensure that returnedObject is an instance of the class that is associated with this Pool. This is done for efficiency reasons.

Specified by:
checkIn in interface Pool
Parameters:
returnedObject - The object to be checked back in.