public abstract class Pool<T>
extends java.lang.Object
Pools
Modifier and Type | Class and Description |
---|---|
static interface |
Pool.Poolable
Objects implementing this interface will have
Pool.Poolable.reset() called when passed to free(Object) . |
Modifier and Type | Field and Description |
---|---|
int |
max
The maximum number of objects that will be pooled.
|
int |
peak
The highest number of free objects.
|
Constructor and Description |
---|
Pool()
Creates a pool with an initial capacity of 16 and no maximum.
|
Pool(int initialCapacity)
Creates a pool with the specified initial capacity and no maximum.
|
Pool(int initialCapacity,
int max) |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Removes and discards all free objects from this pool.
|
protected void |
discard(T object)
Called when an object is discarded.
|
void |
fill(int size)
Adds the specified number of new free objects to the pool.
|
void |
free(T object)
Puts the specified object in the pool, making it eligible to be returned by
obtain() . |
void |
freeAll(Array<T> objects)
Puts the specified objects in the pool.
|
int |
getFree()
The number of objects available to be obtained.
|
protected abstract T |
newObject() |
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.
|
public final int max
public int peak
public Pool()
public Pool(int initialCapacity)
public Pool(int initialCapacity, int max)
initialCapacity
- The initial size of the array supporting the pool. No objects are created/pre-allocated. Use
fill(int)
after instantiation if needed.max
- The maximum number of free objects to store in this pool.protected abstract T newObject()
public T obtain()
newObject()
) or reused (previously
freed
).public void free(T object)
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.
public void fill(int size)
size
- the number of objects to be addedprotected void reset(T object)
Pool.Poolable.reset()
if the object is Pool.Poolable
.protected void discard(T object)
cleared
public void freeAll(Array<T> objects)
The pool does not check if an object is already freed, so the same object must not be freed multiple times.
free(Object)
public void clear()
public int getFree()