@Override
    public void addAt(long offset, T value) {
        // we consider -1 a legal offset to account for loading values from the 0-0.checkpoint
        if (offset < -1) {
            throw new IllegalArgumentException(
                String.format("Next offset %d must be greater than or equal to -1", offset)
            );
        }

        Map.Entry<Long, ?> lastEntry = history.lastEntry();
        if (lastEntry != null && offset <= lastEntry.getKey()) {
            throw new IllegalArgumentException(
                String.format("Next offset %d must be greater than the last offset %d", offset, lastEntry.getKey())
            );
        }

        history.put(offset, value);
    }
A implementation for {@code LogHistory} which uses a red-black tree to store values sorted by offset.
raft/src/main/java/org/apache/kafka/raft/internals/TreeMapLogHistory.java