org.cojen.util
Class WeakCanonicalSet<T>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractSet<T>
          extended by org.cojen.util.WeakCanonicalSet<T>
All Implemented Interfaces:
Iterable<T>, Collection<T>, Set<T>
Direct Known Subclasses:
WeakFlyweightSet

public class WeakCanonicalSet<T>
extends AbstractSet<T>

A thread-safe Set that manages canonical objects: sharable objects that are typically immutable. Call the put method for supplying the WeakCanonicalSet with candidate canonical instances.

Objects that do not customize the hashCode and equals methods don't make sense to be canonicalized because each instance will be considered unique. The object returned from the put method will always be the same as the one passed in.

Author:
Brian S O'Neill

Constructor Summary
WeakCanonicalSet()
           
 
Method Summary
 boolean contains(Object obj)
           
protected  boolean equals(Object a, Object b)
           
protected  int hashCode(Object obj)
           
 Iterator<T> iterator()
           
<U extends T>
U
put(U obj)
          Pass in a candidate canonical object and get a unique instance from this set.
 int size()
           
 String toString()
           
 
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
 
Methods inherited from class java.util.AbstractCollection
add, addAll, clear, containsAll, isEmpty, remove, retainAll, toArray, toArray
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
add, addAll, clear, containsAll, isEmpty, remove, retainAll, toArray, toArray
 

Constructor Detail

WeakCanonicalSet

public WeakCanonicalSet()
Method Detail

put

public <U extends T> U put(U obj)
Pass in a candidate canonical object and get a unique instance from this set. The returned object will always be of the same type as that passed in. If the object passed in does not equal any object currently in the set, it will be added to the set, becoming canonical.

Parameters:
obj - candidate canonical object; null is also accepted

iterator

public Iterator<T> iterator()
Specified by:
iterator in interface Iterable<T>
Specified by:
iterator in interface Collection<T>
Specified by:
iterator in interface Set<T>
Specified by:
iterator in class AbstractCollection<T>

size

public int size()
Specified by:
size in interface Collection<T>
Specified by:
size in interface Set<T>
Specified by:
size in class AbstractCollection<T>

contains

public boolean contains(Object obj)
Specified by:
contains in interface Collection<T>
Specified by:
contains in interface Set<T>
Overrides:
contains in class AbstractCollection<T>

toString

public String toString()
Overrides:
toString in class AbstractCollection<T>

hashCode

protected int hashCode(Object obj)

equals

protected boolean equals(Object a,
                         Object b)


Copyright © 2004-2008 Brian S O'Neill. All Rights Reserved.