Methods Summary |
---|
public boolean | add(java.lang.Object o)Adds the specified element to this set if it is not already
present.
processQueue();
return super.add(WeakElement.create(o, this.queue));
|
public boolean | contains(java.lang.Object o)Returns true if this set contains the specified element.
return super.contains(WeakElement.create(o));
|
private final java.lang.Object | getReferenceObject(java.lang.ref.WeakReference ref)A convenience method to return the object held by the
weak reference or null if it does not exist.
return ((ref != null) ? ref.get() : null);
|
public java.util.Iterator | iterator()Returns an iterator over the elements in this set. The elements
are returned in no particular order.
// remove garbage collected elements
processQueue();
// get an iterator of the superclass WeakHashSet
final Iterator i = super.iterator();
return new Iterator () {
public boolean hasNext ()
{
return i.hasNext();
}
public Object next ()
{
// unwrap the element
return getReferenceObject((WeakReference)i.next());
}
public void remove ()
{
// remove the element from the HashSet
i.remove();
}
};
|
private final void | processQueue()Removes all garbage collected values with their keys from the map.
Since we don't know how much the ReferenceQueue.poll() operation
costs, we should call it only in the put() method.
WeakElement wv = null;
while ((wv = (WeakElement)this.queue.poll()) != null)
{
super.remove(wv);
}
|
public boolean | remove(java.lang.Object o)Removes the given element from this set if it is present.
boolean ret = super.remove(WeakElement.create(o));
processQueue();
return ret;
|