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

com.koushikdutta.quack.WeakExactHashMap Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
package com.koushikdutta.quack;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Iterator;

public class WeakExactHashMap {
    private HashMap, V> map = new HashMap<>();

    private int purgeThreshold = 0;
    public void setPurgeThreshold(int purgeThreshold) {
        this.purgeThreshold = purgeThreshold;
    }

    private static class Entry {
        WeakReference key;
        int hash;
        public Entry(K key) {
            if (key == null)
                throw new IllegalArgumentException("key can not be null");
            this.key = new WeakReference<>(key);
            hash = key.hashCode();
        }

        @Override
        @SuppressWarnings("unchecked")
        public boolean equals(Object obj) {
            Entry other = (Entry)obj;
            K key = this.key.get();
            return key != null && other.key.get() == key;
        }

        @Override
        public int hashCode() {
            return hash;
        }
    }

    private void maybePurge() {
        if (map.size() < purgeThreshold)
            return;

        purge();
    }

    public void purge() {
        Iterator> iter = map.keySet().iterator();
        while (iter.hasNext()) {
            Entry entry = iter.next();
            if (entry.key.get() == null)
                iter.remove();
        }
        int after = map.size();
//        int purged = before - after;
        setPurgeThreshold(after * 2);
    }

    public V get(K key) {
        maybePurge();
        return map.get(new Entry<>(key));
    }

    public V put(K key, V value) {
        maybePurge();
        return map.put(new Entry<>(key), value);
    }

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy