1 22 package org.jboss.util.collection; 23 24 import java.lang.ref.ReferenceQueue ; 25 import java.lang.ref.SoftReference ; 26 import java.lang.reflect.Array ; 27 import java.util.Collection ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.Set ; 31 32 41 public class SoftSet implements Set 42 { 43 private HashMap map = new HashMap (); 44 45 private ReferenceQueue gcqueue = new ReferenceQueue (); 46 47 static class ComparableSoftReference extends SoftReference 48 { 49 private Integer key; 50 ComparableSoftReference(Integer key, Object o, ReferenceQueue q) 51 { 52 super(o, q); 53 this.key = key; 54 } 55 Integer getKey() 56 { 57 return key; 58 } 59 } 60 static class ComparableSoftReferenceIterator implements Iterator 61 { 62 Iterator theIter; 63 ComparableSoftReferenceIterator(Iterator theIter) 64 { 65 this.theIter = theIter; 66 } 67 public boolean hasNext() 68 { 69 return theIter.hasNext(); 70 } 71 public Object next() 72 { 73 ComparableSoftReference csr = (ComparableSoftReference) theIter.next(); 74 return csr.get(); 75 } 76 public void remove() 77 { 78 theIter.remove(); 79 } 80 } 81 82 85 public SoftSet() 86 { 87 } 88 89 public int size() 90 { 91 processQueue(); 92 return map.size(); 93 } 94 95 public boolean isEmpty() 96 { 97 processQueue(); 98 return map.isEmpty(); 99 } 100 101 public boolean contains(Object o) 102 { 103 processQueue(); 104 Integer key = new Integer (o.hashCode()); 105 boolean contains = map.containsKey(key); 106 return contains; 107 } 108 109 public Iterator iterator() 110 { 111 processQueue(); 112 Iterator theIter = map.values().iterator(); 113 return new ComparableSoftReferenceIterator(theIter); 114 } 115 116 public Object [] toArray() 117 { 118 processQueue(); 119 return toArray(new Object [0]); 120 } 121 122 public Object [] toArray(Object [] a) 123 { 124 processQueue(); 125 int size = map.size(); 126 Object [] array = {}; 127 if( a.length >= size ) 128 array = a; 129 Iterator iter = map.values().iterator(); 130 int index = 0; 131 while( iter.hasNext() ) 132 { 133 ComparableSoftReference csr = (ComparableSoftReference) iter.next(); 134 Object value = csr.get(); 135 if( array.length == 0 ) 137 { 138 if( value == null ) 139 { 140 index ++; 141 continue; 142 } 143 Array.newInstance(value.getClass(), size); 144 } 145 array[index] = value; 146 index ++; 147 } 148 return array; 149 } 150 151 public boolean add(Object o) 152 { 153 processQueue(); 154 Integer key = new Integer (o.hashCode()); 155 ComparableSoftReference sr = new ComparableSoftReference(key, o, gcqueue); 156 return map.put(key, sr) == null; 157 } 158 159 public boolean remove(Object o) 160 { 161 processQueue(); 162 Integer key = new Integer (o.hashCode()); 163 return map.remove(key) != null; 164 } 165 166 public boolean containsAll(Collection c) 167 { 168 processQueue(); 169 Iterator iter = c.iterator(); 170 boolean contains = true; 171 while( iter.hasNext() ) 172 { 173 Object value = iter.next(); 174 Integer key = new Integer (value.hashCode()); 175 contains &= map.containsKey(key); 176 } 177 return contains; 178 } 179 180 public boolean addAll(Collection c) 181 { 182 processQueue(); 183 Iterator iter = c.iterator(); 184 boolean added = false; 185 while( iter.hasNext() ) 186 { 187 Object value = iter.next(); 188 Integer key = new Integer (value.hashCode()); 189 ComparableSoftReference sr = new ComparableSoftReference(key, value, gcqueue); 190 added |= map.put(key, sr) == null; 191 } 192 return added; 193 } 194 195 public boolean retainAll(Collection c) 196 { 197 Iterator iter = iterator(); 198 boolean removed = false; 199 while( iter.hasNext() ) 200 { 201 Object value = iter.next(); 202 if( c.contains(value) == false ) 203 { 204 iter.remove(); 205 removed = true; 206 } 207 } 208 return removed; 209 } 210 211 public boolean removeAll(Collection c) 212 { 213 processQueue(); 214 Iterator iter = c.iterator(); 215 boolean removed = false; 216 while( iter.hasNext() ) 217 { 218 Object value = iter.next(); 219 removed |= remove(value); 220 } 221 return removed; 222 } 223 224 public void clear() 225 { 226 while( gcqueue.poll() != null ) 227 ; 228 map.clear(); 229 } 230 231 public boolean equals(Object o) 232 { 233 return map.equals(o); 234 } 235 236 public int hashCode() 237 { 238 return map.hashCode(); 239 } 240 241 245 private void processQueue() 246 { 247 ComparableSoftReference cr; 248 while( (cr = (ComparableSoftReference) gcqueue.poll()) != null ) 249 { 250 map.remove(cr.getKey()); 251 } 252 } 253 254 } 255 | Popular Tags |