persistable sclass NotifyingCollection extends AbstractCollection implements Collection { Collection c; // Backing Collection *() {} // for persistence *(Collection *c) {} public int size() { synchronized (this) {return c.size();} } public boolean isEmpty() { synchronized (this) {return c.isEmpty();} } public boolean contains(Object o) { synchronized (this) {return c.contains(o);} } public Object[] toArray() { synchronized (this) {return c.toArray();} } public T[] toArray(T[] a) { synchronized (this) {return c.toArray(a);} } public Iterator iterator() { return c.iterator(); // caller should not use Iterator.remove() } public boolean add(E e) { synchronized (this) { beforeChange(); if (!c.add(e)) false; } change(); true; } public boolean remove(Object o) { synchronized (this) { beforeChange(); if (!c.remove(o)) false;} change(); true; } public boolean containsAll(Collection coll) { synchronized (this) {return c.containsAll(coll);} } public boolean addAll(Collection coll) { synchronized (this) { beforeChange(); if (!c.addAll(coll)) false;} change(); true; } public boolean removeAll(Collection coll) { synchronized (this) { beforeChange(); if (!c.removeAll(coll)) false;} change(); true; } public boolean retainAll(Collection coll) { synchronized (this) { beforeChange(); if (!c.retainAll(coll)) false;} change(); true; } public void clear() { synchronized (this) { beforeChange(); c.clear();} change(); } public String toString() { synchronized (this) {return c.toString();} } /*public void forEach(Consumer consumer) { synchronized (this) {c.forEach(consumer);} }*/ /*public boolean removeIf(Predicate filter) { synchronized (this) {return c.removeIf(filter);} }*/ @Override public Spliterator spliterator() { return c.spliterator(); // Must be manually synched by user! } /*public Stream stream() { return c.stream(); // Must be manually synched by user! }*/ /*public Stream parallelStream() { return c.parallelStream(); // Must be manually synched by user! }*/ void beforeChange() {} void change() {} Collection unwrap() { ret c; } }