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, orRuntimeException
if none.
- All Implemented Interfaces:
AutoCloseable
Recycler for instances of type T, where instantiating this type may throw checked exception E.
-
Field Summary
FieldsModifier and TypeFieldDescriptionInstances that have been allocated but are unused.Instances that have been allocated. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionacquire()
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
close()
Free all unused instances.void
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
Create a new instance.final void
Recycle an object for reuse by a subsequent call toacquire()
.
-
Field Details
-
usedInstances
Instances that have been allocated. -
unusedInstances
Instances that have been allocated but are unused.
-
-
Constructor Details
-
Recycler
public Recycler()
-
-
Method Details
-
newInstance
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
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.NullPointerException
- ifnewInstance()
returned null.
-
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.- Returns:
- Either a new or a recycled object instance.
- Throws:
E
- If anything goes wrong when trying to allocate a new object instance.
-
recycle
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:
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.
-