How to compare Object according to some sql like conditional rule

I have json condition:

{
  "condition": "AND",
  "rules": [
    {
      "id": "price",
      "field": "price",
      "type": "double",
      "input": "text",
      "operator": "less",
      "value": "10.25"
    },
    {
      "condition": "OR",
      "rules": [
        {
          "id": "category",
          "field": "category",
          "type": "integer",
          "input": "select",
          "operator": "equal",
          "value": "2"
        },
        {
          "id": "category",
          "field": "category",
          "type": "integer",
          "input": "select",
          "operator": "equal",
          "value": "1"
        },
        {
          "condition": "AND",
          "rules": [
            {
              "id": "name",
              "field": "name",
              "type": "string",
              "input": "text",
              "operator": "equal",
              "value": "asdasd"
            }
          ]
        }
      ]
    }
  ]
}

And I convert it into ConditionalRule class:

public class ConditionalRule extends Base {
    public String condition;
    public List<Base> rules = new ArrayList<Base>();

    @Override
    public String toString() {
        return "ConditionalRule [condition=" + condition + ", rules=" + rules
                + "]";
    }



}

public class Base {

}

public class Rule extends Base {

    public String id;
    public String field;
    public String type;
    public String input;
    public String operator;
    public String value;

    @Override
    public String toString() {
        return "Rule [id=" + id + ", field=" + field + ", type=" + type
                + ", input=" + input + ", operator=" + operator + ", value="
                + value + "]";
    }
}

I was able to convert ConditionalRule object into sql where clause, like:

price < 10.25 AND (category = 2 OR category = 1 OR (name = 'asdasd'))

But now I have more challeging task, compare some object according to some ConditionalRule class, like

public boolean matches(Object obj, ConditionalRule rule) {

   //TODO: implement this part

   return true;
}


Source: java

Leave a Reply