package makeo.gadomancy.client.effect; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.Set; class CheckpointingSet implements Iterable { private final Set main = new HashSet<>(); private final Set toAdd = Collections.synchronizedSet(new HashSet<>()); private final Set toRemove = Collections.synchronizedSet(new HashSet<>()); public void add(E elem) { toAdd.add(elem); } public void remove(E elem) { toRemove.add(elem); } public void clear() { synchronized (toAdd) { synchronized (toRemove) { toAdd.clear(); toRemove.clear(); main.clear(); } } } public void update() { synchronized (toAdd) { main.addAll(toAdd); toAdd.clear(); } synchronized (toRemove) { main.removeAll(toRemove); toRemove.clear(); } } @Override public Iterator iterator() { return new Iterator() { private final Iterator backing = main.iterator(); private boolean initialized = false; private E cur = null; @Override public boolean hasNext() { return backing.hasNext(); } @Override public E next() { initialized = true; return cur = backing.next(); } @Override public void remove() { if (!initialized) throw new IllegalStateException(); toRemove.add(cur); } }; } }