1 21 24 package org.lobobrowser.util; 25 26 import java.util.*; 27 import java.lang.ref.*; 28 29 public class WeakValueHashMap implements Map { 30 private final Map map = new HashMap(); 31 private final ReferenceQueue queue = new ReferenceQueue(); 32 33 public WeakValueHashMap() { 34 super(); 35 } 36 37 public int size() { 38 return this.map.size(); 39 } 40 41 public boolean isEmpty() { 42 return this.map.isEmpty(); 43 } 44 45 public boolean containsKey(Object key) { 46 WeakReference wf = (WeakReference) this.map.get(key); 47 return wf != null && wf.get() != null; 48 } 49 50 public boolean containsValue(Object value) { 51 throw new UnsupportedOperationException (); 52 } 53 54 public Object get(Object key) { 55 this.checkQueue(); 56 WeakReference wf = (WeakReference) this.map.get(key); 57 return wf == null ? null : wf.get(); 58 } 59 60 public Object put(Object key, Object value) { 61 if(value == null) { 62 throw new IllegalArgumentException ("null values not accepted"); 63 } 64 this.checkQueue(); 65 Reference ref = new LocalWeakReference(key, value, this.queue); 66 WeakReference oldWf = (WeakReference) this.map.put(key, ref); 67 return oldWf == null ? null : oldWf.get(); 68 } 69 70 public Object remove(Object key) { 71 this.checkQueue(); 72 WeakReference wf = (WeakReference) this.map.remove(key); 73 return wf == null ? null : wf.get(); 74 } 75 76 public void putAll(Map t) { 77 this.checkQueue(); 78 Iterator i = t.entrySet().iterator(); 79 while(i.hasNext()) { 80 Map.Entry entry = (Map.Entry) i.next(); 81 this.put(entry.getKey(), entry.getValue()); 82 } 83 } 84 85 public void clear() { 86 this.checkQueue(); 87 this.map.clear(); 88 } 89 90 public Set keySet() { 91 return this.map.keySet(); 92 } 93 94 private final void checkQueue() { 95 ReferenceQueue queue = this.queue; 96 LocalWeakReference ref; 97 while((ref = (LocalWeakReference) queue.poll()) != null) { 98 this.map.remove(ref.getKey()); 99 } 100 } 101 102 public Collection values() { 103 return new FilteredCollection(this.map.values(), new LocalFilter()); 104 } 105 106 public Set entrySet() { 107 throw new UnsupportedOperationException (); 108 } 109 110 private class LocalFilter implements ObjectFilter { 111 114 public Object decode(Object source) { 115 WeakReference wf = (WeakReference) source; 116 return wf == null ? null : wf.get(); 117 } 118 119 122 public Object encode(Object source) { 123 throw new java.lang.UnsupportedOperationException ("Read-only collection."); 124 } 125 } 126 127 private static class LocalWeakReference extends WeakReference { 128 private final Object key; 129 130 public LocalWeakReference(Object key, Object target, ReferenceQueue queue) { 131 super(target, queue); 132 this.key = key; 133 } 134 135 public Object getKey() { 136 return key; 137 } 138 139 public boolean equals(Object other) { 140 Object target1 = this.get(); 141 Object target2 = other instanceof LocalWeakReference ? ((LocalWeakReference) other).get() : null; 142 return Objects.equals(target1, target2); 143 } 144 145 public int hashCode() { 146 Object target = this.get(); 147 return target == null ? 0 : target.hashCode(); 148 } 149 } 150 } 151 | Popular Tags |