class ValidatorsUtil {

  @SuppressWarnings("DoNotReturnNullOptionals")
  public static Optional<Integer> getValidatorIndex(BeaconState state, BLSPublicKey publicKey) {
    final Integer validatorIndex =
        BeaconStateCache.getTransitionCaches(state)
            .getValidatorIndex()
            .get(
                publicKey,
                key -> {
                  SSZList<Validator> validators = state.getValidators();
                  for (int i = 0; i < validators.size(); i++) {
                    final Validator validator = validators.get(i);
                    if (validator.getPubkey().equals(publicKey)) {
                      return i;
                    }
                  }
                  return null;
                });
    return Optional.ofNullable(validatorIndex)
        // The cache is shared between all states, so filter out any cached responses for validators
        // that are only added into later states
        .filter(index -> index < state.getValidators().size());
  }

}