org.geotools.util
Class DisjointSet<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractSet<E>
          extended by org.geotools.util.DisjointSet<E>
Type Parameters:
E - The type of elements in this set.
All Implemented Interfaces:
java.io.Serializable, java.lang.Iterable<E>, java.util.Collection<E>, java.util.Set<E>

public class DisjointSet<E>
extends java.util.AbstractSet<E>
implements java.io.Serializable

A set which is disjoint from others DisjointSets. Two sets are disjoint (or mutually exclusive) if their intersection is the empty set. Adding an element to a DisjointSet remove it from any other mutually exclusive DisjointSet. Optionnaly, DisjointSets may also have a trash set receiving removed elements. The example below creates 3 mutually exclusive sets with a trash:

 DisjointSet set0 = new DisjointSet(true); // Used as the trash set.
 DisjointSet set1 = new DisjointSet(set0);
 DisjointSet set2 = new DisjointSet(set0);
 
Disjoint sets are thread-safe.

Since:
2.0
Version:
$Id: DisjointSet.java 30792 2008-06-23 19:19:58Z desruisseaux $
Author:
Martin Desruisseaux (IRD)
See Also:
Serialized Form

Constructor Summary
DisjointSet()
          Construct a initially empty set.
DisjointSet(boolean hasTrash)
          Construct a initially empty set with an optional trash set.
DisjointSet(DisjointSet<E> disjointSet)
          Construct a new set mutually exclusive with the specified set.
 
Method Summary
 boolean add(E element)
          Ensures that this collection contains the specified element.
 boolean addAll(java.util.Collection<? extends E> c)
          Adds all of the elements in the specified collection to this set.
 void clear()
          Removes all of the elements from this set.
 boolean contains(java.lang.Object element)
          Returns true if this set contains the specified element.
 boolean containsAll(java.util.Collection<?> c)
          Returns true if this set contains all of the elements in the specified collection.
 boolean equals(java.lang.Object set)
          Compare this set with the specified object for equality.
 java.util.Set<E> getTrash()
          Returns the trash set, or null if there is none.
 int hashCode()
          Returns an hash value for this set.
 java.util.Iterator<E> iterator()
          Returns an iterator over the elements in this collection.
 boolean remove(java.lang.Object element)
          Removes a single instance of the specified element from this set, if it is present.
 boolean removeAll(java.util.Collection<?> c)
          Removes from this set all of its elements that are contained in the specified collection.
 boolean retainAll(java.util.Collection<?> c)
          Retains only the elements in this set that are contained in the specified collection.
 int size()
          Returns the number of elements in this set.
 java.lang.Object[] toArray()
          Returns an array containing all of the elements in this collection.
<T> T[]
toArray(T[] a)
          Returns an array containing all of the elements in this collection.
 java.lang.String toString()
          Returns a string representation of this set.
 
Methods inherited from class java.util.AbstractCollection
isEmpty
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
isEmpty
 

Constructor Detail

DisjointSet

public DisjointSet()
Construct a initially empty set. There is initially no other set mutually exclusive with this one. Mutually exclusive sets must be created using the DisjointSet(DisjointSet) constructor with this newly created set as argument.

DisjointSets constructed using this constructor has no trash. All remove operations on this set really remove all references to the removed element, like a usual Set. This is opposed to moving the element to a "trash" set, which is allowed by the DisjointSet(true) constructor.


DisjointSet

public DisjointSet(boolean hasTrash)
Construct a initially empty set with an optional trash set. There is initially no other set mutually exclusive with this one. Mutually exclusive sets must be created using the DisjointSet(DisjointSet) constructor with this newly created set as argument.

Parameters:
hasTrash - If true, all remove operations will add removed elements to a trash set (thus, really just moving the element to the trash). If false, there is no trash and this constructor behave like the no-argument constructor.
See Also:
getTrash()

DisjointSet

public DisjointSet(DisjointSet<E> disjointSet)
Construct a new set mutually exclusive with the specified set. All sets mutually exclusive with disjointSet will also be mutually exclusive with the newly created set. If disjointSet has a trash set, the newly created set will use the same trash (i.e. all remove operations will really move the element to the trash set). Otherwise, the new DisjointSet have no trash.

Parameters:
disjointSet - The set to be disjoint from.
Method Detail

getTrash

public java.util.Set<E> getTrash()
Returns the trash set, or null if there is none. The trash set receive all elements removed from this set.

Returns:
The trash set, or null if none.

size

public int size()
Returns the number of elements in this set. The size of this set may change as a result of adding elements to a mutually exclusive set.

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

contains

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

Specified by:
contains in interface java.util.Collection<E>
Specified by:
contains in interface java.util.Set<E>
Overrides:
contains in class java.util.AbstractCollection<E>
Parameters:
element - Object to be checked for containment in this set.
Returns:
true if this set contains the specified element.

add

public boolean add(E element)
Ensures that this collection contains the specified element. Adding an element to this set will remove it from any mutually exclusive set.

Specified by:
add in interface java.util.Collection<E>
Specified by:
add in interface java.util.Set<E>
Overrides:
add in class java.util.AbstractCollection<E>
Parameters:
element - Element whose presence in this set is to be ensured.
Returns:
true if the set changed as a result of the call.

remove

public boolean remove(java.lang.Object element)
Removes a single instance of the specified element from this set, if it is present. If this DisjointSet has a trash set, the removed element will be added to the trash set.

Specified by:
remove in interface java.util.Collection<E>
Specified by:
remove in interface java.util.Set<E>
Overrides:
remove in class java.util.AbstractCollection<E>
Parameters:
element - Element to be removed from this set.
Returns:
true if the set changed as a result of the call.

containsAll

public boolean containsAll(java.util.Collection<?> c)
Returns true if this set contains all of the elements in the specified collection.

Specified by:
containsAll in interface java.util.Collection<E>
Specified by:
containsAll in interface java.util.Set<E>
Overrides:
containsAll in class java.util.AbstractCollection<E>
Parameters:
c - collection to be checked for containment in this collection.
Returns:
true if this set contains all of the elements in the specified collection.

addAll

public boolean addAll(java.util.Collection<? extends E> c)
Adds all of the elements in the specified collection to this set. All of the elements will be removed from mutually exclusive sets.

Specified by:
addAll in interface java.util.Collection<E>
Specified by:
addAll in interface java.util.Set<E>
Overrides:
addAll in class java.util.AbstractCollection<E>
Parameters:
c - collection whose elements are to be added to this set.
Returns:
true if this set changed as a result of the call.

removeAll

public boolean removeAll(java.util.Collection<?> c)
Removes from this set all of its elements that are contained in the specified collection. If this DisjointSet has a trash set, all removed elements will be added to the trash set.

Specified by:
removeAll in interface java.util.Collection<E>
Specified by:
removeAll in interface java.util.Set<E>
Overrides:
removeAll in class java.util.AbstractSet<E>
Parameters:
c - elements to be removed from this set.
Returns:
true if this set changed as a result of the call.

retainAll

public boolean retainAll(java.util.Collection<?> c)
Retains only the elements in this set that are contained in the specified collection. If this DisjointSet has a trash set, all removed elements will be added to the trash set.

Specified by:
retainAll in interface java.util.Collection<E>
Specified by:
retainAll in interface java.util.Set<E>
Overrides:
retainAll in class java.util.AbstractCollection<E>
Parameters:
c - elements to be retained in this collection.
Returns:
true if this collection changed as a result of the call.

clear

public void clear()
Removes all of the elements from this set. If this DisjointSet has a trash set, all removed elements will be added to the trash set.

Specified by:
clear in interface java.util.Collection<E>
Specified by:
clear in interface java.util.Set<E>
Overrides:
clear in class java.util.AbstractCollection<E>

iterator

public java.util.Iterator<E> iterator()
Returns an iterator over the elements in this collection.

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

toArray

public java.lang.Object[] toArray()
Returns an array containing all of the elements in this collection.

Specified by:
toArray in interface java.util.Collection<E>
Specified by:
toArray in interface java.util.Set<E>
Overrides:
toArray in class java.util.AbstractCollection<E>
Returns:
an array containing all of the elements in this set.

toArray

public <T> T[] toArray(T[] a)
Returns an array containing all of the elements in this collection.

Specified by:
toArray in interface java.util.Collection<E>
Specified by:
toArray in interface java.util.Set<E>
Overrides:
toArray in class java.util.AbstractCollection<E>
Type Parameters:
T - The type of elements in the array.
Parameters:
a - The array into which the elements of the set are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Returns:
an array containing the elements of the set.

toString

public java.lang.String toString()
Returns a string representation of this set.

Overrides:
toString in class java.util.AbstractCollection<E>

hashCode

public int hashCode()
Returns an hash value for this set.

Specified by:
hashCode in interface java.util.Collection<E>
Specified by:
hashCode in interface java.util.Set<E>
Overrides:
hashCode in class java.util.AbstractSet<E>

equals

public boolean equals(java.lang.Object set)
Compare this set with the specified object for equality.

Specified by:
equals in interface java.util.Collection<E>
Specified by:
equals in interface java.util.Set<E>
Overrides:
equals in class java.util.AbstractSet<E>
Parameters:
set - The object to compare with this set for equality.


Copyright © 1996-2010 Geotools. All Rights Reserved.