Class Recycler<T,E extends Exception>

java.lang.Object
nonapi.io.github.classgraph.recycler.Recycler<T,E>
Type Parameters:
T - The type to recycle.
E - An exception that can be thrown while acquiring an instance of the type to recycle, or RuntimeException if none.
All Implemented Interfaces:
AutoCloseable

public abstract class Recycler<T,E extends Exception> extends Object implements AutoCloseable
Recycler for instances of type T, where instantiating this type may throw checked exception E.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final Queue<T>
    Instances that have been allocated but are unused.
    private final Set<T>
    Instances that have been allocated.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    Acquire on object instance of type T, either by reusing a previously recycled instance if possible, or if there are no currently-unused instances, by allocating a new instance.
    Acquire a Recyclable wrapper around an object instance, which can be used to recycle object instances at the end of a try-with-resources block.
    void
    Free all unused instances.
    void
    Force-close this Recycler, by forcibly moving any instances that have been acquired but not yet recycled into the unused instances list, then calling close() to close any AutoCloseable instances and discard all instances.
    abstract T
    Create a new instance.
    final void
    recycle(T instance)
    Recycle an object for reuse by a subsequent call to acquire().

    Methods inherited from class java.lang.Object

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

    • usedInstances

      private final Set<T> usedInstances
      Instances that have been allocated.
    • unusedInstances

      private final Queue<T> unusedInstances
      Instances that have been allocated but are unused.
  • Constructor Details

    • Recycler

      public Recycler()
  • Method Details

    • newInstance

      public abstract T newInstance() throws E
      Create a new instance. This should either return a non-null instance of type T, or throw an exception of type E.
      Returns:
      The new instance.
      Throws:
      E - If an exception of type E was thrown during instantiation.
    • acquire

      public T acquire() throws E
      Acquire on object instance of type T, either by reusing a previously recycled instance if possible, or if there are no currently-unused instances, by allocating a new instance.
      Returns:
      Either a new or a recycled object instance.
      Throws:
      E - if newInstance() threw an exception of type E.
      NullPointerException - if newInstance() returned null.
    • acquireRecycleOnClose

      public RecycleOnClose<T,E> acquireRecycleOnClose() throws E
      Acquire a Recyclable wrapper around an object instance, which can be used to recycle object instances at the end of a try-with-resources block.
      Returns:
      Either a new or a recycled object instance.
      Throws:
      E - If anything goes wrong when trying to allocate a new object instance.
    • recycle

      public final void recycle(T instance)
      Recycle an object for reuse by a subsequent call to acquire(). If the object is an instance of Resettable, then Resettable.reset() will be called on the instance before recycling it.
      Parameters:
      instance - the instance to recycle.
      Throws:
      IllegalArgumentException - if the object instance was not originally obtained from this Recycler.
    • close

      public void close()
      Free all unused instances. Calls AutoCloseable.close() on any unused instances that implement AutoCloseable.

      The Recycler may continue to be used to acquire new instances after calling this close method, and then this close method may be called again in future, i.e. the effect of calling this method is to simply clear out the recycler of unused instances, closing any AutoCloseable instances.

      Specified by:
      close in interface AutoCloseable
    • forceClose

      public void forceClose()
      Force-close this Recycler, by forcibly moving any instances that have been acquired but not yet recycled into the unused instances list, then calling close() to close any AutoCloseable instances and discard all instances.