@Test public void incorrectState() { Counters counters = new Counters(); counters.changeCounter(Counters.SELECT_COUNT, 1); counters.changeCounter(Counters.SELECT_COUNT, 1); counters.changeCounter(Counters.SELECT_COUNT, -1); counters.changeCounter(Counters.SELECT_COUNT, -1); }
public void changeCounter(CounterSpec spec, int delta) { if (delta == 0) return; int oldVal = getCounter(spec); int newVal = oldVal + delta; if (newVal < 0) { throw new IllegalStateException("Counter " + spec + " decreased to the value less than zero."); } if (newVal != 0) { if (myCounters == null) { myCounters = new ListMap<>(); } myCounters.put(spec, newVal); } else { if (myCounters != null) { myCounters.remove(spec); if (myCounters.isEmpty()) { myCounters = null; } } } }
Counters implements HasCounters { public void changeCounter(CounterSpec spec, int delta) { if (delta == 0) return; int oldVal = getCounter(spec); int newVal = oldVal + delta; if (newVal < 0) { throw new IllegalStateException("Counter " + spec + " decreased to the value less than zero."); } if (newVal != 0) { if (myCounters == null) { myCounters = new ListMap<>(); } myCounters.put(spec, newVal); } else { if (myCounters != null) { myCounters.remove(spec); if (myCounters.isEmpty()) { myCounters = null; } } } } }
Counters implements HasCounters { public void changeCounter(CounterSpec spec, int delta) { if (delta == 0) return; int oldVal = getCounter(spec); int newVal = oldVal + delta; if (newVal < 0) { throw new IllegalStateException("Counter " + spec + " decreased to the value less than zero."); } if (newVal != 0) { if (myCounters == null) { myCounters = new ListMap<>(); } myCounters.put(spec, newVal); } else { if (myCounters != null) { myCounters.remove(spec); if (myCounters.isEmpty()) { myCounters = null; } } } }  }
Counters implements HasCounters { public void changeCounter(CounterSpec spec, int delta) { if (delta == 0) return; int oldVal = getCounter(spec); int newVal = oldVal + delta; if (newVal < 0) { throw new IllegalStateException("Counter " + spec + " decreased to the value less than zero."); } if (newVal != 0) { if (myCounters == null) { myCounters = new ListMap<>(); } myCounters.put(spec, newVal); } else { if (myCounters != null) { myCounters.remove(spec); if (myCounters.isEmpty()) { myCounters = null; } } } }  int getCounter(CounterSpec spec); void changeCounter(CounterSpec spec, int delta); boolean isEmpty(); }
Counters implements HasCounters { public void changeCounter(CounterSpec spec, int delta) { if (delta == 0) return; int oldVal = getCounter(spec); int newVal = oldVal + delta; if (newVal < 0) { throw new IllegalStateException("Counter " + spec + " decreased to the value less than zero."); } if (newVal != 0) { if (myCounters == null) { myCounters = new ListMap<>(); } myCounters.put(spec, newVal); } else { if (myCounters != null) { myCounters.remove(spec); if (myCounters.isEmpty()) { myCounters = null; } } } }  int getCounter(CounterSpec spec); void changeCounter(CounterSpec spec, int delta); boolean isEmpty(); static final CounterSpec HIGHLIGHT_COUNT; static final CounterSpec SELECT_COUNT; }