Package org.apache.commons.pool
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.
-
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 TypeMethodDescriptionvoid
Create an object using thefactory
or other implementation dependent mechanism, passivate it, and then place it in the idle object pool.Borrow an object from the pool, checking its type.void
clear()
Clears any objects sitting idle in the pool, releasing any associated resources (optional operation).void
close()
Close this pool, and free any resources associated with it.int
Return the number of instances currently borrowed from this pool (optional operation).int
Return the number of instances currently idle in this pool (optional operation).void
invalidateObject
(T obj) Invalidates an object from the pool, verifying that it is of the expected type.void
returnObject
(T obj) Return an object to the pool, verifying that it is of the correct type.void
setFactory
(PoolableObjectFactory<T> factory) Deprecated.to be removed in version 2.0toString()
-
Field Details
-
type
Type of objects allowed in the pool. This should be a subtype of the return type of the underlying pool's associated object factory. -
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:
borrowObject
in interfaceObjectPool<T>
- Returns:
- a type-checked object from the pool
- Throws:
ClassCastException
- if the object returned by the pool is not of the expected typeIllegalStateException
- afterclose
has been called on this pool.Exception
- whenmakeObject
throws an exception.NoSuchElementException
- when the pool is exhausted and cannot or will not return another instance.
-
returnObject
Return an object to the pool, verifying that it is of the correct type.- Specified by:
returnObject
in 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:
invalidateObject
in interfaceObjectPool<T>
- Parameters:
obj
- object to invalidate- Throws:
ClassCastException
- if obj is not of the expected type
-
addObject
Create an object using thefactory
or other implementation dependent mechanism, passivate it, and then place it in the idle object pool.addObject
is useful for "pre-loading" a pool with idle objects. (Optional operation).- Specified by:
addObject
in 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 beborrowed
without creating any new instances. Returns a negative value if this information is not available.- Specified by:
getNumIdle
in 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:
getNumActive
in 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:
clear
in 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:
close
in 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:
setFactory
in 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
-