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

  • Type Parameters:
    T - the type to recycle
    E - the exception type that may be thrown when a recyclable item is acquired.
    All Implemented Interfaces:
    java.lang.AutoCloseable

    public class RecycleOnClose<T,​E extends java.lang.Exception>
    extends java.lang.Object
    implements java.lang.AutoCloseable
    An AutoCloseable wrapper for a recyclable object instance. Obtained by calling Recycler.acquireRecycleOnClose() in a try-with-resources statement, so that when the try block exits, the acquired instance is recycled.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private T instance
      The instance.
      private Recycler<T,​E> recycler
      The recycler.
    • Constructor Summary

      Constructors 
      Constructor Description
      RecycleOnClose​(Recycler<T,​E> recycler, T instance)
      Acquire or allocate an instance.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Recycle an instance.
      T get()
      Get the object instance.
      • Methods inherited from class java.lang.Object

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

      • recycler

        private final Recycler<T,​E extends java.lang.Exception> recycler
        The recycler.
      • instance

        private final T instance
        The instance.
    • Constructor Detail

      • RecycleOnClose

        RecycleOnClose​(Recycler<T,​E> recycler,
                       T instance)
        Acquire or allocate an instance.
        Parameters:
        recycler - The Recycler.
        instance - An object instance that was obtained by calling Recycler.acquire() on the recycler.
        Throws:
        java.lang.IllegalArgumentException - If Recycler.newInstance() returned null.
    • Method Detail

      • get

        public T get()
        Get the object instance.
        Returns:
        The object instance.
      • close

        public void close()
        Recycle an instance. Calls Resettable.reset() if the instance implements Resettable.
        Specified by:
        close in interface java.lang.AutoCloseable