@Override public boolean evaluate(FieldManager fieldManager, IndentPrinter printer) { // to compress logs we 'inline' single $and operators boolean printOperator = !LogicalOperator.$and.equals(operator) || children.size() > 1; if (printOperator) { printer.print("- %s:", operator.name()); printer.startBlock(); } try { return evaluateOperator(fieldManager, printer); } finally { if (printOperator) { printer.endBlock(); } } }
public static LogicalOperator parse(String name) { if (name != null) { name = name.trim(); try { return LogicalOperator.valueOf(name); } catch (IllegalArgumentException e) { logException(e); } } return unknown; } }
public static Clause parse(String key, Object value) throws JSONException { if (key == null) { // The Root object, and objects inside arrays should be treated as $and. key = LogicalOperator.$and.name(); } key = key.trim(); LogicalOperator operator = LogicalOperator.parse(key); switch (operator) { case $or: return new LogicalClause(key, value); case $and: return new LogicalClause(key, value); case $not: return new LogicalClause(key, value); default: { return new ConditionalClause(key, value); } } }
protected LogicalClause(String key, Object value) throws JSONException { operatorName = key.trim(); operator = LogicalOperator.parse(operatorName); children = new ArrayList<>(); ApptentiveLog.v(INTERACTIONS, " + LogicalClause of type \"%s\"", operatorName); if (value instanceof JSONArray) { JSONArray jsonArray = (JSONArray) value; for (int i = 0; i < jsonArray.length(); i++) { JSONObject child = (JSONObject) jsonArray.get(i); children.add(ClauseParser.parse(null, child)); } } else if (value instanceof JSONObject) { JSONObject jsonObject = (JSONObject) value; Iterator<String> it = jsonObject.keys(); while (it.hasNext()) { String currentKey = it.next(); if (!jsonObject.isNull(currentKey)) { children.add(ClauseParser.parse(currentKey, jsonObject.get(currentKey))); } else { children.add(ClauseParser.parse(currentKey, null)); } } } else { ApptentiveLog.w(INTERACTIONS, "Unrecognized LogicalClause: %s", value.toString()); } }
ApptentiveLog.v(INTERACTIONS, " - </%s>", operator.name()); return false;