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 Details

    • 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 be null. The factory may not create null objects, 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 be null. The factory may not create null objects, as this leads to an error!
  • Method Details

    • isSilentMode

      public static boolean isSilentMode()
      Returns:
      true if logging is disabled, false if it is enabled.
      Since:
      9.4.0
    • setSilentMode

      public static boolean setSilentMode(boolean bSilentMode)
      Enable or disable certain regular log messages.
      Parameters:
      bSilentMode - true to disable logging, false to 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

      @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:
    • clearUnusedItems

      public void clearUnusedItems()
    • borrowObject

      @Nullable public DATATYPE borrowObject()
      Description copied from interface: IMutableObjectPool
      Borrow an object from the pool. This method blocks until an object is available.
      Specified by:
      borrowObject in interface IMutableObjectPool<DATATYPE>
      Returns:
      The borrowed object. May be null depending on the factory, and the locking used.
    • returnObject

      @Nonnull public ESuccess returnObject(@Nonnull DATATYPE aItem)
      Description copied from interface: IMutableObjectPool
      Return a previously borrowed object back to the pool.
      Specified by:
      returnObject in interface IMutableObjectPool<DATATYPE>
      Parameters:
      aItem - The previously borrowed object to be returned. Never null.
      Returns:
      ESuccess.SUCCESS upon success
    • toString

      public String toString()
      Overrides:
      toString in class Object