Package 

Class Pool


  • 
    public class Pool<T>
    
                        

    Base class for thread-safe pools of recycleable objects.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      public interface Pool.Factory

      Used to create new instances of objects when needed.

    • Constructor Summary

      Constructors 
      Constructor Description
      Pool(int maxPoolSize, Pool.Factory<T> factory) Creates a new pool with the given pool size and factory.
    • Method Summary

      Modifier and Type Method Description
      boolean isEmpty() Whether the pool is empty.
      T get() Returns a new item, from the recycled pool if possible (if there are recycled items),or instantiating one through the factory (if we can respect the pool size).If these conditions are not met, this returns null.
      void recycle(@NonNull() T item) Recycles an item after it has been used.
      void clear() Clears the pool of recycled items.
      final int count() Returns the count of all items managed by this pool.
      final int activeCount() Returns the active items managed by this pools, which means, itemscurrently being used.
      final int recycledCount() Returns the recycled items managed by this pool, which means, itemsthat were used and later recycled, and are currently available forsecond use.
      String toString()
      • Methods inherited from class java.lang.Object

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

      • Pool

        Pool(int maxPoolSize, Pool.Factory<T> factory)
        Creates a new pool with the given pool size and factory.
        Parameters:
        maxPoolSize - the max pool size
        factory - the factory
    • Method Detail

      • isEmpty

         boolean isEmpty()

        Whether the pool is empty. This means that get will returna null item, because all objects were reclaimed and not recycled yet.

      • get

        @Nullable() T get()

        Returns a new item, from the recycled pool if possible (if there are recycled items),or instantiating one through the factory (if we can respect the pool size).If these conditions are not met, this returns null.

      • recycle

         void recycle(@NonNull() T item)

        Recycles an item after it has been used. The item should come from a previous get call.

        Parameters:
        item - used item
      • count

         final int count()

        Returns the count of all items managed by this pool. Includes- active items: currently being used- recycled items: used and recycled, available for second use

      • activeCount

         final int activeCount()

        Returns the active items managed by this pools, which means, itemscurrently being used.

      • recycledCount

         final int recycledCount()

        Returns the recycled items managed by this pool, which means, itemsthat were used and later recycled, and are currently available forsecond use.