package com.orientechnologies.orient.core.sql.parser;

import java.util.Map;
import java.util.Set;

/**
 * Created by luigidellaquila on 18/02/15.
 */
public class OJsonItem {
  protected OIdentifier leftIdentifier;
  protected String      leftString;
  protected OExpression right;

  public void toString(Map<Object, Object> params, StringBuilder builder) {
    if (leftIdentifier != null) {
      builder.append("\"");
      leftIdentifier.toString(params, builder);
      builder.append("\"");
    }
    if (leftString != null) {
      builder.append("\"");
      builder.append(OExpression.encode(leftString));
      builder.append("\"");
    }
    builder.append(": ");
    right.toString(params, builder);
  }

  public String getLeftValue() {
    if (leftString != null) {
      return leftString;
    }
    if (leftIdentifier != null) {
      return leftIdentifier.getStringValue();
    }
    return null;
  }

  public boolean needsAliases(Set<String> aliases) {
    if (aliases.contains(leftIdentifier.getStringValue())) {
      return true;
    }
    if (right.needsAliases(aliases)) {
      return true;
    }
    return false;
  }

  public boolean isAggregate() {
    return right.isAggregate();
  }

  public OJsonItem splitForAggregation(AggregateProjectionSplit aggregateSplit) {
    if (isAggregate()) {
      OJsonItem item = new OJsonItem();
      item.leftIdentifier = leftIdentifier;
      item.leftString = leftString;
      item.right = right.splitForAggregation(aggregateSplit);
      return item;
    } else {
      return this;
    }
  }

  public OJsonItem copy() {
    OJsonItem result = new OJsonItem();
    result.leftIdentifier = leftIdentifier == null ? null : leftIdentifier.copy();
    result.leftString = leftString;
    result.right = right.copy();
    return result;
  }

  public void extractSubQueries(SubQueryCollector collector) {
    right.extractSubQueries(collector);
  }

  public boolean refersToParent() {
    return right != null && right.refersToParent();
  }
}

@@ -2003,11 +2003,837 @@
 t();%0A  %7D
+%0A%0A  @Override%0A  public boolean equals(Object o) %7B%0A    if (this == o)%0A      return true;%0A    if (o == null %7C%7C getClass() != o.getClass())%0A      return false;%0A%0A    OJsonItem oJsonItem = (OJsonItem) o;%0A%0A    if (leftIdentifier != null ? !leftIdentifier.equals(oJsonItem.leftIdentifier) : oJsonItem.leftIdentifier != null)%0A      return false;%0A    if (leftString != null ? !leftString.equals(oJsonItem.leftString) : oJsonItem.leftString != null)%0A      return false;%0A    return right != null ? right.equals(oJsonItem.right) : oJsonItem.right == null;%0A  %7D%0A%0A  @Override%0A  public int hashCode() %7B%0A    int result = leftIdentifier != null ? leftIdentifier.hashCode() : 0;%0A    result = 31 * result + (leftString != null ? leftString.hashCode() : 0);%0A    result = 31 * result + (right != null ? right.hashCode() : 0);%0A    return result;%0A  %7D
 %0A%7D%0A

Implement equals() and hashCode() for OJsonItem