1 13 14 package oreilly.hcj.references; 15 16 import java.util.AbstractSet ; 17 import java.util.Collection ; 18 import java.util.Iterator ; 19 import java.util.Set ; 20 import java.util.WeakHashMap ; 21 22 59 public class WeakHashSet extends AbstractSet implements Set { 60 61 private static final Object DUMMY = new String ("DUMMY"); 63 64 WeakHashMap backingStore = new WeakHashMap (); 65 66 72 public WeakHashSet() { 73 backingStore = new WeakHashMap (); 74 } 75 76 83 public WeakHashSet(final Collection c) { 84 backingStore = new WeakHashMap (Math.max((int)(c.size() / .75f) + 1, 16)); 85 addAll(c); 86 } 87 88 93 public WeakHashSet(final int initialCapacity, final float loadFactor) { 94 backingStore = new WeakHashMap (initialCapacity, loadFactor); 95 } 96 97 102 public WeakHashSet(final int initialCapacity) { 103 backingStore = new WeakHashMap (initialCapacity); 104 } 105 106 109 public boolean isEmpty() { 110 return backingStore.keySet() 111 .isEmpty(); 112 } 113 114 119 public boolean add(final Object o) { 120 if (o == null) { 121 throw new NullPointerException (); 122 } 123 124 return backingStore.put(o, DUMMY) == null; 125 } 126 127 132 public boolean addAll(final Collection c) { 133 boolean changed = false; 134 Iterator iter = c.iterator(); 135 136 while (iter.hasNext()) { 137 changed = (changed | (backingStore.put(iter.next(), DUMMY) != DUMMY)); 138 } 139 140 return changed; 141 } 142 143 146 public void clear() { 147 backingStore.clear(); 148 } 149 150 153 public boolean contains(final Object o) { 154 return backingStore.containsKey(o); 155 } 156 157 160 public boolean containsAll(final Collection c) { 161 return backingStore.keySet() 162 .containsAll(c); 163 } 164 165 168 public boolean equals(final Object o) { 169 return backingStore.equals(o); 170 } 171 172 185 public int hashCode() { 186 return backingStore.keySet() 187 .hashCode(); 188 } 189 190 209 public Iterator iterator() { 210 return backingStore.keySet() 211 .iterator(); 212 } 213 214 217 public boolean remove(final Object o) { 218 return backingStore.keySet() 219 .remove(o); 220 } 221 222 225 public boolean removeAll(final Collection c) { 226 return backingStore.keySet() 227 .removeAll(c); 228 } 229 230 233 public boolean retainAll(final Collection c) { 234 return backingStore.keySet() 235 .retainAll(c); 236 } 237 238 241 public int size() { 242 return backingStore.keySet() 243 .size(); 244 } 245 246 249 public Object [] toArray() { 250 return backingStore.keySet() 251 .toArray(); 252 } 253 254 257 public Object [] toArray(final Object [] a) { 258 return backingStore.keySet() 259 .toArray(a); 260 } 261 262 265 public String toString() { 266 return backingStore.keySet() 267 .toString(); 268 } 269 270 273 protected Object clone() throws CloneNotSupportedException { 274 throw new CloneNotSupportedException (); 275 } 276 } 277 278 279 | Popular Tags |