Class Pool<T>

  • Type Parameters:
    T - the type of instances to be managed

    public class Pool<T>
    extends java.lang.Object
    Implements a simple pool for explicit memory management of frequently created elements. In order to keep interfaces of instances clean, we delegate creation and clear operation to the IPoolManager delegate.
    Author:
    Holger Eichelberger
    • Constructor Summary

      Constructors 
      Constructor Description
      Pool​(IPoolManager<T> manager)
      Creates a new pool for a given type of instances represented by its pool manager.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private boolean containsByRef​(T instance)
      Returns whether the pool already contains instance.
      T getInstance()
      Returns an instance from the pool or creates a new instance via the manager.
      void releaseInstance​(T instance)
      Releases an instance obtained from getInstance().
      Cave: Do not release instances twice!
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • pool

        private java.util.List<T> pool
    • Constructor Detail

      • Pool

        public Pool​(IPoolManager<T> manager)
        Creates a new pool for a given type of instances represented by its pool manager.
        Parameters:
        manager - the pool manager
    • Method Detail

      • getInstance

        public T getInstance()
        Returns an instance from the pool or creates a new instance via the manager.
        Returns:
        an instance
      • releaseInstance

        public void releaseInstance​(T instance)
        Releases an instance obtained from getInstance().
        Cave: Do not release instances twice!
        Parameters:
        instance - the instance to be released (ignored if null)
      • containsByRef

        private boolean containsByRef​(T instance)
        Returns whether the pool already contains instance.
        Parameters:
        instance - the instance to look for (by reference)
        Returns:
        true if the reference is already there, false else