Package com.helger.commons.pool
Class ObjectPool<DATATYPE>
- java.lang.Object
-
- com.helger.commons.pool.ObjectPool<DATATYPE>
-
- Type Parameters:
DATATYPE- The type of the objects contained in the pool.
- All Implemented Interfaces:
IHasConditionalLogger,IMutableObjectPool<DATATYPE>
@ThreadSafe public final class ObjectPool<DATATYPE> extends Object implements IMutableObjectPool<DATATYPE>, IHasConditionalLogger
A simple generic object pool with a fixed size determined in the constructor.- Author:
- Philip Helger
-
-
Constructor Summary
Constructors Constructor Description ObjectPool(int nItemCount, IObjectPoolFactory<DATATYPE> aFactory)Create a new object pool for a certain amount of items and a factory that creates the objects on demand.ObjectPool(int nItemCount, Supplier<? extends DATATYPE> aFactory)Create a new object pool for a certain amount of items and a factory that creates the objects on demand.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description DATATYPEborrowObject()Borrow an object from the pool.voidclearUnusedItems()intgetBorrowedObjectCount()intgetPoolSize()static booleanisSilentMode()ESuccessreturnObject(DATATYPE aItem)Return a previously borrowed object back to the pool.static booleansetSilentMode(boolean bSilentMode)Enable or disable certain regular log messages.StringtoString()
-
-
-
Constructor Detail
-
ObjectPool
public ObjectPool(@Nonnegative int nItemCount, @Nonnull Supplier<? extends DATATYPE> aFactory)
Create a new object pool for a certain amount of items and a factory that creates the objects on demand.- Parameters:
nItemCount- The number of items in the pool. Must be ≥ 1.aFactory- The factory to create object. May not benull. The factory may not createnullobjects, as this leads to an error!
-
ObjectPool
public ObjectPool(@Nonnegative int nItemCount, @Nonnull IObjectPoolFactory<DATATYPE> aFactory)
Create a new object pool for a certain amount of items and a factory that creates the objects on demand.- Parameters:
nItemCount- The number of items in the pool. Must be ≥ 1.aFactory- The factory to create object. May not benull. The factory may not createnullobjects, as this leads to an error!
-
-
Method Detail
-
isSilentMode
public static boolean isSilentMode()
- Returns:
trueif logging is disabled,falseif it is enabled.- Since:
- 9.4.0
-
setSilentMode
public static boolean setSilentMode(boolean bSilentMode)
Enable or disable certain regular log messages.- Parameters:
bSilentMode-trueto disable logging,falseto enable logging- Returns:
- The previous value of the silent mode.
- Since:
- 9.4.0
-
getPoolSize
@Nonnegative public int getPoolSize()
- Returns:
- The maximum number of items in the pool. Always > 0.
- Since:
- 11.0.6
- See Also:
getBorrowedObjectCount()
-
getBorrowedObjectCount
@Nonnegative public int getBorrowedObjectCount()
- Returns:
- The number of objects currently borrowed from the pool. Something
between 0 and
getPoolSize() - Since:
- 11.0.6
- See Also:
getPoolSize()
-
clearUnusedItems
public void clearUnusedItems()
-
borrowObject
@Nullable public DATATYPE borrowObject()
Description copied from interface:IMutableObjectPoolBorrow an object from the pool. This method blocks until an object is available.- Specified by:
borrowObjectin interfaceIMutableObjectPool<DATATYPE>- Returns:
- The borrowed object. May be
nulldepending on the factory, and the locking used.
-
returnObject
@Nonnull public ESuccess returnObject(@Nonnull DATATYPE aItem)
Description copied from interface:IMutableObjectPoolReturn a previously borrowed object back to the pool.- Specified by:
returnObjectin interfaceIMutableObjectPool<DATATYPE>- Parameters:
aItem- The previously borrowed object to be returned. Nevernull.- Returns:
ESuccess.SUCCESSupon success
-
-