Class Recycler<T,​E extends java.lang.Exception>

  • Type Parameters:
    T - The type to recycle.
    E - An exception that can be thrown while acquiring an instance of the type to recycle, or use an unchecked exception type such as RuntimeException if none.
    All Implemented Interfaces:
    java.lang.AutoCloseable
    Direct Known Subclasses:
    RecyclerExceptionless

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

      Constructors 
      Constructor Description
      Recycler()  
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      T acquire()
      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.
      RecycleOnClose<T,​E> acquireRecycleOnClose()
      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 close()
      Free all unused instances.
      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.
      abstract T newInstance()
      Create a new instance.
      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
    • Constructor Detail

      • Recycler

        public Recycler()
    • Method Detail

      • newInstance

        public abstract T newInstance()
                               throws E extends java.lang.Exception
        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.
        E extends java.lang.Exception
      • acquire

        public T acquire()
                  throws E extends java.lang.Exception
        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.
        java.lang.IllegalArgumentException - if newInstance() returned null.
        E extends java.lang.Exception
      • acquireRecycleOnClose

        public RecycleOnClose<T,​E> acquireRecycleOnClose()
                                                        throws E extends java.lang.Exception
        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.
        E extends java.lang.Exception
      • 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.
        Throws:
        java.lang.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 java.lang.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.