// Path: src/main/java/org/tools4j/hoverraft/command/CommandLog.java
// public interface CommandLog {
//     long size();
//     default long lastIndex() {
//         return size() - 1;
//     }
//     int readTerm(long index);
//     void readTo(long index, LogKey target);
//     void readTo(long index, CommandKey target);
//     void readTo(long index, LogEntry target);
// 
//     void append(int term, Command command);
//     void truncateIncluding(long index);
// 
//     default int lastTerm() {
//         return readTerm(lastIndex());
//     }
// 
//     default void lastKeyTo(final LogKey target) {
//         readTo(lastIndex(), target);
//     }
// 
//     default int lastKeyCompareTo(final LogKey logKey) {
//         final int termCompare = Integer.compare(lastTerm(), logKey.term());
//         return termCompare == 0 ? Long.compare(lastIndex(), logKey.index()) : termCompare;
//     }
// 
//     boolean contains(CommandKey commandKey);
//     default LogContainment contains(final LogKey logKey) {
//         return LogContainment.containmentFor(logKey, this);
//     }
// 
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/command/LogEntry.java
// public interface LogEntry extends DirectPayload {
//     LogKey logKey();
// 
//     default LogEntry term(final int term) {
//         logKey().term(term);
//         return this;
//     }
//     default LogEntry index(final long index) {
//         logKey().index(index);
//         return this;
//     }
// 
//     Command command();
// 
//     default void copyFrom(final LogEntry logEntry) {
//         writeBufferOrNull().putBytes(offset(), logEntry.readBufferOrNull(), logEntry.offset(), logEntry.byteLength());
//     }
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/command/machine/StateMachine.java
// public interface StateMachine {
//     void onMessage(Command message);
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/message/AppendRequest.java
// public interface AppendRequest extends Message {
// 
//     int term();
// 
//     AppendRequest term(int term);
// 
//     int leaderId();
// 
//     AppendRequest leaderId(int leaderId);
// 
//     LogKey prevLogKey();
// 
//     long leaderCommit();
// 
//     AppendRequest leaderCommit(long leaderCommit);
// 
//     Sequence<LogEntry> logEntries();
// 
//     default AppendRequest appendLogEntry(final LogEntry logEntry) {
//         logEntries().append(logEntry);
//         return this;
//     }
// 
//     @Override
//     default Transition accept(final ServerContext serverContext, final EventHandler eventHandler) {
//         return eventHandler.onAppendRequest(serverContext, this);
//     }
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/message/VoteRequest.java
// public interface VoteRequest extends Message {
// 
//     int term();
// 
//     VoteRequest term(int term);
// 
//     int candidateId();
// 
//     VoteRequest candidateId(int candidateId);
// 
//     LogKey lastLogKey();
// 
//     @Override
//     default Transition accept(final ServerContext serverContext, final EventHandler eventHandler) {
//         return eventHandler.onVoteRequest(serverContext, this);
//     }
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/server/ServerContext.java
// public interface ServerContext {
// 
//     ServerConfig serverConfig();
// 
//     ConsensusConfig consensusConfig();
// 
//     Connections<Message> connections();
// 
//     DirectFactory directFactory();
// 
//     StateMachine stateMachine();
// 
//     Timer timer();
// 
//     ResendStrategy resendStrategy();
// 
//     void perform();
// 
//     default int id() {
//         return serverConfig().id();
//     }
// }


import java.util.Objects;
import org.tools4j.hoverraft.command.CommandLog;
import org.tools4j.hoverraft.command.LogEntry;
import org.tools4j.hoverraft.command.machine.StateMachine;
import org.tools4j.hoverraft.event.*;
import org.tools4j.hoverraft.message.AppendRequest;
import org.tools4j.hoverraft.message.VoteRequest;
import org.tools4j.hoverraft.server.ServerContext;
                .startWith(serverContext, event, higherTermHandler)
                .ifSteadyThen(serverContext, event, eventHandler());
    }

    public int currentTerm() {
        return persistentState.currentTerm();
    }

    protected PersistentState persistentState() {
        return persistentState;
    }

    protected VolatileState volatileState() {
        return volatileState;
    }

    protected Transition onVoteRequest(final ServerContext serverContext, final VoteRequest voteRequest) {
        return voteRequestHandler.onVoteRequest(serverContext, voteRequest);
    }

    protected Transition onAppendRequest(final ServerContext serverContext, final AppendRequest appendRequest) {
        return appendRequestHandler.onAppendRequest(serverContext, appendRequest);
    }

    protected void invokeStateMachineWithCommittedLogEntries(final ServerContext serverContext) {
        final CommandLog commandLog = persistentState.commandLog();
        final StateMachine stateMachine = serverContext.stateMachine();
        long lastApplied = volatileState.lastApplied();
        while (volatileState.commitIndex() > lastApplied) {
            lastApplied++;
// Path: src/main/java/org/tools4j/hoverraft/command/CommandLog.java
// public interface CommandLog {
//     long size();
//     default long lastIndex() {
//         return size() - 1;
//     }
//     int readTerm(long index);
//     void readTo(long index, LogKey target);
//     void readTo(long index, CommandKey target);
//     void readTo(long index, LogEntry target);
// 
//     void append(int term, Command command);
//     void truncateIncluding(long index);
// 
//     default int lastTerm() {
//         return readTerm(lastIndex());
//     }
// 
//     default void lastKeyTo(final LogKey target) {
//         readTo(lastIndex(), target);
//     }
// 
//     default int lastKeyCompareTo(final LogKey logKey) {
//         final int termCompare = Integer.compare(lastTerm(), logKey.term());
//         return termCompare == 0 ? Long.compare(lastIndex(), logKey.index()) : termCompare;
//     }
// 
//     boolean contains(CommandKey commandKey);
//     default LogContainment contains(final LogKey logKey) {
//         return LogContainment.containmentFor(logKey, this);
//     }
// 
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/command/LogEntry.java
// public interface LogEntry extends DirectPayload {
//     LogKey logKey();
// 
//     default LogEntry term(final int term) {
//         logKey().term(term);
//         return this;
//     }
//     default LogEntry index(final long index) {
//         logKey().index(index);
//         return this;
//     }
// 
//     Command command();
// 
//     default void copyFrom(final LogEntry logEntry) {
//         writeBufferOrNull().putBytes(offset(), logEntry.readBufferOrNull(), logEntry.offset(), logEntry.byteLength());
//     }
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/command/machine/StateMachine.java
// public interface StateMachine {
//     void onMessage(Command message);
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/message/AppendRequest.java
// public interface AppendRequest extends Message {
// 
//     int term();
// 
//     AppendRequest term(int term);
// 
//     int leaderId();
// 
//     AppendRequest leaderId(int leaderId);
// 
//     LogKey prevLogKey();
// 
//     long leaderCommit();
// 
//     AppendRequest leaderCommit(long leaderCommit);
// 
//     Sequence<LogEntry> logEntries();
// 
//     default AppendRequest appendLogEntry(final LogEntry logEntry) {
//         logEntries().append(logEntry);
//         return this;
//     }
// 
//     @Override
//     default Transition accept(final ServerContext serverContext, final EventHandler eventHandler) {
//         return eventHandler.onAppendRequest(serverContext, this);
//     }
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/message/VoteRequest.java
// public interface VoteRequest extends Message {
// 
//     int term();
// 
//     VoteRequest term(int term);
// 
//     int candidateId();
// 
//     VoteRequest candidateId(int candidateId);
// 
//     LogKey lastLogKey();
// 
//     @Override
//     default Transition accept(final ServerContext serverContext, final EventHandler eventHandler) {
//         return eventHandler.onVoteRequest(serverContext, this);
//     }
// }
// 
// Path: src/main/java/org/tools4j/hoverraft/server/ServerContext.java
// public interface ServerContext {
// 
//     ServerConfig serverConfig();
// 
//     ConsensusConfig consensusConfig();
// 
//     Connections<Message> connections();
// 
//     DirectFactory directFactory();
// 
//     StateMachine stateMachine();
// 
//     Timer timer();
// 
//     ResendStrategy resendStrategy();
// 
//     void perform();
// 
//     default int id() {
//         return serverConfig().id();
//     }
// }


// Path: src/main/java/org/tools4j/hoverraft/state/AbstractState.java
import java.util.Objects;
import org.tools4j.hoverraft.command.CommandLog;
import org.tools4j.hoverraft.command.LogEntry;
import org.tools4j.hoverraft.command.machine.StateMachine;
import org.tools4j.hoverraft.event.*;
import org.tools4j.hoverraft.message.AppendRequest;
import org.tools4j.hoverraft.message.VoteRequest;
import org.tools4j.hoverraft.server.ServerContext;

                .startWith(serverContext, event, higherTermHandler)
                .ifSteadyThen(serverContext, event, eventHandler());
    }

    public int currentTerm() {
        return persistentState.currentTerm();
    }

    protected PersistentState persistentState() {
        return persistentState;
    }

    protected VolatileState volatileState() {
        return volatileState;
    }

    protected Transition onVoteRequest(final ServerContext serverContext, final VoteRequest voteRequest) {
        return voteRequestHandler.onVoteRequest(serverContext, voteRequest);
    }

    protected Transition onAppendRequest(final ServerContext serverContext, final AppendRequest appendRequest) {
        return appendRequestHandler.onAppendRequest(serverContext, appendRequest);
    }

    protected void invokeStateMachineWithCommittedLogEntries(final ServerContext serverContext) {
        final CommandLog commandLog = persistentState.commandLog();
        final StateMachine stateMachine = serverContext.stateMachine();
        long lastApplied = volatileState.lastApplied();
        while (volatileState.commitIndex() > lastApplied) {
            lastApplied++;
            final LogEntry logEntry = serverContext.directFactory().logEntry();