docs: @SuppressWarnings("unchecked")
    private void applyIndex(long index) {
      // Apply entries prior to this entry.
      if (reader.hasNext() && reader.getNextIndex() == index) {
        // Read the entry from the log. If the entry is non-null then apply it, otherwise
        // simply update the last applied index and return a null result.
        Indexed<RaftLogEntry> entry = reader.next();
        try {
          if (entry.index() != index) {
            throw new IllegalStateException("inconsistent index applying entry " + index + ": " + entry);
          }
          CompletableFuture future = futures.remove(index);
          apply(entry).whenComplete((r, e) -> {
            raft.setLastApplied(index);
            if (future != null) {
              if (e == null) {
                future.complete(r);
              } else {
                future.completeExceptionally(e);
              }
            }
          });
        } catch (Exception e) {
          logger.error("Failed to apply {}: {}", entry, e);
        }
      } else {
        CompletableFuture future = futures.remove(index);
        if (future != null) {
          logger.error("Cannot apply index " + index);
          future.completeExceptionally(new IndexOutOfBoundsException("Cannot apply index " + index));
        }
      }
    }