Package com.badlogic.gdx.utils
Class Pool<T>
- java.lang.Object
-
- com.badlogic.gdx.utils.Pool<T>
-
- Direct Known Subclasses:
FlushablePool,ParticleEffectPool,ReflectionPool
public abstract class Pool<T> extends java.lang.ObjectA pool of objects that can be reused to avoid allocation.- See Also:
Pools
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfacePool.PoolableObjects implementing this interface will havePool.Poolable.reset()called when passed tofree(Object).
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidclear()Removes and discards all free objects from this pool.protected voiddiscard(T object)Called when an object is discarded.voidfill(int size)Adds the specified number of new free objects to the pool.voidfree(T object)Puts the specified object in the pool, making it eligible to be returned byobtain().voidfreeAll(Array<T> objects)Puts the specified objects in the pool.intgetFree()The number of objects available to be obtained.protected abstract TnewObject()Tobtain()Returns an object from this pool.protected voidreset(T object)Called when an object is freed to clear the state of the object for possible later reuse.
-
-
-
Constructor Detail
-
Pool
public Pool()
Creates a pool with an initial capacity of 16 and no maximum.
-
Pool
public Pool(int initialCapacity)
Creates a pool with the specified initial capacity and no maximum.
-
Pool
public Pool(int initialCapacity, int max)- Parameters:
initialCapacity- The initial size of the array supporting the pool. No objects are created/pre-allocated. Usefill(int)after instantiation if needed.max- The maximum number of free objects to store in this pool.
-
-
Method Detail
-
newObject
protected abstract T newObject()
-
obtain
public T obtain()
Returns an object from this pool. The object may be new (fromnewObject()) or reused (previouslyfreed).
-
free
public void free(T object)
Puts the specified object in the pool, making it eligible to be returned byobtain(). If the pool already containsmaxfree objects, the specified object isdiscarded, it is not reset and not added to the pool.The pool does not check if an object is already freed, so the same object must not be freed multiple times.
-
fill
public void fill(int size)
Adds the specified number of new free objects to the pool. Usually called early on as a pre-allocation mechanism but can be used at any time.- Parameters:
size- the number of objects to be added
-
reset
protected void reset(T object)
Called when an object is freed to clear the state of the object for possible later reuse. The default implementation callsPool.Poolable.reset()if the object isPool.Poolable.
-
discard
protected void discard(T object)
Called when an object is discarded. This is the case when an object is freed, but the maximum capacity of the pool is reached, and when the pool iscleared
-
freeAll
public void freeAll(Array<T> objects)
Puts the specified objects in the pool. Null objects within the array are silently ignored.The pool does not check if an object is already freed, so the same object must not be freed multiple times.
- See Also:
free(Object)
-
clear
public void clear()
Removes and discards all free objects from this pool.
-
getFree
public int getFree()
The number of objects available to be obtained.
-
-