fork download
  1. interface Collection<T> {
  2. void add(T t);
  3. void addRange(T[] ts);
  4. }
  5.  
  6. class CollectionBase<T> implements Collection<T> {
  7. @Override
  8. public void add(T t) {
  9.  
  10. }
  11. @Override
  12. public void addRange(T[] ts) {
  13. for (T t : ts) {
  14. add(t);
  15. }
  16. }
  17. }
  18.  
  19. class CollectionWithCounter<T> implements Collection<T> {
  20. int elementsCount = 0;
  21. Collection base;
  22.  
  23. public CollectionWithCounter(Collection<T> b) {
  24. base = b;
  25. }
  26. @Override
  27. public void add(T t) {
  28. elementsCount++;
  29. base.add(t);
  30. }
  31.  
  32. @Override
  33. public void addRange(T[] ts) {
  34. elementsCount += ts.length;
  35. base.addRange(ts);
  36. }
  37. }
  38.  
  39. class Main {
  40. public static void main(String[] args) {
  41. CollectionWithCounter<Object> cc;
  42.  
  43. cc = new CollectionWithCounter<>(new CollectionBase());
  44. cc.add(null);
  45. cc.add(null);
  46. cc.add(null);
  47. System.out.println("elementsCount = " + cc.elementsCount);
  48.  
  49. cc = new CollectionWithCounter<>(new CollectionBase());
  50. cc.addRange(new Object[3]);
  51. System.out.println("elementsCount = " + cc.elementsCount);
  52. }
  53. }
Success #stdin #stdout 0.03s 711168KB
stdin
Standard input is empty
stdout
elementsCount = 3
elementsCount = 3