Class WeakReferenceHolder<T>

java.lang.Object
com.puppycrawl.tools.checkstyle.utils.WeakReferenceHolder<T>
Type Parameters:
T - the type of the referenced object

public final class WeakReferenceHolder<T> extends 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
    Modifier and Type
    Field
    Description
    private WeakReference<T>
    The weak reference to the object.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new WeakReferenceHolder with no initial reference.
  • Method Summary

    Modifier and Type
    Method
    Description
    get()
    Returns the object held by this weak reference, or null if the object has been garbage collected.
    void
    lazyUpdate(T newObject, Runnable afterUpdate)
    Updates the referenced object only if the new object is different from the currently referenced object.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • WeakReferenceHolder

      Constructs a new WeakReferenceHolder with no initial reference.
  • Method Details

    • get

      public T get()
      Returns the object held by this weak reference, or null if the object has been garbage collected.
      Returns:
      the referenced object, or null if it has been collected
    • lazyUpdate

      public void lazyUpdate(T newObject, Runnable afterUpdate)
      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;