org.geotools.util
Class DisjointSet

java.lang.Object
  extended byjava.util.AbstractCollection
      extended byjava.util.AbstractSet
          extended byorg.geotools.util.DisjointSet
All Implemented Interfaces:
java.util.Collection, java.util.Set

public class DisjointSet
extends java.util.AbstractSet

A set which is disjoint from others DisjointSets. Two sets are disjoint (or mutually exclusiveDisjointSet 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.

Version:
$Id: DisjointSet.java 11001 2005-02-02 03:18:45Z desruisseaux $
Author:
Martin Desruisseaux

Constructor Summary
DisjointSet()
          Construct a initially empty set.
DisjointSet(boolean hasTrash)
          Construct a initially empty set with an optional trash set.
DisjointSet(DisjointSet disjointSet)
          Construct a new set mutually exclusive with the specified set.
 
Method Summary
 boolean add(java.lang.Object element)
          Ensures that this collection contains the specified element.
 boolean addAll(java.util.Collection 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 getTrash()
          Returns the trash set, or null if there is none.
 int hashCode()
          Returns an hash value for this set.
 java.util.Iterator 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.
 java.lang.Object[] toArray(java.lang.Object[] 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 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 getTrash()
Returns the trash set, or null if there is none. The trash set receive all elements removed from this set.


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.


contains

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

Parameters:
element - Object to be checked for containment in this set.
Returns:
true if this set contains the specified element.

add

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

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.

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.

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 c)
Adds all of the elements in the specified collection to this set. All of the elements will be removed from mutually exclusive sets.

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.

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.

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.


iterator

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


toArray

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

Returns:
an array containing all of the elements in this set.

toArray

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

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.


hashCode

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


equals

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



Copyright © GeoTools. All Rights Reserved.