package org.xbib.logging.ref;

/**
 * A strong reference with an attachment. Since strong references are always reachable, a reaper may not be used.
 *
 * @param <T> the reference value type
 * @param <A> the attachment type
 */
public class StrongReference<T, A> implements Reference<T, A> {

    private volatile T referent;
    private final A attachment;

    /**
     * Construct a new instance.
     *
     * @param referent   the referent
     * @param attachment the attachment
     */
    public StrongReference(final T referent, final A attachment) {
        this.referent = referent;
        this.attachment = attachment;
    }

    /**
     * Construct a new instance.
     *
     * @param referent the referent
     */
    public StrongReference(final T referent) {
        this(referent, null);
    }

    @Override
    public T get() {
        return referent;
    }

    @Override
    public void clear() {
        referent = null;
    }

    @Override
    public A getAttachment() {
        return attachment;
    }

    @Override
    public Type getType() {
        return Type.STRONG;
    }

    @Override
    public String toString() {
        return "strong reference to " + get();
    }
}
