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 use an unchecked exception type such asRuntimeException
if none.
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
RecyclerExceptionless
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.
-
-
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 thisRecycler
, by forcibly moving any instances that have been acquired but not yet recycled into the unused instances list, then callingclose()
to close anyAutoCloseable
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 toacquire()
.
-
-
-
Method Detail
-
newInstance
public abstract T newInstance() throws E extends 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 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.IllegalArgumentException
- ifnewInstance()
returned null.E extends Exception
-
acquireRecycleOnClose
public RecycleOnClose<T,E> acquireRecycleOnClose() throws E extends 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.- Throws:
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
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 anyAutoCloseable
instances.- Specified by:
close
in interfaceAutoCloseable
-
forceClose
public void forceClose()
Force-close thisRecycler
, by forcibly moving any instances that have been acquired but not yet recycled into the unused instances list, then callingclose()
to close anyAutoCloseable
instances and discard all instances.
-
-