interface Collection<T> {
void add(T t);
void addRange(T[] ts);
}
class CollectionBase<T> implements Collection<T> {
@Override
public void add(T t) {
}
@Override
public void addRange(T[] ts) {
for (T t : ts) {
add(t);
}
}
}
class CollectionWithCounter<T> implements Collection<T> {
int elementsCount = 0;
public CollectionWithCounter(Collection<T> b) {
base = b;
}
@Override
public void add(T t) {
elementsCount++;
base.add(t);
}
@Override
public void addRange(T[] ts) {
elementsCount += ts.length;
base.addRange(ts);
}
}
class Main {
public static void main
(String[] args
) { CollectionWithCounter<Object> cc;
cc = new CollectionWithCounter<>(new CollectionBase());
cc.add(null);
cc.add(null);
cc.add(null);
System.
out.
println("elementsCount = " + cc.
elementsCount);
cc = new CollectionWithCounter<>(new CollectionBase());
System.
out.
println("elementsCount = " + cc.
elementsCount); }
}
aW50ZXJmYWNlIENvbGxlY3Rpb248VD4gewoJdm9pZCBhZGQoVCB0KTsKCXZvaWQgYWRkUmFuZ2UoVFtdIHRzKTsKfQoKY2xhc3MgQ29sbGVjdGlvbkJhc2U8VD4gaW1wbGVtZW50cyBDb2xsZWN0aW9uPFQ+IHsKCUBPdmVycmlkZSAKCXB1YmxpYyB2b2lkIGFkZChUIHQpIHsKCQkKCX0KCUBPdmVycmlkZQoJcHVibGljIHZvaWQgYWRkUmFuZ2UoVFtdIHRzKSB7CgkJZm9yIChUIHQgOiB0cykgewoJCQlhZGQodCk7CgkJfQoJfQp9CgpjbGFzcyBDb2xsZWN0aW9uV2l0aENvdW50ZXI8VD4gaW1wbGVtZW50cyBDb2xsZWN0aW9uPFQ+IHsKCWludCBlbGVtZW50c0NvdW50ID0gMDsKCUNvbGxlY3Rpb24gYmFzZTsKCQoJcHVibGljIENvbGxlY3Rpb25XaXRoQ291bnRlcihDb2xsZWN0aW9uPFQ+IGIpIHsKCQliYXNlID0gYjsKCX0KCUBPdmVycmlkZQoJcHVibGljIHZvaWQgYWRkKFQgdCkgewoJCWVsZW1lbnRzQ291bnQrKzsKCQliYXNlLmFkZCh0KTsKCX0K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