APIdia Launch Partner
Top Description Inners Fields Constructors Methods
com.badlogic.gdx.utils

public abstract Class Pool<T>

extends Object
Class Inheritance
Known Direct Subclasses
com.badlogic.gdx.utils.ReflectionPool, com.badlogic.gdx.utils.SortedIntList.NodePool, com.badlogic.gdx.utils.FlushablePool, com.badlogic.gdx.graphics.g2d.ParticleEffectPool, com.badlogic.gdx.graphics.g3d.particles.batches.BillboardParticleBatch.RenderablePool, com.badlogic.gdx.graphics.g3d.particles.influencers.ModelInfluencer.Random.ModelInstancePool, com.badlogic.gdx.graphics.g3d.particles.influencers.ParticleControllerInfluencer.Random.ParticleControllerPool, com.badlogic.gdx.utils.DefaultPool

A pool of objects that can be reused to avoid allocation.
Author
Nathan Sweet
See Also
PoolManager

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
Pool.Poolable

Objects implementing this interface will have reset() called when passed to Pool#free(Object).

Field Summary

Modifier and TypeField and Description
private final Array<T>
public final int
max

The maximum number of objects that will be pooled.

public int
peak

The highest number of free objects.

Constructor Summary

AccessConstructor and Description
public
Pool()

Creates a pool with an initial capacity of 16 and no maximum.

public
Pool(int initialCapacity)

Creates a pool with the specified initial capacity and no maximum.

public
Pool(int
The initial size of the array supporting the pool. No objects are created/pre-allocated. Use fill(int) after instantiation if needed.
initialCapacity
,
int
The maximum number of free objects to store in this pool.
max
)

Method Summary

Modifier and TypeMethod and Description
public void
clear()

Removes and discards all free objects from this pool.

protected void
discard(T object)

Called when an object is discarded.

public void
fill(int
the number of objects to be added
size
)

Adds the specified number of new free objects to the pool.

public void
free(T object)

Puts the specified object in the pool, making it eligible to be returned by obtain().

public void
freeAll(Array<T> objects)

Puts the specified objects in the pool.

public int
getFree()

The number of objects available to be obtained.

protected abstract T
public T
obtain()

Returns an object from this pool.

protected void
reset(T object)

Called when an object is freed to clear the state of the object for possible later reuse.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

freeObjectsback to summary
private final Array<T> freeObjects
maxback to summary
public final int max

The maximum number of objects that will be pooled.

peakback to summary
public int peak

The highest number of free objects. Can be reset any time.

Constructor Detail

Poolback to summary
public Pool()

Creates a pool with an initial capacity of 16 and no maximum.

Poolback to summary
public Pool(int initialCapacity)

Creates a pool with the specified initial capacity and no maximum.

Poolback to summary
public Pool(int initialCapacity, int max)
Parameters
initialCapacity:int

The initial size of the array supporting the pool. No objects are created/pre-allocated. Use fill(int) after instantiation if needed.

max:int

The maximum number of free objects to store in this pool.

Method Detail

clearback to summary
public void clear()

Removes and discards all free objects from this pool.

discardback to summary
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 is cleared

fillback to summary
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:int

the number of objects to be added

freeback to summary
public void free(T object)

Puts the specified object in the pool, making it eligible to be returned by obtain(). If the pool already contains max free objects, the specified object is discarded, 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.

freeAllback to summary
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)
getFreeback to summary
public int getFree()

The number of objects available to be obtained.

newObjectback to summary
protected abstract T newObject()
obtainback to summary
public T obtain()

Returns an object from this pool. The object may be new (from newObject()) or reused (previously freed).

resetback to summary
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 calls Poolable#reset() if the object is Poolable.

com.badlogic.gdx.utils back to summary

public Interface Pool.Poolable

Known Direct Implementers
com.badlogic.gdx.utils.QuadTreeFloat, com.badlogic.gdx.Net.HttpRequest, com.badlogic.gdx.graphics.g2d.GlyphLayout, com.badlogic.gdx.graphics.g2d.GlyphLayout.GlyphRun, com.badlogic.gdx.graphics.g3d.utils.BaseAnimationController.Transform, com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder.VertexInfo, com.badlogic.gdx.math.FloatCounter, com.badlogic.gdx.scenes.scene2d.Action, com.badlogic.gdx.scenes.scene2d.Event, com.badlogic.gdx.scenes.scene2d.Stage.TouchFocus, com.badlogic.gdx.scenes.scene2d.ui.Cell

Objects implementing this interface will have reset() called when passed to Pool#free(Object).

Method Summary

Modifier and TypeMethod and Description
public void
reset()

Resets the object for reuse.

Method Detail

resetback to summary
public void reset()

Resets the object for reuse. Object references should be nulled and fields may be set to default values.