/*
 * Copyright (c) 2004-2017, by Alex Leigh.
 * All rights reserved.
 *
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE
 * The copyright notice above does not evidence any
 * actual or intended publication of such source code.
 */

package lc.mecha.lang;

import java.lang.ref.WeakReference;
import java.util.AbstractMap;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;

/**
 * This class decorates the <code>WeakHashMap</code> class to wrap the values in
 * <code>WeakReference</code> objects. This results in a map where the values may
 * garbage collect.
 *
 * @param <K>
 * @param <V>
 */
public class WeakerHashMap<K, V> extends AbstractMap<K, V> implements Map<K, V> {
    private static final WeakHashMap map = new WeakHashMap();

    @Override
    public Set entrySet() {
        throw new RuntimeException("Not implemented.");
    }

    @Override
    public V put(final K key, final V value) {
        final WeakReference<V> ptr = (WeakReference<V>) map.put(key, new WeakReference<>(value));
        return ptr == null ? null : ptr.get();
    }

    public String toString() {
        return "WeakerHashMap [" + this.hashCode() + "]";
    }
}
