Class ClearableReference<T>

java.lang.Object
com.seeq.utilities.ClearableReference<T>
Type Parameters:
T - The type of object to be wrapped inside the ClearableReference

public class ClearableReference<T> extends Object
A ClearableReference wraps an object inside another object. When a method parameter is reassigned in a method, it does not affect the original reference from the calling method. This class provides a way for "reassignments" to happen and affect the calling method. In particular, this can be used to pass an object into a method while clearing the calling method's reference to it, which can help plug memory leaks.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a ClearableReference that does not refer to anything yet.
    Create a ClearableReference that refers to the argument.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Clear the reference to the object currently referred to.
    get()
    Get the object this ClearableReference refers to.
    Clear the reference to the object currently referred to and return it.
    void
    set(T value)
    Set the object this ClearableReference refers to.

    Methods inherited from class java.lang.Object

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

    • ClearableReference

      public ClearableReference()
      Create a ClearableReference that does not refer to anything yet.
    • ClearableReference

      public ClearableReference(T value)
      Create a ClearableReference that refers to the argument.
      Parameters:
      value - The object this ClearableReference should refer to.
  • Method Details

    • set

      public void set(T value)
      Set the object this ClearableReference refers to.
      Parameters:
      value - The object this ClearableReference should refer to.
    • get

      public T get()
      Get the object this ClearableReference refers to.
      Returns:
      The object this ClearableReference refers to.
    • clear

      public void clear()
      Clear the reference to the object currently referred to.
    • getAndClear

      public T getAndClear()
      Clear the reference to the object currently referred to and return it.
      Returns:
      The object this ClearableReference will no longer refer to.