126 lines
2.5 KiB
Java
126 lines
2.5 KiB
Java
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<E> extends AbstractSet<E> {
|
|
|
|
private final LogSet<E>.MyLog log = new LogSet.MyLog();
|
|
final Set<E> wrapped;
|
|
private Runnable verify;
|
|
|
|
public final LogSet<E>.MyLog myLog() {
|
|
return this.log;
|
|
}
|
|
|
|
public LogSet(Set<E> wrapped) {
|
|
this.wrapped = wrapped;
|
|
}
|
|
|
|
LogSet<E> 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<E> 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<E> {
|
|
private final Iterator<E> it;
|
|
E current;
|
|
|
|
public WrapIt(Iterator<E> 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) {
|
|
|
|
}
|
|
}
|
|
}
|