All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.seeq.utilities.ClearableReference Maven / Gradle / Ivy

The newest version!
package com.seeq.utilities;

/**
 * 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.
 *
 * @param 
 *         The type of object to be wrapped inside the ClearableReference
 */
public class ClearableReference {
    private T value;

    /**
     * Create a ClearableReference that does not refer to anything yet.
     */
    public ClearableReference() {
        this.value = null;
    }

    /**
     * Create a ClearableReference that refers to the argument.
     *
     * @param value
     *         The object this ClearableReference should refer to.
     */
    public ClearableReference(T value) {
        this.value = value;
    }

    /**
     * Set the object this ClearableReference refers to.
     *
     * @param value
     *         The object this ClearableReference should refer to.
     */
    public void set(T value) {
        this.value = value;
    }

    /**
     * Get the object this ClearableReference refers to.
     *
     * @return The object this ClearableReference refers to.
     */
    public T get() {
        return this.value;
    }

    /**
     * Clear the reference to the object currently referred to.
     */
    public void clear() {
        this.value = null;
    }

    /**
     * Clear the reference to the object currently referred to and return it.
     *
     * @return The object this ClearableReference will no longer refer to.
     */
    public T getAndClear() {
        T temporary = this.value;
        this.value = null;
        return temporary;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy