Package 

Class ObjectPool


  • 
    public class ObjectPool<T extends ObjectPool.Poolable>
    
                        

    An object pool for recycling of object instances extending Poolable. Cost/Benefit : Cost - The pool can only contain objects extending Poolable. Benefit - The pool can very quickly determine if an object is elligable for storage without iteration. Benefit - The pool can also know if an instance of Poolable is already stored in a different pool instance. Benefit - The pool can grow as needed, if it is empty Cost - However, refilling the pool when it is empty might incur a time cost with sufficiently large capacity. Set the replenishPercentage to a lower number if this is a concern. Created by Tony Patino on 6/20/16.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      public abstract class ObjectPool.Poolable
    • Method Summary

      Modifier and Type Method Description
      int getPoolId() Returns the id of the given pool instance.
      float getReplenishPercentage()
      void setReplenishPercentage(float percentage) Set the percentage of the pool to replenish on empty.
      static synchronized ObjectPool create(int withCapacity, ObjectPool.Poolable object) Returns an ObjectPool instance, of a given starting capacity, that recycles instances of a given Poolable object.
      synchronized T get() Returns an instance of Poolable.
      synchronized void recycle(T object) Recycle an instance of Poolable that this pool is capable of generating.The T instance passed must not already exist inside this or any other ObjectPool instance.
      synchronized void recycle(List<T> objects) Recycle a List of Poolables that this pool is capable of generating.The T instances passed must not already exist inside this or any other ObjectPool instance.
      int getPoolCapacity() Returns the capacity of this object pool.
      int getPoolCount() Returns the number of objects remaining in the pool, for diagnostic purposes.
      • Methods inherited from class java.lang.Object

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

      • getPoolId

         int getPoolId()

        Returns the id of the given pool instance.

      • setReplenishPercentage

         void setReplenishPercentage(float percentage)

        Set the percentage of the pool to replenish on empty. Valid values are between0.00f and 1.00f

        Parameters:
        percentage - a value between 0 and 1, representing the percentage of the pool to replenish.
      • create

         static synchronized ObjectPool create(int withCapacity, ObjectPool.Poolable object)

        Returns an ObjectPool instance, of a given starting capacity, that recycles instances of a given Poolable object.

        Parameters:
        withCapacity - A positive integer value.
        object - An instance of the object that the pool should recycle.
      • get

         synchronized T get()

        Returns an instance of Poolable. If get() is called with an empty pool, the pool will bereplenished. If the pool capacity is sufficiently large, this could come at a performancecost.

      • recycle

         synchronized void recycle(T object)

        Recycle an instance of Poolable that this pool is capable of generating.The T instance passed must not already exist inside this or any other ObjectPool instance.

        Parameters:
        object - An object of type T to recycle
      • recycle

         synchronized void recycle(List<T> objects)

        Recycle a List of Poolables that this pool is capable of generating.The T instances passed must not already exist inside this or any other ObjectPool instance.

        Parameters:
        objects - A list of objects of type T to recycle
      • getPoolCapacity

         int getPoolCapacity()

        Returns the capacity of this object pool. Note : The pool will automatically resizeto contain additional objects if the user tries to add more objects than the pool'scapacity allows, but this comes at a performance cost.

      • getPoolCount

         int getPoolCount()

        Returns the number of objects remaining in the pool, for diagnostic purposes.