interface Collection<T> {
void add(T t);
void addRange(T[] ts);
}
abstract class AbstractCollection<T> implements Collection<T> {
@Override
public void addRange(T[] ts) {
for (T t : ts) {
add(t);
}
}
}
class CollectionWithCounter<T> extends AbstractCollection<T> {
int elementsCount = 0;
@Override
public void add(T t) {
elementsCount++;
}
@Override
public void addRange(T[] ts) {
elementsCount += ts.length;
super.addRange(ts);
}
}
class Main {
public static void main
(String[] args
) { CollectionWithCounter<Object> cc;
cc = new CollectionWithCounter<>();
cc.add(null);
cc.add(null);
cc.add(null);
System.
out.
println("elementsCount = " + cc.
elementsCount);
cc = new CollectionWithCounter<>();
System.
out.
println("elementsCount = " + cc.
elementsCount); }
}
aW50ZXJmYWNlIENvbGxlY3Rpb248VD4gewoJdm9pZCBhZGQoVCB0KTsKCXZvaWQgYWRkUmFuZ2UoVFtdIHRzKTsKfQoKYWJzdHJhY3QgY2xhc3MgQWJzdHJhY3RDb2xsZWN0aW9uPFQ+IGltcGxlbWVudHMgQ29sbGVjdGlvbjxUPiB7CglAT3ZlcnJpZGUKCXB1YmxpYyB2b2lkIGFkZFJhbmdlKFRbXSB0cykgewoJCWZvciAoVCB0IDogdHMpIHsKCQkJYWRkKHQpOwoJCX0KCX0KfQoKY2xhc3MgQ29sbGVjdGlvbldpdGhDb3VudGVyPFQ+IGV4dGVuZHMgQWJzdHJhY3RDb2xsZWN0aW9uPFQ+IHsKCWludCBlbGVtZW50c0NvdW50ID0gMDsKCQoJQE92ZXJyaWRlCglwdWJsaWMgdm9pZCBhZGQoVCB0KSB7CgkJZWxlbWVudHNDb3VudCsrOwoJfQoJCglAT3ZlcnJpZGUKCXB1YmxpYyB2b2lkIGFkZFJhbmdlKFRbXSB0cykgewoJCWVsZW1lbnRzQ291bnQgKz0gdHMubGVuZ3RoOwoJCXN1cGVyLmFkZFJhbmdlKHRzKTsKCX0KfQoKY2xhc3MgTWFpbiB7CglwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7Cgk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