T - The type to recycle.E - An exception that can be thrown while acquiring an instance of the type to recycle, or
RuntimeException if none.public abstract class Recycler<T,E extends java.lang.Exception>
extends java.lang.Object
implements java.lang.AutoCloseable
| Constructor and Description |
|---|
Recycler() |
| Modifier and Type | Method and 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()
|
abstract T |
newInstance()
Create a new instance.
|
void |
recycle(T instance)
Recycle an object for reuse by a subsequent call to
acquire(). |
public abstract T newInstance() throws E extends java.lang.Exception
E - If an exception of type E was thrown during instantiation.E extends java.lang.Exceptionpublic T acquire() throws E extends java.lang.Exception
E - if newInstance() threw an exception of type E.java.lang.NullPointerException - if newInstance() returned null.E extends java.lang.Exceptionpublic RecycleOnClose<T,E> acquireRecycleOnClose() throws E extends java.lang.Exception
E - If anything goes wrong when trying to allocate a new object instance.E extends java.lang.Exceptionpublic final void recycle(T instance)
acquire(). If the object is an instance of
Resettable, then Resettable.reset() will be called on the instance before recycling it.instance - the instance to recycle.java.lang.IllegalArgumentException - if the object instance was not originally obtained from this Recycler.public void close()
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.
close in interface java.lang.AutoCloseablepublic void forceClose()