Top Description Fields Constructors Methods
nonapi.io.github.classgraph.recycler

public abstract Class Recycler<T, E extends Exception>

extends Object
implements AutoCloseable
Class Inheritance
All Implemented Interfaces
java.lang.AutoCloseable
Type Parameters
<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.
Imports
java.util.ArrayList, .Collections, .Queue, .Set, java.util.concurrent.ConcurrentHashMap, .ConcurrentLinkedQueue

Recycler for instances of type T, where instantiating this type may throw checked exception E.

Field Summary

Modifier and TypeField and Description
private final Queue<T>
unusedInstances

Instances that have been allocated but are unused.

private final Set<T>
usedInstances

Instances that have been allocated.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public T

Returns:

Either a new or a recycled object instance.
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.

public RecycleOnClose<T, E>

Returns:

Either a new or a recycled object instance.
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.

public void
close()

Implements java.lang.AutoCloseable.close.

Free all unused instances.

public void
forceClose()

Force-close this Recycler, by forcibly moving any instances that have been acquired but not yet recycled into the unused instances list, then calling close() to close any AutoCloseable instances and discard all instances.

public abstract T

Returns:

The new instance.
newInstance
()

Create a new instance.

public final void
recycle(final T
the instance to recycle.
instance
)

Recycle an object for reuse by a subsequent call to acquire().

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

unusedInstancesback to summary
private final Queue<T> unusedInstances

Instances that have been allocated but are unused.

usedInstancesback to summary
private final Set<T> usedInstances

Instances that have been allocated.

Constructor Detail

Recyclerback to summary
public Recycler()

Method Detail

acquireback to summary
public T acquire() throws SingletonMap:E

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:T

Either a new or a recycled object instance.

Exceptions
SingletonMap:E:
if newInstance() threw an exception of type E.
NullPointerException:
if newInstance() returned null.
acquireRecycleOnCloseback to summary
public RecycleOnClose<T, E> acquireRecycleOnClose() throws SingletonMap:E

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:RecycleOnClose<T, E>

Either a new or a recycled object instance.

Exceptions
SingletonMap:E:
If anything goes wrong when trying to allocate a new object instance.
closeback to summary
public void close()

Implements java.lang.AutoCloseable.close.

Free all unused instances. Calls 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.

Annotations
@Override
forceCloseback to summary
public void forceClose()

Force-close this Recycler, by forcibly moving any instances that have been acquired but not yet recycled into the unused instances list, then calling close() to close any AutoCloseable instances and discard all instances.

newInstanceback to summary
public abstract T newInstance() throws SingletonMap:E

Create a new instance. This should either return a non-null instance of type T, or throw an exception of type E.

Returns:T

The new instance.

Exceptions
SingletonMap:E:
If an exception of type E was thrown during instantiation.
recycleback to summary
public final void recycle(final T instance)

Recycle an object for reuse by a subsequent call to acquire(). If the object is an instance of Resettable, then Resettable#reset() will be called on the instance before recycling it.

Parameters
instance:T

the instance to recycle.

Exceptions
IllegalArgumentException:
if the object instance was not originally obtained from this Recycler.