Class Pool<T>

java.lang.Object
arc.util.pooling.Pool<T>
com.xpdustry.claj.common.util.Pool<T>

public class Pool<T> extends arc.util.pooling.Pool<T>
Taken from arc-lite.

A pool of objects that can be reused to avoid allocation. The implementation is thread-safe, lock-less, and pool array is preallocated.

See Also:
  • invalid reference
    Pools
  • Nested Class Summary

    Nested classes/interfaces inherited from class arc.util.pooling.Pool

    arc.util.pooling.Pool.Poolable
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    final int
    The maximum number of objects that can be pooled.
    int
    The highest number of free objects.

    Fields inherited from class arc.util.pooling.Pool

    max
  • Constructor Summary

    Constructors
    Constructor
    Description
    Pool(int capacity, arc.func.Prov<T> newObject)
     
    Pool(arc.func.Prov<T> newObject)
    Creates a pool with a maximum capacity of 2048 objects.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Removes all free objects from this pool.
    void
    Fill the pool with new objects.
    void
    fill(int size)
    Fill the pool with size new objects.
    void
    free(T object)
    Puts the specified object in the pool, making it eligible to be returned by obtain().
    void
    freeAll(arc.struct.Seq<T> objects)
    Puts the specified objects in the pool.
    int
    The number of objects available to be obtained.
    protected T
     
    Returns an object from this pool.
    boolean
    offer(T object)
    Same as
    invalid reference
    #free()
    , but returns true if the object has been placed to the pool array.
    Same as obtain(), but returns null instead of allocating a new object if the pool is empty.
    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 Object

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

    • capacity

      public final int capacity
      The maximum number of objects that can be pooled.
    • peak

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

    • Pool

      public Pool(arc.func.Prov<T> newObject)
      Creates a pool with a maximum capacity of 2048 objects.
    • Pool

      public Pool(int capacity, arc.func.Prov<T> newObject)
      Parameters:
      capacity - The maximum number of free objects to store in this pool.
  • Method Details

    • newObject

      protected T newObject()
      Specified by:
      newObject in class arc.util.pooling.Pool<T>
    • obtain

      public T obtain()
      Returns an object from this pool. The object may be new (from newObject()) or reused (previously freed).
      Overrides:
      obtain in class arc.util.pooling.Pool<T>
    • poll

      public T poll()
      Same as obtain(), but returns null instead of allocating a new object if the pool is empty.
    • 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 Pool.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.

      Overrides:
      free in class arc.util.pooling.Pool<T>
    • offer

      public boolean offer(T object)
      Same as
      invalid reference
      #free()
      , but returns true if the object has been placed to the pool array. Else it means that the pool is full.
    • 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.
      Overrides:
      reset in class arc.util.pooling.Pool<T>
    • freeAll

      public void freeAll(arc.struct.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.

      Overrides:
      freeAll in class arc.util.pooling.Pool<T>
      See Also:
    • fill

      public void fill()
      Fill the pool with new objects.
    • fill

      public void fill(int size)
      Fill the pool with size new objects.
    • clear

      public void clear()
      Removes all free objects from this pool.
      Overrides:
      clear in class arc.util.pooling.Pool<T>
    • getFree

      public int getFree()
      The number of objects available to be obtained. This can be an estimation if pool is highly used.
      Overrides:
      getFree in class arc.util.pooling.Pool<T>