package dblog; /** * Description: des * Author: zsx * CreateDate: 2020/4/16 20:51 */ import java.util.AbstractSet; import java.util.Iterator; import java.util.Set; public class LogSet extends AbstractSet { private final LogSet.MyLog log = new LogSet.MyLog(); final Set wrapped; private Runnable verify; public final LogSet.MyLog myLog() { return this.log; } public LogSet(Set wrapped) { this.wrapped = wrapped; } LogSet setVerify(Runnable verify) { this.verify = verify; return this; } public boolean add(E e) { if (this.verify != null) { this.verify.run(); } if (this.wrapped.add(e)) { this.myLog().afterAdd(e); return true; } else { return false; } } public void clear() { if (this.verify != null) { this.verify.run(); } Iterator it = this.iterator(); while(it.hasNext()) { this.myLog().afterRemove(it.next()); } this.wrapped.clear(); } public boolean contains(Object o) { return this.wrapped.contains(o); } public Iterator iterator() { return new LogSet.WrapIt(this.wrapped.iterator()); } public boolean remove(Object o) { if (this.verify != null) { this.verify.run(); } if (this.wrapped.remove(o)) { this.myLog().afterRemove(o); return true; } else { return false; } } public int size() { return this.wrapped.size(); } public int hashCode() { return this.wrapped.hashCode(); } protected class WrapIt implements Iterator { private final Iterator it; E current; public WrapIt(Iterator iterator) { this.it = iterator; } public boolean hasNext() { return this.it.hasNext(); } public E next() { return this.current = this.it.next(); } public void remove() { if (LogSet.this.verify != null) { LogSet.this.verify.run(); } LogSet.this.myLog().afterRemove(this.current); this.it.remove(); } } private final class MyLog { private MyLog() { } private void afterRemove(Object e) { } private void afterAdd(E e) { } } }