net.spy.util
Class ReferenceSet

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractSet
          extended by net.spy.util.ReferenceSet
All Implemented Interfaces:
java.lang.Iterable, java.util.Collection, java.util.Set
Direct Known Subclasses:
SoftHashSet

public abstract class ReferenceSet
extends java.util.AbstractSet

This class aids in implementing sets of references.


Constructor Summary
ReferenceSet()
          Get an instance of ReferenceSet.
ReferenceSet(java.util.Collection c)
          Get a ReferenceSet with the contents from the given Collection.
ReferenceSet(int n)
          Create a ReferenceSet with the given capacity.
 
Method Summary
 boolean add(java.lang.Object o)
          Add an object to the Set.
protected abstract  java.lang.ref.Reference getReference(java.lang.Object o)
          Obtain the desired type of reference to the given object.
 java.util.Iterator iterator()
          Get an iterator.
 int size()
          Get the current size of the Set.
 
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
 
Methods inherited from class java.util.AbstractCollection
addAll, clear, contains, containsAll, isEmpty, remove, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
addAll, clear, contains, containsAll, isEmpty, remove, retainAll, toArray, toArray
 

Constructor Detail

ReferenceSet

public ReferenceSet()
Get an instance of ReferenceSet.


ReferenceSet

public ReferenceSet(int n)
Create a ReferenceSet with the given capacity.

Parameters:
n - the initial capacity

ReferenceSet

public ReferenceSet(java.util.Collection c)
Get a ReferenceSet with the contents from the given Collection.

Parameters:
c - the collection
Method Detail

add

public boolean add(java.lang.Object o)
Add an object to the Set.

Specified by:
add in interface java.util.Collection
Specified by:
add in interface java.util.Set
Overrides:
add in class java.util.AbstractCollection
Parameters:
o - the object
Returns:
true if the object did not already exist

size

public int size()
Get the current size of the Set. This is not an entirely cheap operation, as it walks the entire iterator to make sure all entries are still valid references.

Specified by:
size in interface java.util.Collection
Specified by:
size in interface java.util.Set
Specified by:
size in class java.util.AbstractCollection

iterator

public java.util.Iterator iterator()
Get an iterator. This iterator does not support removing entries due to limitations with HashMap and Iterator that would otherwise require me to duplicate all of HashMap.

Specified by:
iterator in interface java.lang.Iterable
Specified by:
iterator in interface java.util.Collection
Specified by:
iterator in interface java.util.Set
Specified by:
iterator in class java.util.AbstractCollection

getReference

protected abstract java.lang.ref.Reference getReference(java.lang.Object o)
Obtain the desired type of reference to the given object.

Unfortunately, java doesn't give me a way to enforce this in the language (i.e. at compile time), but subclasses of ReferenceSet must implement hashCode() and equals() in such a way that they return what the referenced object would return if the object were not a reference. If the reference has disappeared, equals() should return false, and hashCode should return 0.

Parameters:
o - an object
Returns:
a reference to that object


Copyright © 1995-2005 SPY Internetworking. All Rights Reserved.