tip.util
Class EqualitySet<E>

java.lang.Object
  extended by tip.util.EqualitySet<E>
Type Parameters:
E - the type of the elements in the set
All Implemented Interfaces:
Iterable<E>, Collection<E>, Set<E>, Stringifiable<E>

public class EqualitySet<E>
extends Object
implements Set<E>, Stringifiable<E>

EqualitySet implements a Set where equality of elements can be abstracted using an Equalitor, i.e. elements in the set are regarded as equal based on Equalitor.equals(Object, Object).

Author:
Johnni Winther, jw@brics.dk

Constructor Summary
EqualitySet()
          Creates a new empty EqualitySet using the IdentityEqualitor as equalitor.
EqualitySet(Collection<E> c)
          Create a new EqualitySet using the IdentityEqualitor as equalitor, initially containing the elements in c.
EqualitySet(Collection<E> c, Equalitor<E> equalitor)
          Creates a new EqualitySet using equalitor as the equalitor, initially containing the elements in c.
EqualitySet(Equalitor<E> equalitor)
          Creates a new empty EqualitySet using equalitor as the equalitor.
EqualitySet(Set<EqualityElement<E>> set, Equalitor<E> equalitor)
          Create an EqualitySet containing the elements of set with equalitor as the equalitor.
 
Method Summary
 boolean add(E o)
          Adds the element o to this set.
 boolean add(EqualityElement<E> e)
          Adds the element held by the EqualityElement e to this set.
 boolean addAll(Collection<? extends E> c)
          Adds all elements of the collection c to this set
 boolean addAll(EqualitySet<E> c)
          Adds all elements in c to the set.
 void clear()
          
 EqualitySet<E> clone()
          Returns a shallow clone of this set.
 boolean contains(Object o)
          Returns true if this set contains the element o.
 boolean containsAll(Collection<?> c)
          
 boolean equals(Object o)
          
 Equalitor<E> getEqualitor()
          Returns the Equalitor used for element equality.
 int hashCode()
          Returns the hashcode for this EqualitySet.
 boolean isEmpty()
          
 Iterator<E> iterator()
          
 boolean remove(Object o)
          
 boolean removeAll(Collection<?> c)
          Removes all element in the collection c from this set.
 boolean retainAll(Collection<?> c)
          
 int size()
          
 Object[] toArray()
          
<T> T[]
toArray(T[] a)
          
 String toString()
          
 String toString(Stringifier<E> stringifier)
          Returns a text representation of the set using the stringifier.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

EqualitySet

public EqualitySet()
Creates a new empty EqualitySet using the IdentityEqualitor as equalitor.


EqualitySet

public EqualitySet(Collection<E> c)
Create a new EqualitySet using the IdentityEqualitor as equalitor, initially containing the elements in c.

Parameters:
c - the initial set

EqualitySet

public EqualitySet(Equalitor<E> equalitor)
Creates a new empty EqualitySet using equalitor as the equalitor.

Parameters:
equalitor - the equalitor used for element equality
Throws:
IllegalArgumentException - if no Equalitor was provided

EqualitySet

public EqualitySet(Collection<E> c,
                   Equalitor<E> equalitor)
Creates a new EqualitySet using equalitor as the equalitor, initially containing the elements in c.

Parameters:
equalitor - the equalitor used for element equality
c - the initial set
Throws:
IllegalArgumentException - if no Equalitor was provided

EqualitySet

public EqualitySet(Set<EqualityElement<E>> set,
                   Equalitor<E> equalitor)
Create an EqualitySet containing the elements of set with equalitor as the equalitor.

Parameters:
set - the initial set of elements
equalitor - the equalitor used for element equality
Throws:
IllegalArgumentException - if no Equalitor was provided
Method Detail

add

public boolean add(E o)
Adds the element o to this set.

Specified by:
add in interface Collection<E>
Specified by:
add in interface Set<E>
Parameters:
o - the element added to this set
Returns:
true is the element o was added to this set

add

public boolean add(EqualityElement<E> e)
Adds the element held by the EqualityElement e to this set. If the equalitor of e is not the same as the equalitor used in the set, an IllegalArgumentException is thrown.

Parameters:
e - the element added to this set
Returns:
true if this set was changed
Throws:
IllegalArgumentException - if the equalitors mismatch

addAll

public boolean addAll(Collection<? extends E> c)
Adds all elements of the collection c to this set

Specified by:
addAll in interface Collection<E>
Specified by:
addAll in interface Set<E>
Parameters:
c - the collection in question
Returns:
true if this set was changed

addAll

public boolean addAll(EqualitySet<E> c)
Adds all elements in c to the set. If the equalitor of e is not the same as the equalitor used in the set, an IllegalArgumentException is thrown.

Parameters:
c - the set of elements added to this set
Returns:
true if this set was changed
Throws:
IllegalArgumentException - if the equalitors mismatch

clear

public void clear()

Specified by:
clear in interface Collection<E>
Specified by:
clear in interface Set<E>

clone

public EqualitySet<E> clone()
Returns a shallow clone of this set.

Overrides:
clone in class Object
Returns:
a shallow clone of this set

contains

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

Specified by:
contains in interface Collection<E>
Specified by:
contains in interface Set<E>
Parameters:
o - the element in question
Returns:
true if this set contains the element o.

containsAll

public boolean containsAll(Collection<?> c)

Specified by:
containsAll in interface Collection<E>
Specified by:
containsAll in interface Set<E>

equals

public boolean equals(Object o)

Specified by:
equals in interface Collection<E>
Specified by:
equals in interface Set<E>
Overrides:
equals in class Object

getEqualitor

public Equalitor<E> getEqualitor()
Returns the Equalitor used for element equality.

Returns:
the Equalitor used for element equality

hashCode

public int hashCode()
Returns the hashcode for this EqualitySet.

Specified by:
hashCode in interface Collection<E>
Specified by:
hashCode in interface Set<E>
Overrides:
hashCode in class Object
Returns:
the hashcode for this EqualitySet

isEmpty

public boolean isEmpty()

Specified by:
isEmpty in interface Collection<E>
Specified by:
isEmpty in interface Set<E>

iterator

public Iterator<E> iterator()

Specified by:
iterator in interface Iterable<E>
Specified by:
iterator in interface Collection<E>
Specified by:
iterator in interface Set<E>

remove

public boolean remove(Object o)

Specified by:
remove in interface Collection<E>
Specified by:
remove in interface Set<E>

removeAll

public boolean removeAll(Collection<?> c)
Removes all element in the collection c from this set.

Specified by:
removeAll in interface Collection<E>
Specified by:
removeAll in interface Set<E>
Parameters:
c - the collection in question
Returns:
true if this set was changed

retainAll

public boolean retainAll(Collection<?> c)

Specified by:
retainAll in interface Collection<E>
Specified by:
retainAll in interface Set<E>

size

public int size()

Specified by:
size in interface Collection<E>
Specified by:
size in interface Set<E>

toArray

public Object[] toArray()

Specified by:
toArray in interface Collection<E>
Specified by:
toArray in interface Set<E>

toArray

public <T> T[] toArray(T[] a)

Specified by:
toArray in interface Collection<E>
Specified by:
toArray in interface Set<E>

toString

public String toString()

Overrides:
toString in class Object

toString

public String toString(Stringifier<E> stringifier)
Returns a text representation of the set using the stringifier.

Specified by:
toString in interface Stringifiable<E>
Parameters:
stringifier - the Stringifier used for elements
Returns:
a text representation of the set using the stringifier