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

org.apfloat.ConcurrentWeakHashMap Maven / Gradle / Ivy

There is a newer version: 1.14.0
Show newest version
package org.apfloat;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.AbstractMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Combination of WeakHashMap and ConcurrentHashMap,
 * providing weak keys and non-blocking access.
 *
 * @since 1.5
 * @version 1.5
 * @author Mikko Tommila
 */

class ConcurrentWeakHashMap
    extends AbstractMap
{
    private static class Key
        extends WeakReference
    {
        private int hashCode;

        public Key(Object key, ReferenceQueue queue)
        {
            super(key, queue);
            this.hashCode = key.hashCode();
        }

        public int hashCode()
        {
            return this.hashCode;
        }

        public boolean equals(Object obj)
        {
            if (this == obj)
            {
                // Always matches even if referenced object has been garbage collected, needed for expunging garbage collected keys
                return true;
            }
            if (obj instanceof Key)
            {
                Key that = (Key) obj;
                Object value = get();
                return (value != null && value.equals(that.get()));
            }
            return false;
        }
    }

    private ConcurrentHashMap map;
    private ReferenceQueue queue;

    public ConcurrentWeakHashMap()
    {
        this.map = new ConcurrentHashMap();
        this.queue = new ReferenceQueue();
    }

    public void clear()
    {
        expungeStaleEntries();
        this.map.clear();
    }

    public Set> entrySet()
    {
        throw new UnsupportedOperationException();
    }

    public V get(Object key)
    {
        // Do not expunge stale entries here to improve performance
        return this.map.get(wrap(key));
    }

    public V put(K key, V value)
    {
        expungeStaleEntries();
        return this.map.put(wrap(key), value);
    }

    public V remove(Object key)
    {
        expungeStaleEntries();
        return this.map.remove(wrap(key));
    }

    public int size()
    {
        expungeStaleEntries();
        return this.map.size();
    }

    private Key wrap(Object key)
    {
        return new Key(key, this.queue);
    }

    private void expungeStaleEntries()
    {
        // Should not cause (much) blocking
        Key key;
        while ((key = (Key) this.queue.poll()) != null)
        {
            this.map.remove(key);
        }
    }
}