Class Recycler<T,E extends java.lang.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, orRuntimeExceptionif none.
- All Implemented Interfaces:
java.lang.AutoCloseable
public abstract class Recycler<T,E extends java.lang.Exception> extends java.lang.Object implements java.lang.AutoCloseableRecycler for instances of type T, where instantiating this type may throw checked exception E.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Queue<T>unusedInstancesInstances that have been allocated but are unused.private java.util.Set<T>usedInstancesInstances that have been allocated.
-
Constructor Summary
Constructors Constructor Description Recycler()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Tacquire()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.voidclose()Free all unused instances.voidforceClose()abstract TnewInstance()Create a new instance.voidrecycle(T instance)Recycle an object for reuse by a subsequent call toacquire().
-
-
-
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.
-
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- ifnewInstance()threw an exception of type E.java.lang.NullPointerException- ifnewInstance()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.
-
recycle
public final void recycle(T instance)
Recycle an object for reuse by a subsequent call toacquire(). If the object is an instance ofResettable, thenResettable.reset()will be called on the instance before recycling it.- Parameters:
instance- the instance to recycle.- Throws:
java.lang.IllegalArgumentException- if the object instance was not originally obtained from thisRecycler.
-
close
public void close()
Free all unused instances. CallsAutoCloseable.close()on any unused instances that implementAutoCloseable.The
Recyclermay 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 anyAutoCloseableinstances.- Specified by:
closein interfacejava.lang.AutoCloseable
-
forceClose
public void forceClose()
-
-