Class BaseKeyedObjectPool<K,V>
- java.lang.Object
-
- org.apache.commons.pool.BaseKeyedObjectPool<K,V>
-
- Type Parameters:
K- the type of keys in this poolV- the type of objects held in this pool
- All Implemented Interfaces:
KeyedObjectPool<K,V>
- Direct Known Subclasses:
GenericKeyedObjectPool,StackKeyedObjectPool
public abstract class BaseKeyedObjectPool<K,V> extends java.lang.Object implements KeyedObjectPool<K,V>
A simple base implementation ofKeyedObjectPool. Optional operations are implemented to either do nothing, return a value indicating it is unsupported or throwUnsupportedOperationException.- Since:
- Pool 1.0
- Version:
- $Revision: 1222396 $ $Date: 2011-12-22 14:02:25 -0500 (Thu, 22 Dec 2011) $
-
-
Field Summary
Fields Modifier and Type Field Description private booleanclosedWhether or not the pool is close
-
Constructor Summary
Constructors Constructor Description BaseKeyedObjectPool()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddObject(K key)Not supported in this base implementation.protected voidassertOpen()Throws anIllegalStateExceptionwhen this pool has been closed.abstract VborrowObject(K key)Obtains an instance from this pool for the specifiedkey.voidclear()Not supported in this base implementation.voidclear(K key)Not supported in this base implementation.voidclose()Close this pool.intgetNumActive()Not supported in this base implementation.intgetNumActive(K key)Not supported in this base implementation.intgetNumIdle()Not supported in this base implementation.intgetNumIdle(K key)Not supported in this base implementation.abstract voidinvalidateObject(K key, V obj)Invalidates an object from the pool.protected booleanisClosed()Has this pool instance been closed.abstract voidreturnObject(K key, V obj)Return an instance to the pool.voidsetFactory(KeyedPoolableObjectFactory<K,V> factory)Deprecated.to be removed in pool 2.0
-
-
-
Method Detail
-
borrowObject
public abstract V borrowObject(K key) throws java.lang.Exception
Obtains an instance from this pool for the specifiedkey.Instances returned from this method will have been either newly created with
makeObjector will be a previously idle object and have been activated withactivateObjectand then validated withvalidateObject.By contract, clients must return the borrowed object using
returnObject,invalidateObject, or a related method as defined in an implementation or sub-interface, using akeythat isequivalentto the one used to borrow the instance in the first place.The behaviour of this method when the pool has been exhausted is not strictly specified (although it may be specified by implementations). Older versions of this method would return
nullto indicate exhaustion, newer versions are encouraged to throw aNoSuchElementException.- Specified by:
borrowObjectin interfaceKeyedObjectPool<K,V>- Parameters:
key- the key used to obtain the object- Returns:
- an instance from this pool.
- Throws:
java.lang.IllegalStateException- afterclosehas been called on this pooljava.lang.Exception- whenmakeObjectthrows an exceptionjava.util.NoSuchElementException- when the pool is exhausted and cannot or will not return another instance
-
returnObject
public abstract void returnObject(K key, V obj) throws java.lang.Exception
Return an instance to the pool. By contract,objmust have been obtained usingborrowObjector a related method as defined in an implementation or sub-interface using akeythat is equivalent to the one used to borrow the instance in the first place.- Specified by:
returnObjectin interfaceKeyedObjectPool<K,V>- Parameters:
key- the key used to obtain the objectobj- aborrowedinstance to be returned.- Throws:
java.lang.Exception
-
invalidateObject
public abstract void invalidateObject(K key, V obj) throws java.lang.Exception
Invalidates an object from the pool.
By contract,
objmust have been obtained usingborrowObjectusing akeythat is equivalent to the one used to borrow theObjectin the first place.This method should be used when an object that has been borrowed is determined (due to an exception or other problem) to be invalid.
- Specified by:
invalidateObjectin interfaceKeyedObjectPool<K,V>- Parameters:
key- the key used to obtain the objectobj- aborrowedinstance to be returned.- Throws:
java.lang.Exception
-
addObject
public void addObject(K key) throws java.lang.Exception, java.lang.UnsupportedOperationException
Not supported in this base implementation. Always throws anUnsupportedOperationException, subclasses should override this behavior.- Specified by:
addObjectin interfaceKeyedObjectPool<K,V>- Parameters:
key- ignored- Throws:
java.lang.UnsupportedOperationExceptionjava.lang.Exception- whenKeyedPoolableObjectFactory.makeObject(K)fails.java.lang.IllegalStateException- afterKeyedObjectPool.close()has been called on this pool.
-
getNumIdle
public int getNumIdle(K key) throws java.lang.UnsupportedOperationException
Not supported in this base implementation.- Specified by:
getNumIdlein interfaceKeyedObjectPool<K,V>- Parameters:
key- ignored- Returns:
- a negative value.
- Throws:
java.lang.UnsupportedOperationException- deprecated: when this implementation doesn't support the operation
-
getNumActive
public int getNumActive(K key) throws java.lang.UnsupportedOperationException
Not supported in this base implementation.- Specified by:
getNumActivein interfaceKeyedObjectPool<K,V>- Parameters:
key- ignored- Returns:
- a negative value.
- Throws:
java.lang.UnsupportedOperationException- deprecated: when this implementation doesn't support the operation
-
getNumIdle
public int getNumIdle() throws java.lang.UnsupportedOperationExceptionNot supported in this base implementation.- Specified by:
getNumIdlein interfaceKeyedObjectPool<K,V>- Returns:
- a negative value.
- Throws:
java.lang.UnsupportedOperationException- deprecated: when this implementation doesn't support the operation
-
getNumActive
public int getNumActive() throws java.lang.UnsupportedOperationExceptionNot supported in this base implementation.- Specified by:
getNumActivein interfaceKeyedObjectPool<K,V>- Returns:
- a negative value.
- Throws:
java.lang.UnsupportedOperationException- deprecated: when this implementation doesn't support the operation
-
clear
public void clear() throws java.lang.Exception, java.lang.UnsupportedOperationExceptionNot supported in this base implementation.- Specified by:
clearin interfaceKeyedObjectPool<K,V>- Throws:
java.lang.UnsupportedOperationExceptionjava.lang.Exception
-
clear
public void clear(K key) throws java.lang.Exception, java.lang.UnsupportedOperationException
Not supported in this base implementation.- Specified by:
clearin interfaceKeyedObjectPool<K,V>- Parameters:
key- ignored- Throws:
java.lang.UnsupportedOperationExceptionjava.lang.Exception
-
close
public void close() throws java.lang.ExceptionClose this pool. This affects the behavior ofisClosedandassertOpen.- Specified by:
closein interfaceKeyedObjectPool<K,V>- Throws:
java.lang.Exception
-
setFactory
@Deprecated public void setFactory(KeyedPoolableObjectFactory<K,V> factory) throws java.lang.IllegalStateException, java.lang.UnsupportedOperationException
Deprecated.to be removed in pool 2.0Not supported in this base implementation. Always throws anUnsupportedOperationException, subclasses should override this behavior.- Specified by:
setFactoryin interfaceKeyedObjectPool<K,V>- Parameters:
factory- the new KeyedPoolableObjectFactory- Throws:
java.lang.IllegalStateException- when the factory cannot be set at this timejava.lang.UnsupportedOperationException- when this implementation doesn't support the operation
-
isClosed
protected final boolean isClosed()
Has this pool instance been closed.- Returns:
truewhen this pool has been closed.- Since:
- Pool 1.4
-
assertOpen
protected final void assertOpen() throws java.lang.IllegalStateExceptionThrows anIllegalStateExceptionwhen this pool has been closed.- Throws:
java.lang.IllegalStateException- when this pool has been closed.- Since:
- Pool 1.4
- See Also:
isClosed()
-
-