### Question:
StatefulSystemMetricsCollector { @Nullable public R getLatestDiff() { mIsValid &= mCollector.getSnapshot(this.mCurr); if (!mIsValid) { return null; } mCurr.diff(mPrev, mDiff); return mDiff; } StatefulSystemMetricsCollector(S collector);  StatefulSystemMetricsCollector(S collector, R curr, R prev, R diff); S getCollector(); @Nullable R getLatestDiffAndReset(); @Nullable R getLatestDiff();  }### Answer:
@Test public void testCustomBaseSnapshot() throws Exception { DummyMetric metric = new DummyMetric(); metric.value = 0; DummyMetricCollector collector = new DummyMetricCollector(); collector.currentValue = 10; StatefulSystemMetricsCollector<DummyMetric, DummyMetricCollector> withCustomInitialSnapshot = new StatefulSystemMetricsCollector<>( collector, collector.createMetrics(), metric, collector.createMetrics()); StatefulSystemMetricsCollector<DummyMetric, DummyMetricCollector> defaultInitialSnapshot = new StatefulSystemMetricsCollector<>(collector); assertThat(withCustomInitialSnapshot.getLatestDiff().value).isEqualTo(10); assertThat(defaultInitialSnapshot.getLatestDiff().value).isEqualTo(0); }
@Test public void testGetLatestDiff() { DummyMetricCollector collector = new DummyMetricCollector(); collector.currentValue = 10; StatefulSystemMetricsCollector<DummyMetric, DummyMetricCollector> statefulCollector = new StatefulSystemMetricsCollector<>(collector); assertThat(statefulCollector.getLatestDiff().value).isEqualTo(0); collector.currentValue = 20; assertThat(statefulCollector.getLatestDiff().value).isEqualTo(10); collector.currentValue = 30; assertThat(statefulCollector.getLatestDiff().value).isEqualTo(20); }