Class PoolUtils.CheckedObjectPool<T>
java.lang.Object
org.apache.commons.pool.PoolUtils.CheckedObjectPool<T>
- All Implemented Interfaces:
ObjectPool<T>
- Enclosing class:
PoolUtils
An object pool that performs type checking on objects passed
to pool methods.
- Since:
- Pool 1.3
- Version:
- $Revision: 1222670 $ $Date: 2011-12-23 08:18:25 -0500 (Fri, 23 Dec 2011) $
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCheckedObjectPool(ObjectPool<T> pool, Class<T> type) Create a CheckedObjectPool accepting objects of the given type using the given pool. -
Method Summary
Modifier and TypeMethodDescriptionvoidCreate an object using thefactoryor other implementation dependent mechanism, passivate it, and then place it in the idle object pool.Borrow an object from the pool, checking its type.voidclear()Clears any objects sitting idle in the pool, releasing any associated resources (optional operation).voidclose()Close this pool, and free any resources associated with it.intReturn the number of instances currently borrowed from this pool (optional operation).intReturn the number of instances currently idle in this pool (optional operation).voidinvalidateObject(T obj) Invalidates an object from the pool, verifying that it is of the expected type.voidreturnObject(T obj) Return an object to the pool, verifying that it is of the correct type.voidsetFactory(PoolableObjectFactory<T> factory) Deprecated.to be removed in version 2.0toString()
-
Field Details
-
type
-
pool
Underlying object pool
-
-
Constructor Details
-
CheckedObjectPool
CheckedObjectPool(ObjectPool<T> pool, Class<T> type) Create a CheckedObjectPool accepting objects of the given type using the given pool.- Parameters:
pool- underlying object pooltype- expected pooled object type- Throws:
IllegalArgumentException- if either parameter is null
-
-
Method Details
-
borrowObject
Borrow an object from the pool, checking its type.- Specified by:
borrowObjectin interfaceObjectPool<T>- Returns:
- a type-checked object from the pool
- Throws:
ClassCastException- if the object returned by the pool is not of the expected typeException- whenmakeObjectthrows an exception.NoSuchElementException- when the pool is exhausted and cannot or will not return another instance.IllegalStateException- afterclosehas been called on this pool.
-
returnObject
Return an object to the pool, verifying that it is of the correct type.- Specified by:
returnObjectin interfaceObjectPool<T>- Parameters:
obj- object to return- Throws:
ClassCastException- if obj is not of the expected type
-
invalidateObject
Invalidates an object from the pool, verifying that it is of the expected type.- Specified by:
invalidateObjectin interfaceObjectPool<T>- Parameters:
obj- object to invalidate- Throws:
ClassCastException- if obj is not of the expected type
-
addObject
Create an object using thefactoryor other implementation dependent mechanism, passivate it, and then place it in the idle object pool.addObjectis useful for "pre-loading" a pool with idle objects. (Optional operation).- Specified by:
addObjectin interfaceObjectPool<T>- Throws:
Exception- whenPoolableObjectFactory.makeObject()fails.IllegalStateException- afterObjectPool.close()has been called on this pool.UnsupportedOperationException- when this pool cannot add new idle objects.
-
getNumIdle
Return the number of instances currently idle in this pool (optional operation). This may be considered an approximation of the number of objects that can beborrowedwithout creating any new instances. Returns a negative value if this information is not available.- Specified by:
getNumIdlein interfaceObjectPool<T>- Returns:
- the number of instances currently idle in this pool or a negative value if unsupported
- Throws:
UnsupportedOperationException- deprecated: if this implementation does not support the operation
-
getNumActive
Return the number of instances currently borrowed from this pool (optional operation). Returns a negative value if this information is not available.- Specified by:
getNumActivein interfaceObjectPool<T>- Returns:
- the number of instances currently borrowed from this pool or a negative value if unsupported
- Throws:
UnsupportedOperationException- deprecated: if this implementation does not support the operation
-
clear
Clears any objects sitting idle in the pool, releasing any associated resources (optional operation). Idle objects cleared must bedestroyed.- Specified by:
clearin interfaceObjectPool<T>- Throws:
UnsupportedOperationException- if this implementation does not support the operationException
-
close
public void close()Close this pool, and free any resources associated with it.Calling
ObjectPool.addObject()orObjectPool.borrowObject()after invoking this method on a pool will cause them to throw anIllegalStateException.- Specified by:
closein interfaceObjectPool<T>
-
setFactory
@Deprecated public void setFactory(PoolableObjectFactory<T> factory) throws IllegalStateException, UnsupportedOperationException Deprecated.to be removed in version 2.0Sets the object factory associated with the pool- Specified by:
setFactoryin interfaceObjectPool<T>- Parameters:
factory- object factory- Throws:
IllegalStateException- when the factory cannot be set at this timeUnsupportedOperationException- if this implementation does not support the operation
-
toString
-