HistoryChain { S getNonnullPayload(int fromThis) { HistoryChain<S> pointer = this; int counter = 0; while (counter < fromThis || pointer.state == null) { if (pointer.state != null) { counter++; } pointer = pointer.previous; if (pointer == null) { throw new NoSuchElementException(); } } return pointer.state; } HistoryChain(@Nullable HistoryChain<S> previous, @Nullable S state);   }
@Test void testGetNonnullPayload() { HistoryChain<String> historyChain = new HistoryChain<>(null, "payload1").append("payload2"); assertEquals("payload1", historyChain.getNonnullPayload(1)); }