package de.faktorlogik.eclipse.ui;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.eclipse.core.databinding.observable.Diffs;
import org.eclipse.core.databinding.observable.set.WritableSet;
/**
* A writable set that allows to replace all the values of the set in one
* step.
*/
final class ReplacableWritableSet extends WritableSet {
/**
* Replaces the elements of this set with the specified elements.
*
* @param elements
* the elements to replace this collection with
* @return true
if this set has been changed,
* false
otherwise
*/
boolean replaceWith(final Collection elements) {
getterCalled();
Set removes = new HashSet();
Iterator wrappedIterator = wrappedSet.iterator();
while (wrappedIterator.hasNext()) {
Object element = wrappedIterator.next();
if (!elements.contains(element)) {
wrappedIterator.remove();
removes.add(element);
}
}
Set additions = new HashSet();
Iterator elementsIterator = elements.iterator();
while (elementsIterator.hasNext()) {
Object element = elementsIterator.next();
if (wrappedSet.add(element)) {
additions.add(element);
}
}
if (!additions.isEmpty() || !removes.isEmpty()) {
fireSetChange(Diffs.createSetDiff(additions, removes));
return true;
}
return false;
}
}