MetricsDeltaCalculator { public long calculateDelta(String metricName, Long currentValue) { Long previousValue = previousValues.put(metricName, currentValue); long delta = currentValue; if (previousValue != null) { delta = currentValue - previousValue; } return delta; }  long calculateDelta(String metricName, Long currentValue); void revertDelta(String metricName, Long delta); void clear();  }
@Test public void shouldReturnCurrentValueWhenThereIsNoPreviousStateForMetric() { long delta = calculator.calculateDelta("metricWithoutHistory", 13L); assertThat(delta).isEqualTo(13); }
@Test public void shouldReturnDeltaBetweenCurrentAndPreviousState() { calculator.calculateDelta("metric", 13L); long delta = calculator.calculateDelta("metric", 20L); assertThat(delta).isEqualTo(7); }