1 2 12 package com.versant.core.util; 13 14 import java.util.HashSet ; 15 import java.util.List ; 16 import java.util.ArrayList ; 17 import java.util.Iterator ; 18 import java.lang.ref.ReferenceQueue ; 19 import java.lang.ref.WeakReference ; 20 import java.lang.ref.Reference ; 21 22 27 public final class WeakBag { 28 29 private HashSet set = new HashSet (); 30 private ReferenceQueue refQueue = new ReferenceQueue (); 31 32 public WeakBag() { 33 } 34 35 39 public WeakReference add(Object o) { 40 WeakReference ans = new WeakReference (o, refQueue); 41 set.add(ans); 42 return ans; 43 } 44 45 48 public void remove(Reference o) { 49 set.remove(o); 50 } 51 52 55 public int size() { 56 return set.size(); 57 } 58 59 62 public List values() { 63 ArrayList a = new ArrayList (set.size()); 64 for (Iterator i = set.iterator(); i.hasNext(); ) { 65 WeakReference r = (WeakReference )i.next(); 66 Object o = r.get(); 67 if (o != null) a.add(o); 68 } 69 return a; 70 } 71 72 76 public void clean() { 77 for (;;) { 78 Object r = refQueue.poll(); 79 if (r == null) return; 80 set.remove(r); 81 } 82 } 83 84 } 85 | Popular Tags |