-
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 classObjectPool.Poolable
-
Field Summary
Fields Modifier and Type Field Description private intpoolIdprivate floatreplenishPercentage
-
Method Summary
Modifier and Type Method Description intgetPoolId()Returns the id of the given pool instance. floatgetReplenishPercentage()voidsetReplenishPercentage(float percentage)Set the percentage of the pool to replenish on empty. static synchronized ObjectPoolcreate(int withCapacity, ObjectPool.Poolable object)Returns an ObjectPool instance, of a given starting capacity, that recycles instances of a given Poolable object. synchronized Tget()Returns an instance of Poolable. synchronized voidrecycle(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 voidrecycle(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. intgetPoolCapacity()Returns the capacity of this object pool. intgetPoolCount()Returns the number of objects remaining in the pool, for diagnostic purposes. -
-
Method Detail
-
getPoolId
int getPoolId()
Returns the id of the given pool instance.
-
getReplenishPercentage
float getReplenishPercentage()
-
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.
-
-
-
-