StateHistory { public void add(final S state) { if (stateHistory.size() >= capacity) { stateHistory.removeFirst(); } stateHistory.addLast(state); save(); } StateHistory(
      @NonNull final String sourceName,
      @NonNull final Repository<Collection<S>> repository,
      @NonNull final MysqlSourceMetrics metrics);  StateHistory(
      @NonNull final String sourceName,
      @Min(1) final int capacity,
      @NonNull final Repository<Collection<S>> repository,
      @NonNull final MysqlSourceMetrics metrics); void add(final S state); S removeLast(); S removeLast(int count); void clear(); boolean isEmpty(); int size();  }
@Test public void test() throws Exception { MysqlSourceState firstState = mock(MysqlSourceState.class); MysqlSourceState secondState = mock(MysqlSourceState.class); MysqlSourceState thirdState = mock(MysqlSourceState.class); MysqlSourceState fourthState = mock(MysqlSourceState.class); TestRepository repository = new TestRepository(firstState); StateHistory<MysqlSourceState> history = new StateHistory<>(SOURCE_NAME, 2, repository, metrics); history.add(secondState); assertEquals(Arrays.asList(firstState, secondState), repository.get()); history.add(thirdState); history.add(fourthState); assertEquals(Arrays.asList(thirdState, fourthState), repository.get()); }