common
Class IndexedHashSetDiff

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractSet
          extended by common.IndexedHashSetDiff
All Implemented Interfaces:
IndexedSet, IndexedSetDiff, SetDiff, java.lang.Cloneable, java.lang.Iterable, java.util.Collection, java.util.Set

public class IndexedHashSetDiff
extends java.util.AbstractSet
implements IndexedSetDiff, java.lang.Cloneable

Implementation of the IndexedSet interface that is represented as a set of differences from an underlying IndexedSet.

An IndexedHashSetDiff will behave correctly if objects are added to the underlying set after the IndexedHashSetDiff is created, as long as these objects have not also been added to the IndexedHashSetDiff (then they would be double-counted). Removing objects from the underlying set may cause an IndexedHashSetDiff to behave incorrectly, because it may change the indices of elements of the underlying set.


Nested Class Summary
 
Nested classes/interfaces inherited from interface common.IndexedSet
IndexedSet.EmptyIndexedSet
 
Field Summary
 
Fields inherited from interface common.IndexedSet
EMPTY_INDEXED_SET
 
Constructor Summary
IndexedHashSetDiff(IndexedSet underlying)
          Creates a new IndexedHashSetDiff with the given underlying IndexedSet.
 
Method Summary
 boolean add(java.lang.Object o)
          Adds the given object to this IndexedSet, with an index equal to the previous size of the set.
 void changeUnderlying()
          Changes the underlying IndexedSet so it is equal to this one, and clears the changes in this IndexedSetDiff.
 void clearChanges()
          Clears the changes in this IndexedSetDiff so it is once again equal to the underlying IndexedSet.
 java.lang.Object clone()
           
 boolean contains(java.lang.Object o)
           
 java.lang.Object get(int index)
          Returns the object with the specified index in this IndexedSet.
 java.util.Set getAdditions()
          Returns the set of objects that are in this set and not the underlying set.
 java.util.Set getRemovals()
          Returns the set of objects that are in the underlying set but not in this set.
 int indexOf(java.lang.Object o)
          Returns the index of the given object in this IndexedSet, or -1 if this IndexedSet does not contain the given object.
 java.util.Iterator iterator()
          Returns an iterator that is robust to additions to this IndexedSetDiff and additions to the underlying set.
static void main(java.lang.String[] args)
          Test program.
 boolean remove(java.lang.Object o)
          Removes the given object from this IndexedSet if it was present.
 int size()
           
 
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
 
Methods inherited from class java.util.AbstractCollection
addAll, clear, containsAll, isEmpty, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
addAll, clear, containsAll, equals, hashCode, isEmpty, removeAll, retainAll, toArray, toArray
 

Constructor Detail

IndexedHashSetDiff

public IndexedHashSetDiff(IndexedSet underlying)
Creates a new IndexedHashSetDiff with the given underlying IndexedSet.

Method Detail

size

public int size()
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()
Returns an iterator that is robust to additions to this IndexedSetDiff and additions to the underlying set. It is also robust to removals from this IndexedSetDiff if the removed object was in the underlying set at the time of removal.

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

contains

public boolean contains(java.lang.Object o)
Specified by:
contains in interface java.util.Collection
Specified by:
contains in interface java.util.Set
Overrides:
contains in class java.util.AbstractCollection

add

public boolean add(java.lang.Object o)
Adds the given object to this IndexedSet, with an index equal to the previous size of the set. Does nothing if the object was already in this set.

Specified by:
add in interface java.util.Collection
Specified by:
add in interface java.util.Set
Overrides:
add in class java.util.AbstractCollection
Returns:
true if the object was not already in this set

remove

public boolean remove(java.lang.Object o)
Removes the given object from this IndexedSet if it was present. If the object was in the set but not at the end of the ordering, this operation may change the indices of some elements.

Specified by:
remove in interface java.util.Collection
Specified by:
remove in interface java.util.Set
Overrides:
remove in class java.util.AbstractCollection
Returns:
true if the object was in this set

get

public java.lang.Object get(int index)
Returns the object with the specified index in this IndexedSet. Currently, if the given index corresponds to an element of the underlying set, then this method takes time linear in the number of objects removed from the underlying set.

Specified by:
get in interface IndexedSet
Throws:
java.lang.IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())

indexOf

public int indexOf(java.lang.Object o)
Returns the index of the given object in this IndexedSet, or -1 if this IndexedSet does not contain the given object.

Specified by:
indexOf in interface IndexedSet

getAdditions

public java.util.Set getAdditions()
Returns the set of objects that are in this set and not the underlying set.

Specified by:
getAdditions in interface SetDiff
Returns:
unmodifiable Set of objects

getRemovals

public java.util.Set getRemovals()
Returns the set of objects that are in the underlying set but not in this set.

Specified by:
getRemovals in interface SetDiff

changeUnderlying

public void changeUnderlying()
Changes the underlying IndexedSet so it is equal to this one, and clears the changes in this IndexedSetDiff. Note that his operation may change the indices of some elements.

Specified by:
changeUnderlying in interface SetDiff

clearChanges

public void clearChanges()
Clears the changes in this IndexedSetDiff so it is once again equal to the underlying IndexedSet.

Specified by:
clearChanges in interface SetDiff

clone

public java.lang.Object clone()
Overrides:
clone in class java.lang.Object

main

public static void main(java.lang.String[] args)
Test program.