org.aksw.commons.collections.tracker.TrackingSet Maven / Gradle / Ivy
package org.aksw.commons.collections.tracker;
import java.util.AbstractSet;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.aksw.commons.collections.diff.SetDiff;
/**
* Set which tracks insertions and removals to another set
*
* Can be used with maps by applying it to their .entrySet()
*
* @author raven
*
*/
public class TrackingSet
extends AbstractSet
{
protected SetDiff diff;
protected Set delegate;
public TrackingSet(Set delegate) {
this.delegate = delegate;
this.diff = new SetDiff<>(new HashSet(), new HashSet(), null);
}
public SetDiff getDiff() {
return diff;
}
public void restore() {
delegate.removeAll(diff.getAdded());
delegate.addAll(diff.getRemoved());
}
@Override
public boolean add(T e) {
boolean result = delegate.add(e);
if(result) {
// If the object was in the removed set, it means that
// its addition restored the original state
if(diff.getRemoved().contains(e)) {
diff.getRemoved().remove(e);
} else {
diff.getAdded().add(e);
}
}
return result;
}
@SuppressWarnings("unchecked")
@Override
public boolean remove(Object o) {
boolean result = delegate.remove(o);
if(result) {
if(diff.getAdded().contains(o)) {
diff.getAdded().remove(o);
} else {
diff.getRemoved().add((T)o);
}
}
return result;
}
@Override
public int size() {
return delegate.size();
}
@Override
public Iterator iterator() {
return delegate.iterator();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy