org.apache.jdo.util
Class WeakHashSet

java.lang.Object
  extended byjava.util.AbstractCollection
      extended byjava.util.AbstractSet
          extended byjava.util.HashSet
              extended byorg.apache.jdo.util.WeakHashSet
All Implemented Interfaces:
java.lang.Cloneable, java.util.Collection, java.io.Serializable, java.util.Set

public class WeakHashSet
extends java.util.HashSet

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

See Also:
Serialized Form

Nested Class Summary
private static class WeakHashSet.WeakElement
          A WeakHashSet stores objects of class WeakElement.
 
Field Summary
(package private)  java.lang.ref.ReferenceQueue queue
          Helps to detect garbage collected values.
 
Fields inherited from class java.util.HashSet
 
Constructor Summary
WeakHashSet()
           
 
Method Summary
 boolean add(java.lang.Object o)
          Adds the specified element to this set if it is not already present.
 boolean contains(java.lang.Object o)
          Returns true if this set contains the specified element.
private  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.
 java.util.Iterator iterator()
          Returns an iterator over the elements in this set.
private  void processQueue()
          Removes all garbage collected values with their keys from the map.
 boolean remove(java.lang.Object o)
          Removes the given element from this set if it is present.
 
Methods inherited from class java.util.HashSet
clear, clone, isEmpty, size
 
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
 
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
addAll, containsAll, equals, hashCode, removeAll, retainAll, toArray, toArray
 

Field Detail

queue

java.lang.ref.ReferenceQueue queue
Helps to detect garbage collected values.

Constructor Detail

WeakHashSet

public WeakHashSet()
Method Detail

iterator

public java.util.Iterator iterator()
Returns an iterator over the elements in this set. The elements are returned in no particular order.

Returns:
an Iterator over the elements in this set.

contains

public boolean contains(java.lang.Object o)
Returns true if this set contains the specified element.

Parameters:
o - element whose presence in this set is to be tested.
Returns:
true if this set contains the specified element.

add

public boolean add(java.lang.Object o)
Adds the specified element to this set if it is not already present.

Parameters:
o - element to be added to this set.
Returns:
true if the set did not already contain the specified element.

remove

public boolean remove(java.lang.Object o)
Removes the given element from this set if it is present.

Parameters:
o - object to be removed from this set, if present.
Returns:
true if the set contained the specified element.

getReferenceObject

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.


processQueue

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 add() method.