org.jboss.util.collection
Class WeakSet

java.lang.Object
  extended byjava.util.AbstractCollection
      extended byjava.util.AbstractSet
          extended byorg.jboss.util.collection.WeakSet
All Implemented Interfaces:
Collection, Set

public class WeakSet
extends AbstractSet
implements Set

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:
$Revision: 1.1 $
Author:
Jason Dillon

Field Summary
protected  ReferenceQueue queue
          The reference queue used to get object removal notifications.
protected  Set set
          The Set which will be used for element storage.
 
Constructor Summary
WeakSet()
          Construct a WeakSet based on a HashSet.
WeakSet(Set set)
          Construct a WeakSet.
 
Method Summary
 boolean add(Object obj)
          Add an element to the set.
 void clear()
          Removes all of the elements from this set.
 Object clone()
          Returns a shallow copy of this WeakSet instance: the elements themselves are not cloned.
 boolean contains(Object obj)
          Returns true if this set contains the specified element.
 boolean isEmpty()
          Returns true if this set contains no elements.
 Iterator iterator()
          Return an iteration over the elements in the set.
protected  void maintain()
          Maintain the elements in the set.
 boolean remove(Object obj)
          Removes the given element from this set if it is present.
 int size()
          Return the size of the set.
 
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

protected final ReferenceQueue queue
The reference queue used to get object removal notifications.


set

protected final Set set
The Set which will be used for element storage.

Constructor Detail

WeakSet

public WeakSet(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.

WeakSet

public WeakSet()
Construct a WeakSet based on a HashSet.

Method Detail

maintain

protected final void maintain()
Maintain the elements in the set. Removes objects from the set that have been reclaimed due to GC.


size

public int size()
Return the size of the set.

Specified by:
size in interface Set
Returns:
The size of the set.

iterator

public Iterator iterator()
Return an iteration over the elements in the set.

Specified by:
iterator in interface Set
Returns:
An iteration over the elements in the set.

add

public boolean add(Object obj)
Add an element to the set.

Specified by:
add in interface Set
Parameters:
obj - Element to add to the set.
Returns:
True if the element was added.

isEmpty

public boolean isEmpty()
Returns true if this set contains no elements.

Specified by:
isEmpty in interface Set
Returns:
true if this set contains no elements.

contains

public boolean contains(Object obj)
Returns true if this set contains the specified element.

Specified by:
contains in interface Set
Parameters:
obj - Element whose presence in this set is to be tested.
Returns:
true if this set contains the specified element.

remove

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

Specified by:
remove in interface Set
Parameters:
obj - Object to be removed from this set, if present.
Returns:
true if the set contained the specified element.

clear

public void clear()
Removes all of the elements from this set.

Specified by:
clear in interface Set

clone

public Object clone()
Returns a shallow copy of this WeakSet instance: the elements themselves are not cloned.

Returns:
A shallow copy of this set.


Copyright © 2002 JBoss Group, LLC. All Rights Reserved.