1: // Wrapper class - uses composition in place of inheritance
2: public class InstrumentedSet<E> extends ForwardingSet<E> {
3: private int addCount = 0;
4: public InstrumentedSet(Set<E> s) {
5: super(s);
6: }
7: @Override public boolean add(E e) {
8: addCount++;
9: return super.add(e);
10: }
11: @Override public boolean addAll(Collection<? extends E> c) {
12: addCount += c.size();
13: return super.addAll(c);
14: }
15: public int getAddCount() {
16: return addCount;
17: }
18: }
19: // Reusable forwarding class
20: public class ForwardingSet<E> implements Set<E> {
21: private final Set<E> s;
22: public ForwardingSet(Set<E> s) { this.s = s; }
23: public void clear() { s.clear(); }
24: public boolean contains(Object o) { return s.contains(o); }
25: public boolean isEmpty() { return s.isEmpty(); }
26: public int size() { return s.size(); }
27: public Iterator<E> iterator() { return s.iterator(); }
28: public boolean add(E e) { return s.add(e); }
29: public boolean remove(Object o) { return s.remove(o); }
30: public boolean containsAll(Collection<?> c)
31: { return s.containsAll(c); }
32: public boolean addAll(Collection<? extends E> c)
33: { return s.addAll(c); }
34: public boolean removeAll(Collection<?> c)
35: { return s.removeAll(c); }
36: public boolean retainAll(Collection<?> c)
37: { return s.retainAll(c); }
38: public Object[] toArray() { return s.toArray(); }
39: public <T> T[] toArray(T[] a) { return s.toArray(a); }
40: @Override public boolean equals(Object o)
41: { return s.equals(o); }
42: @Override public int hashCode() { return s.hashCode(); }
43: @Override public String toString() { return s.toString(); }
44: }