FileDocCategorySizeDatePackage
WeakHashSet.javaAPI DocGlassfish v2 API6883Fri May 04 22:35:20 BST 2007com.sun.jdo.spi.persistence.utility

WeakHashSet

public class WeakHashSet extends HashSet
A weak HashSet. An element stored in the WeakHashSet might be garbage collected, if there is no strong reference to this element.

Fields Summary
ReferenceQueue
queue
Helps to detect garbage collected values.
Constructors Summary
Methods Summary
public booleanadd(java.lang.Object o)
Adds the specified element to this set if it is not already present.

param
o element to be added to this set.
return
true if the set did not already contain the specified element.

        processQueue();
        return super.add(WeakElement.create(o, this.queue));
    
public booleancontains(java.lang.Object o)
Returns true if this set contains the specified element.

param
o element whose presence in this set is to be tested.
return
true if this set contains the specified element.

        return super.contains(WeakElement.create(o));
    
private final java.lang.ObjectgetReferenceObject(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.Iteratoriterator()
Returns an iterator over the elements in this set. The elements are returned in no particular order.

return
an Iterator over the elements in this set.

    
                                    
       
    
        // 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 voidprocessQueue()
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 booleanremove(java.lang.Object o)
Removes the given element from this set if it is present.

param
o object to be removed from this set, if present.
return
true if the set contained the specified element.

        boolean ret = super.remove(WeakElement.create(o));
        processQueue();
        return ret;