Package com.reve.utils.memory
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.
This is a basic Pool Skeleton, where object can be saved for reuse.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintintReturn the count for which instance was supposed to be saved for reuse, but it didn't because ofmaxPoolSizerestriction.Get an object from the pool if available.voidreturnToPool(T object) When an object has been used and dusted, then must be return to pool so that later it can be reused.intsize()Return the current size of this pool.
-
Constructor Details
-
Pool
public Pool()Default constructor. -
Pool
Constructor will set the max pool size toDEFAULT_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 callinggetNewObject()will return null if no free instance is found If this value is not null, thengetNewObject()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 callreturnToPool(Reusable)will not save the instance.
-
Pool
Create a pool with given maximum 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 callinggetNewObject()will return null if no free instance is found If this value is not null, thengetNewObject()will try to create a new instance of this class and return in case no free instance is found.maxPoolSize- Maximum size of the pool, that will can be stored in the memory. If pool size exceeds this limit, then callreturnToPool(Reusable)will not save the instance.
-
-
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
Poolcreated fromgetNewObject()
-
getInstanceNotSavedCount
public int getInstanceNotSavedCount()Return the count for which instance was supposed to be saved for reuse, but it didn't because ofmaxPoolSizerestriction.- Returns:
- the count for which instance was supposed to be saved for reuse, but it didn't because of
maxPoolSizerestriction.
-
returnToPool
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, thenReusable.reset()method of this object will be called before putting it to free list.
-
getNewObject
Get an object from the pool if available.- Returns:
- an object from the pool if available or
objectClassis set properly. Otherwise, null object will be returned.
-