Class WeakReferenceHolder<T>
java.lang.Object
com.puppycrawl.tools.checkstyle.utils.WeakReferenceHolder<T>
- Type Parameters:
T- the type of the referenced object
A wrapper class for
WeakReference that provides a convenient way
to manage weak references to objects.
This class encapsulates the creation and retrieval of weak references, simplifying the common pattern of storing and accessing weakly referenced objects.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a newWeakReferenceHolderwith no initial reference. -
Method Summary
Modifier and TypeMethodDescriptionget()Returns the object held by this weak reference, ornullif the object has been garbage collected.voidlazyUpdate(T newObject, Runnable afterUpdate) Updates the referenced object only if the new object is different from the currently referenced object.
-
Field Details
-
reference
The weak reference to the object.
-
-
Constructor Details
-
WeakReferenceHolder
public WeakReferenceHolder()Constructs a newWeakReferenceHolderwith no initial reference.
-
-
Method Details
-
get
Returns the object held by this weak reference, ornullif the object has been garbage collected.- Returns:
- the referenced object, or
nullif it has been collected
-
lazyUpdate
Updates the referenced object only if the new object is different from the currently referenced object. After updating, runs the specified callback if provided.- Parameters:
newObject- the new object to reference;afterUpdate- a callback to run after updating the reference;
-