import java.util.HashMap;
import java.util.Map;
public class Idempotency {
  record Saved(String fingerprint, int result) {}
  final Map<String, Saved> results = new HashMap<>();
  int effects = 0;
  int execute(String key, String fingerprint) {
    Saved previous = results.get(key);
    if (previous != null) {
      if (!previous.fingerprint().equals(fingerprint)) throw new IllegalArgumentException("Key conflict");
      return previous.result();
    }
    int result = ++effects;
    results.put(key, new Saved(fingerprint, result)); return result;
  }
  public static void main(String[] args) {
    var service = new Idempotency();
    System.out.println(service.execute("command-1", "same-input"));
    System.out.println(service.execute("command-1", "same-input"));
    if (service.effects != 1) throw new AssertionError("Duplicate effect");
    try { service.execute("command-1", "different-input"); throw new AssertionError("Conflict accepted"); }
    catch (IllegalArgumentException expected) { System.out.println(expected.getMessage()); }
  }
}
