Class Pool<T>

java.lang.Object
arc.util.pooling.Pool<T>

public abstract class Pool<T> extends Object
A pool of objects that can be reused to avoid allocation.
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    Objects implementing this interface will have Pool.Poolable.reset() called when passed to free(Object).
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    final int
    The maximum number of objects that will be pooled.
    int
    The highest number of free objects.
  • Constructor Summary

    Constructors
    Constructor
    Description
    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)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Removes all free objects from this pool.
    void
    free(T object)
    Puts the specified object in the pool, making it eligible to be returned by obtain().
    void
    freeAll(Seq<T> objects)
    Puts the specified objects in the pool.
    int
    The number of objects available to be obtained.
    protected abstract T
     
    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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • max

      public final int max
      The maximum number of objects that will be pooled.
    • peak

      public int peak
      The highest number of free objects. Can be reset any time.
  • Constructor Details

    • 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:
      max - The maximum number of free objects to store in this pool.
  • Method Details

    • newObject

      protected abstract T newObject()
    • obtain

      public T obtain()
      Returns an object from this pool. The object may be new (from newObject()) or reused (previously freed).
    • free

      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 reset but 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.

    • 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 calls Pool.Poolable.reset() if the object is Pool.Poolable.
    • freeAll

      public void freeAll(Seq<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:
    • clear

      public void clear()
      Removes all free objects from this pool.
    • getFree

      public int getFree()
      The number of objects available to be obtained.