Class Pool<T extends Reusable>

java.lang.Object
com.reve.utils.memory.Pool<T>
Type Parameters:
T - is the type of the object this pool will contain. One important thing to understand that this pool will not create any instance of the object. Rather it will only save them and return them when requested.

public class Pool<T extends Reusable> extends Object
This is a basic Pool Skeleton, where object can be saved for reuse.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Default constructor.
    Pool(int maxPoolSize)
    Constructor with maximum pool size.
    Pool(Class<T> objectClass)
    Constructor will set the max pool size to DEFAULT_MAX_POOL_SIZE
    Pool(Class<T> objectClass, int maxPoolSize)
    Create a pool with given maximum pool size.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
     
    int
    Return the count for which instance was supposed to be saved for reuse, but it didn't because of maxPoolSize restriction.
    Get an object from the pool if available.
    void
    returnToPool(T object)
    When an object has been used and dusted, then must be return to pool so that later it can be reused.
    int
    Return the current size of this pool.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Pool

      public Pool()
      Default constructor.
    • Pool

      public Pool(Class<T> objectClass)
      Constructor will set the max pool size to DEFAULT_MAX_POOL_SIZE
      Parameters:
      objectClass - This value is mainly used to create a new instance if no free instance is found. this value can be null, then calling getNewObject() will return null if no free instance is found If this value is not null, then getNewObject() will try to create a new instance of this class and return in case no free instance is found.
    • Pool

      public Pool(int maxPoolSize)
      Constructor with maximum pool size.
      Parameters:
      maxPoolSize - Maximum size of the pool, that will can be stored in the memory. If pool size exceeds this limit, then call returnToPool(Reusable) will not save the instance.
    • Pool

      public Pool(Class<T> objectClass, int maxPoolSize)
      Create a pool with given maximum pool size.
      Parameters:
      maxPoolSize - Maximum size of the pool, that will can be stored in the memory. If pool size exceeds this limit, then call returnToPool(Reusable) will not save the instance.
      objectClass - This value is mainly used to create a new instance if no free instance is found. this value can be null, then calling getNewObject() will return null if no free instance is found If this value is not null, then getNewObject() will try to create a new instance of this class and return in case no free instance is found.
  • Method Details

    • size

      public int size()
      Return the current size of this pool.
      Returns:
      the current size of this pool.
    • getInstanceCreated

      public int getInstanceCreated()
      Returns:
      the total number of instance of type Pool created from getNewObject()
    • getInstanceNotSavedCount

      public int getInstanceNotSavedCount()
      Return the count for which instance was supposed to be saved for reuse, but it didn't because of maxPoolSize restriction.
      Returns:
      the count for which instance was supposed to be saved for reuse, but it didn't because of maxPoolSize restriction.
    • returnToPool

      public void returnToPool(T object)
      When an object has been used and dusted, then must be return to pool so that later it can be reused.
      Parameters:
      object - Object to be returned to the pool. If this object is not null, then Reusable.reset() method of this object will be called before putting it to free list.
    • getNewObject

      public T getNewObject()
      Get an object from the pool if available.
      Returns:
      an object from the pool if available or objectClass is set properly. Otherwise, null object will be returned.