org.sadun.util.pool
Class ExtendedObjectPool

java.lang.Object
  extended byorg.sadun.util.pool.ExtendedObjectPool

public class ExtendedObjectPool
extends java.lang.Object

An object pool identical to ObjectPool but which requires the acquiring object to identify itself.

Author:
Cristiano Sadun

Constructor Summary
ExtendedObjectPool(int n, java.lang.Class cls)
          Create a pool of n object of the given class using the default constructor.
ExtendedObjectPool(int n, java.lang.Class cls, java.lang.Object[] params)
          Create a pool of n object of the given class using the given construction parameters.
ExtendedObjectPool(int n, java.lang.Class cls, java.lang.Object[] params, Setup ps)
          Create a pool of n object of the given class using the given construction parameters.
ExtendedObjectPool(int n, ObjectPool.Factory factory)
          Create a pool of n objects using the given factory
ExtendedObjectPool(int n, java.lang.String clsName)
          Create a pool of n object of the given class (by name) using the default constructor.
ExtendedObjectPool(int n, java.lang.String clsName, java.lang.Object[] params)
          Create a pool of n object of the given class (by name) using the given construction parameters.
ExtendedObjectPool(int n, java.lang.String clsName, java.lang.Object[] params, Setup ps)
          Create a pool of n object of the given class (by name) using the given construction parameters.
 
Method Summary
 java.lang.Object acquire(java.lang.Object requester)
          Attempt to acquire an object.
 java.lang.Object acquire(java.lang.Object requester, boolean waitIfUnavailable)
          Attempt to acquire an object.
 java.lang.Object acquire(java.lang.Object requester, int i)
          Attempt to acquire the i-th object.
 int getFreeCount()
          Return the number of available objects in the pool
 int getSize()
          Return the size of the pool
 int getUsedCount()
          Return the number of used objects in the pool
static void main(java.lang.String[] args)
           
 void release(java.lang.Object obj)
          Release an object, notifying waiting thread (if any) that one pooled object has become available.
 void release(java.lang.Object obj, boolean releaseWaitingCalls)
          Release an object.
 java.lang.Object renew(java.lang.Object owner, java.lang.Object obj)
          Renew one object in the pool.
 java.lang.String toString()
          Return a string description of the pool
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ExtendedObjectPool

public ExtendedObjectPool(int n,
                          ObjectPool.Factory factory)
Create a pool of n objects using the given factory


ExtendedObjectPool

public ExtendedObjectPool(int n,
                          java.lang.String clsName,
                          java.lang.Object[] params,
                          Setup ps)
                   throws java.lang.ClassNotFoundException
Create a pool of n object of the given class (by name) using the given construction parameters.

If some post-construction setup is needed, it can be provided as an Setup object.

Parameters:
n - the size of the pool
clsName - the name of the class of the objects to pool
params - the construction parameters, or null
Throws:
java.lang.ClassNotFoundException - if the given class name cannot be resolved

ExtendedObjectPool

public ExtendedObjectPool(int n,
                          java.lang.String clsName,
                          java.lang.Object[] params)
                   throws java.lang.ClassNotFoundException
Create a pool of n object of the given class (by name) using the given construction parameters.

Parameters:
n - the size of the pool
clsName - the name of the class of the objects to pool
params - the construction parameters, or null
Throws:
java.lang.ClassNotFoundException - if the given class name cannot be resolved

ExtendedObjectPool

public ExtendedObjectPool(int n,
                          java.lang.String clsName)
                   throws java.lang.ClassNotFoundException
Create a pool of n object of the given class (by name) using the default constructor.

Parameters:
n - the size of the pool
clsName - the name of the class of the objects to pool
Throws:
java.lang.ClassNotFoundException - if the given class name cannot be resolved

ExtendedObjectPool

public ExtendedObjectPool(int n,
                          java.lang.Class cls,
                          java.lang.Object[] params,
                          Setup ps)
Create a pool of n object of the given class using the given construction parameters.

If some post-construction setup is needed, it can be provided as an Setup object.

Parameters:
n - the size of the pool
cls - the class of the objects to pool
params - the construction parameters, or null

ExtendedObjectPool

public ExtendedObjectPool(int n,
                          java.lang.Class cls,
                          java.lang.Object[] params)
Create a pool of n object of the given class using the given construction parameters.

Parameters:
n - the size of the pool
cls - the class of the objects to pool
params - the construction parameters, or null

ExtendedObjectPool

public ExtendedObjectPool(int n,
                          java.lang.Class cls)
Create a pool of n object of the given class using the default constructor.

Parameters:
n - the size of the pool
cls - the class of the objects to pool
Method Detail

getFreeCount

public int getFreeCount()
Return the number of available objects in the pool

Returns:
the number of available objects in the pool

getUsedCount

public int getUsedCount()
Return the number of used objects in the pool

Returns:
the number of used objects in the pool

getSize

public int getSize()
Return the size of the pool

Returns:
the size of the pool

acquire

public java.lang.Object acquire(java.lang.Object requester,
                                boolean waitIfUnavailable)
Attempt to acquire an object.

Parameters:
requester - the object performing the attempt
waitIfUnavailable - if true, in case all the pooled objects are used, the call will block until an object is released. If false, in the same condition the method returns null.
Returns:
a pooled object

acquire

public java.lang.Object acquire(java.lang.Object requester)
Attempt to acquire an object.

If there aren't any objects available, this method blocks until one becomes available.

Parameters:
requester - the object performing the attempt
Returns:
a pooled object

acquire

public java.lang.Object acquire(java.lang.Object requester,
                                int i)
Attempt to acquire the i-th object.

If the object is not available, this method blocks until the object becomes available.

Parameters:
requester - the object performing the attempt
Returns:
a pooled object

release

public void release(java.lang.Object obj,
                    boolean releaseWaitingCalls)
Release an object.

Parameters:
releaseWaitingCalls - if true, the method notifies waiting objects that one pooled object has become available.

release

public void release(java.lang.Object obj)
Release an object, notifying waiting thread (if any) that one pooled object has become available.


renew

public java.lang.Object renew(java.lang.Object owner,
                              java.lang.Object obj)
Renew one object in the pool.

A new instance is created substituting the passed object in the pool, and the new instance is returned. The object is released() but any thread waiting on that object shifts waiting for another object.


toString

public java.lang.String toString()
Return a string description of the pool

Returns:
a string description of the pool

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Throws:
java.lang.Exception