1 21 22 package org.jacorb.notification.util; 23 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 import java.util.Set ; 28 import java.util.WeakHashMap ; 29 30 34 public class WeakHashSet implements Set 35 { 36 private final Map entries_ = new WeakHashMap (); 37 38 private static final Object PRESENT = new Object (); 39 40 45 public int size() 46 { 47 return entries_.size(); 48 } 49 50 55 public boolean isEmpty() 56 { 57 return entries_.isEmpty(); 58 } 59 60 65 public boolean contains(Object o) 66 { 67 return PRESENT == entries_.get(o); 68 } 69 70 75 public Iterator iterator() 76 { 77 return entries_.keySet().iterator(); 78 } 79 80 85 public Object [] toArray() 86 { 87 return entries_.keySet().toArray(); 88 } 89 90 95 public Object [] toArray(Object [] a) 96 { 97 return entries_.keySet().toArray(a); 98 } 99 100 105 public boolean add(Object o) 106 { 107 return entries_.put(o, PRESENT) == null; 108 } 109 110 115 public boolean remove(Object o) 116 { 117 return entries_.remove(o) != null; 118 } 119 120 125 public boolean containsAll(Collection c) 126 { 127 Iterator i = c.iterator(); 128 while (i.hasNext()) 129 { 130 if (!(PRESENT == entries_.get(i.next()))) 131 { 132 return false; 133 } 134 } 135 136 return true; 137 } 138 139 144 public boolean addAll(Collection c) 145 { 146 boolean modified = false; 147 Iterator i = c.iterator(); 148 while (i.hasNext()) 149 { 150 modified |= add(i.next()); 151 } 152 return modified; 153 } 154 155 160 public boolean retainAll(Collection c) 161 { 162 boolean modified = false; 163 Iterator i = entries_.keySet().iterator(); 164 while(i.hasNext()) 165 { 166 if (!c.contains(i.next())) 167 { 168 i.remove(); 169 modified = true; 170 } 171 } 172 return modified; 173 } 174 175 180 public boolean removeAll(Collection c) 181 { 182 boolean modified = false; 183 Iterator i = c.iterator(); 184 while (i.hasNext()) 185 { 186 modified |= remove(i.next()); 187 } 188 return modified; 189 } 190 191 196 public void clear() 197 { 198 entries_.clear(); 199 } 200 } | Popular Tags |