package io.burt.jmespath.node;

import io.burt.jmespath.Adapter;

public class ComparisonNode extends OperatorNode {
  private final String operator;

  public ComparisonNode(String operator, JmesPathNode left, JmesPathNode right) {
    super(left, right);
    this.operator = operator;
  }

  @Override
  protected <T> T evaluateOne(Adapter<T> adapter, T currentValue) {
    T leftResult = operands()[0].evaluate(adapter, currentValue);
    T rightResult = operands()[1].evaluate(adapter, currentValue);
    if (adapter.isNumber(leftResult) && adapter.isNumber(rightResult)) {
      return compareNumbers(adapter, leftResult, rightResult);
    } else {
      return compareObjects(adapter, leftResult, rightResult);
    }
  }

  private <T> T compareObjects(Adapter<T> adapter, T leftResult, T rightResult) {
    int result = adapter.compare(leftResult, rightResult);
    if (operator.equals("==")) {
      return adapter.createBoolean(result == 0);
    } else if (operator.equals("!=")) {
      return adapter.createBoolean(result != 0);
    }
    return adapter.createNull();
  }

  private <T> T compareNumbers(Adapter<T> adapter, T leftResult, T rightResult) {
    int result = adapter.compare(leftResult, rightResult);
    if (operator.equals("==")) {
      return adapter.createBoolean(result == 0);
    } else if (operator.equals("!=")) {
      return adapter.createBoolean(result != 0);
    } else if (operator.equals(">")) {
      return adapter.createBoolean(result > 0);
    } else if (operator.equals(">=")) {
      return adapter.createBoolean(result >= 0);
    } else if (operator.equals("<")) {
      return adapter.createBoolean(result < 0);
    } else if (operator.equals("<=")) {
      return adapter.createBoolean(result <= 0);
    }
    return adapter.createNull();
  }

  protected String operator() {
    return operator;
  }

  @Override
  protected String internalToString() {
    return operator;
  }

  @Override
  protected boolean internalEquals(Object o) {
    ComparisonNode other = (ComparisonNode) o;
    return operator().equals(other.operator());
  }

  @Override
  protected int internalHashCode() {
    return operator.hashCode();
  }
}

@@ -1902,32 +1902,91 @@
 %0A    return 
-operator
+String.format(%22%25s, %25s, %25s%22, operator, operands()%5B0%5D, operands()%5B1%5D)
 ;%0A  %7D%0A%0A  @Ov

Fix ComparisonNode#toString