2010-12-06 03:21:09 +00:00
|
|
|
/* Copyright (c) 2008-2010, Avian Contributors
|
2008-02-19 18:06:52 +00:00
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software
|
|
|
|
for any purpose with or without fee is hereby granted, provided
|
|
|
|
that the above copyright notice and this permission notice appear
|
|
|
|
in all copies.
|
|
|
|
|
|
|
|
There is NO WARRANTY for this software. See license.txt for
|
|
|
|
details. */
|
|
|
|
|
2007-07-21 20:44:39 +00:00
|
|
|
package java.util;
|
|
|
|
|
|
|
|
import java.lang.ref.ReferenceQueue;
|
|
|
|
import java.lang.ref.Reference;
|
|
|
|
import java.lang.ref.WeakReference;
|
|
|
|
|
|
|
|
public class WeakHashMap<K, V> implements Map<K, V> {
|
|
|
|
private final HashMap<K, V> map;
|
|
|
|
private final ReferenceQueue queue;
|
|
|
|
|
|
|
|
public WeakHashMap(int capacity) {
|
2007-08-12 21:01:47 +00:00
|
|
|
map = new HashMap(capacity, new MyHelper());
|
2007-07-21 20:44:39 +00:00
|
|
|
queue = new ReferenceQueue();
|
|
|
|
}
|
|
|
|
|
|
|
|
public WeakHashMap() {
|
|
|
|
this(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void poll() {
|
|
|
|
for (MyCell<K, V> c = (MyCell<K, V>) queue.poll();
|
|
|
|
c != null;
|
|
|
|
c = (MyCell<K, V>) queue.poll())
|
|
|
|
{
|
|
|
|
map.remove(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-26 14:57:34 +00:00
|
|
|
public boolean isEmpty() {
|
|
|
|
return map.isEmpty();
|
|
|
|
}
|
|
|
|
|
2007-07-21 20:44:39 +00:00
|
|
|
public int size() {
|
|
|
|
return map.size();
|
|
|
|
}
|
|
|
|
|
2009-04-17 14:57:49 +00:00
|
|
|
public boolean containsKey(Object key) {
|
2007-07-22 19:06:21 +00:00
|
|
|
poll();
|
|
|
|
return map.containsKey(key);
|
|
|
|
}
|
|
|
|
|
2009-04-17 14:57:49 +00:00
|
|
|
public boolean containsValue(Object value) {
|
2007-07-22 19:06:21 +00:00
|
|
|
poll();
|
|
|
|
return map.containsValue(value);
|
|
|
|
}
|
|
|
|
|
2009-04-22 21:24:26 +00:00
|
|
|
public V get(Object key) {
|
2007-07-21 20:44:39 +00:00
|
|
|
poll();
|
|
|
|
return map.get(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public V put(K key, V value) {
|
|
|
|
poll();
|
|
|
|
return map.put(key, value);
|
|
|
|
}
|
|
|
|
|
2007-09-26 14:57:34 +00:00
|
|
|
public void putAll(Map<? extends K,? extends V> elts) {
|
|
|
|
map.putAll(elts);
|
|
|
|
}
|
|
|
|
|
2009-04-22 21:24:26 +00:00
|
|
|
public V remove(Object key) {
|
2007-07-21 20:44:39 +00:00
|
|
|
poll();
|
|
|
|
return map.remove(key);
|
|
|
|
}
|
|
|
|
|
2007-07-21 22:36:51 +00:00
|
|
|
public void clear() {
|
|
|
|
map.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Set<Entry<K, V>> entrySet() {
|
|
|
|
return map.entrySet();
|
|
|
|
}
|
|
|
|
|
2007-07-22 19:06:21 +00:00
|
|
|
public Set<K> keySet() {
|
|
|
|
return map.keySet();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Collection<V> values() {
|
|
|
|
return map.values();
|
|
|
|
}
|
|
|
|
|
2007-07-21 20:44:39 +00:00
|
|
|
private static class MyCell<K, V>
|
|
|
|
extends WeakReference<K>
|
|
|
|
implements HashMap.Cell<K, V>
|
|
|
|
{
|
|
|
|
public V value;
|
|
|
|
public HashMap.Cell<K, V> next;
|
|
|
|
public int hashCode;
|
|
|
|
|
2009-09-01 23:22:31 +00:00
|
|
|
public MyCell(K key, ReferenceQueue queue, V value,
|
|
|
|
HashMap.Cell<K, V> next, int hashCode)
|
|
|
|
{
|
|
|
|
super(key, queue);
|
2007-07-21 20:44:39 +00:00
|
|
|
this.value = value;
|
|
|
|
this.next = next;
|
2007-08-13 00:50:25 +00:00
|
|
|
this.hashCode = hashCode;
|
2007-07-21 20:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public K getKey() {
|
|
|
|
return get();
|
|
|
|
}
|
|
|
|
|
|
|
|
public V getValue() {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2010-08-15 12:01:33 +00:00
|
|
|
public V setValue(V value) {
|
|
|
|
V old = this.value;
|
2007-07-21 20:44:39 +00:00
|
|
|
this.value = value;
|
2010-08-15 12:01:33 +00:00
|
|
|
return old;
|
2007-07-21 20:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public HashMap.Cell<K, V> next() {
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setNext(HashMap.Cell<K, V> next) {
|
|
|
|
this.next = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int hashCode() {
|
|
|
|
return hashCode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-01 23:22:31 +00:00
|
|
|
private class MyHelper<K, V>
|
2007-08-12 21:01:47 +00:00
|
|
|
extends HashMap.MyHelper<K, V>
|
2007-07-21 20:44:39 +00:00
|
|
|
{
|
|
|
|
public HashMap.Cell<K, V> make(K key, V value, HashMap.Cell<K, V> next) {
|
2009-09-01 23:22:31 +00:00
|
|
|
return new MyCell(key, queue, value, next, hash(key));
|
2007-07-21 20:44:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|