org.jboss.util.collection
public class: WeakSet [javadoc |
source]
java.lang.Object
java.util.AbstractCollection
java.util.AbstractSet
org.jboss.util.collection.WeakSet
All Implemented Interfaces:
Set, Collection
A
Set implementation with
weak elements. An entry in
a
WeakSet will automatically be removed when the element is no
longer in ordinary use. More precisely, the presence of an given element
will not prevent the element from being discarded by the garbage collector,
that is, made finalizable, finalized, and then reclaimed.
- version:
< - tt>$Revision: 1.1 $
- author:
< - a href="mailto:jason@planet57.com">Jason Dillon
| Field Summary |
|---|
| protected final ReferenceQueue | queue | The reference queue used to get object removal notifications. |
| protected final Set | set | The Set which will be used for element storage. |
| Constructor: |
public WeakSet() {
this(new HashSet());
}
Construct a WeakSet based on a HashSet. |
public WeakSet(Set set) {
if (set == null)
throw new NullArgumentException("set");
// reset any elements to weak objects
if (set.size() != 0) {
Object elements[] = set.toArray();
set.clear();
for (int i=0; i< elements.length; i++) {
add(elements[i]);
}
}
this.set = set;
}
Construct a WeakSet. Any elements in the given set will be
wrapped in WeakObject references. Parameters:
set - The Set which will be used for element storage.
Throws:
NullArgumentException - Set is null.
|
| Methods from java.util.AbstractCollection: |
|---|
|
add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray, toString |
| Method from org.jboss.util.collection.WeakSet Detail: |
public boolean add(Object obj) {
maintain();
return set.add(WeakObject.create(obj, queue));
}
Add an element to the set. |
public void clear() {
set.clear();
}
Removes all of the elements from this set. |
public Object clone() {
maintain();
try {
return super.clone();
}
catch (CloneNotSupportedException e) {
throw new InternalError();
}
}
Returns a shallow copy of this WeakSet instance: the elements
themselves are not cloned. |
public boolean contains(Object obj) {
maintain();
return set.contains(WeakObject.create(obj));
}
Returns true if this set contains the specified element. |
public boolean isEmpty() {
maintain();
return set.isEmpty();
}
Returns true if this set contains no elements. |
public Iterator iterator() {
return new Iterator() {
/** The set's iterator */
Iterator iter = set.iterator();
/** The next available object. */
Object next = null;
public boolean hasNext() {
while (iter.hasNext()) {
WeakObject weak = (WeakObject)iter.next();
Object obj = null;
if (weak != null && (obj = weak.get()) == null) {
// object has been reclaimed by the GC
continue;
}
next = obj;
return true;
}
return false;
}
public Object next() {
if ((next == null) && !hasNext()) {
throw new NoSuchElementException();
}
Object obj = next;
next = null;
return obj;
}
public void remove() {
iter.remove();
}
};
}
Return an iteration over the elements in the set. |
protected final void maintain() {
WeakObject weak;
while ((weak = (WeakObject)queue.poll()) != null) {
set.remove(weak);
}
}
Maintain the elements in the set. Removes objects from the set that
have been reclaimed due to GC. |
public boolean remove(Object obj) {
maintain();
return set.remove(WeakObject.create(obj));
}
Removes the given element from this set if it is present. |
public int size() {
maintain();
return set.size();
}
Return the size of the set. |