Class LinkedPool<E>

java.lang.Object
de.tomatengames.util.pool.LinkedPool<E>
Type Parameters:
E - The type of object managed by this pool.
All Implemented Interfaces:
Pool<E>

public class LinkedPool<E> extends Object implements Pool<E>
The LinkedPool class implements the Pool interface and manages a pool of objects by linking them together. It allows to specify a capacity for the pool and provides cleaning utilities.
Since:
1.8
  • Constructor Summary

    Constructors
    Constructor
    Description
    LinkedPool(Supplier<E> factory)
    Constructs a new LinkedPool with the specified supplier for creating objects and an unlimited capacity.
    LinkedPool(Supplier<E> factory, long capacity)
    Constructs a new LinkedPool with the specified supplier for creating objects and a given capacity.
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Returns the maximum capacity of the pool.
    Claims an instance of Pooled from the pool.
    void
    Cleans up the pool by reducing its size to the minimum size recorded since the last clean operation.
    void
    reduce(long newSize)
    Reduces the size of the pool to a specified new size.
    long
    Returns the amount of stored objects currently in the pool.

    Methods inherited from class Object

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

    • LinkedPool

      public LinkedPool(Supplier<E> factory)
      Constructs a new LinkedPool with the specified supplier for creating objects and an unlimited capacity.
      Parameters:
      factory - A supplier function that provides instances of type E.
    • LinkedPool

      public LinkedPool(Supplier<E> factory, long capacity)
      Constructs a new LinkedPool with the specified supplier for creating objects and a given capacity.
      Parameters:
      factory - A supplier function that provides instances of type E.
      capacity - The maximum number of objects that will be stored in the pool.
  • Method Details

    • capacity

      public long capacity()
      Returns the maximum capacity of the pool. The capacity limits the amount of stored objects in the pool.
      Returns:
      The maximum number of objects that will be stored in the pool.
    • size

      public long size()
      Returns the amount of stored objects currently in the pool.
      Returns:
      The current number of objects in the pool.
    • clean

      public void clean()
      Cleans up the pool by reducing its size to the minimum size recorded since the last clean operation.

      Call this regularly for active cleaning.

    • reduce

      public void reduce(long newSize)
      Reduces the size of the pool to a specified new size. If the given new size is larger than the current amount of stored objects in the pool, nothing happens.
      Parameters:
      newSize - The desired new size of the pool.
      Throws:
      IllegalArgumentException - If the new size is negative.
    • claim

      public Pooled<E> claim()
      Description copied from interface: Pool
      Claims an instance of Pooled from the pool.

      When done with the object, it may be returned to the pool using either the Pooled.free() method or the Pooled.close() method. Do not call both, that would be a double-free.

      It is valid to not return ("steal") a claimed object to the pool. That prevents the pool from providing that object ever again.

      Specified by:
      claim in interface Pool<E>
      Returns:
      A Pooled instance containing an object of type E.