package org.xbib.logging.ref;

import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * A special version of {@link PhantomReference} that is strongly retained until it is reaped by the collection thread.
 */
public class CleanerReference<T, A> extends PhantomReference<T, A> {

    private static final Set<CleanerReference<?, ?>> set = Collections.newSetFromMap(new ConcurrentHashMap<>());

    /**
     * Construct a new instance with a reaper.
     *
     * @param referent   the referent
     * @param attachment the attachment
     * @param reaper     the reaper to use
     */
    public CleanerReference(final T referent, final A attachment, final Reaper<T, A> reaper) {
        super(referent, attachment, reaper);
        set.add(this);
    }

    void clean() {
        set.remove(this);
    }

    @Override
    public final int hashCode() {
        return super.hashCode();
    }

    @Override
    public final boolean equals(final Object obj) {
        return super.equals(obj);
    }
}
