public static ResultList fromBoolean(boolean result) { return new ResultList(result ? Result.TRUE : Result.FALSE); }
/** * Converts the given object value into a result list, so it can be compared by logical operators. * * @param value The object to convert. * @return The corresponding result value. */ public static ResultList toResultList(Object value) { if (value instanceof ResultList) { return (ResultList)value; } else if (value instanceof AttributeNode.VariableValueList) { ResultList retVal = new ResultList(); for (VariableValue vv : (AttributeNode.VariableValueList)value) { retVal.add(vv.getVariables(), Result.TRUE); } return retVal; } else if (value == null || value == Result.FALSE || value == Boolean.FALSE || (Number.class.isInstance(value) && ((Number)value).doubleValue() == 0)) { return new ResultList(Result.FALSE); } else if (value == Result.INVALID) { return new ResultList(Result.INVALID); } else { return new ResultList(Result.TRUE); } }
public ResultList evaluateListsFalse(AttributeNode.VariableValueList lhs, AttributeNode.VariableValueList rhs) { ResultList lst = evaluateListsTrue(lhs, rhs); if (lst.toResult() == Result.TRUE) { return new ResultList(Result.FALSE); } else if (lst.toResult() == Result.FALSE) { return new ResultList(Result.TRUE); } else { return lst; } }
/** * Evaluates a binary comparison where one or both operands are null. * Boolean outcomes are only defined for (in)equality relations, all others * return Result.INVALID. * * Precondition: lhs AND/OR rhs is null. */ private ResultList evaluateWithAtLeastOneNullSide(Object lhs, Object rhs) { if (operator.equals("==") || operator.equals("=")) { // Glob (=) operator falls back to equality for non-strings return ResultList.fromBoolean(lhs == rhs); } else if (operator.equals("!=")) { return ResultList.fromBoolean(lhs != rhs); } else { return new ResultList(Result.INVALID); } }
public ResultList evaluateListAndSingle(AttributeNode.VariableValueList lhs, Object rhs) { if (rhs == null && lhs == null) { return new ResultList(Result.TRUE); } if (rhs == null || lhs == null) { return new ResultList(Result.FALSE); } ResultList retVal = new ResultList(); for (int i = 0; i < lhs.size(); i++) { Result result = evaluateBool(lhs.get(i).getValue(), rhs); retVal.add((FieldPathIteratorHandler.VariableMap)lhs.get(i).getVariables().clone(), result); } return retVal; }
public Object evaluate(Context context) { Object oLeft = lhs.evaluate(context); Object oRight = rhs.evaluate(context); if (oLeft == null || oRight == null) { return evaluateWithAtLeastOneNullSide(oLeft, oRight); } if (oLeft == Result.INVALID || oRight == Result.INVALID) { return new ResultList(Result.INVALID); } if (oLeft instanceof AttributeNode.VariableValueList && oRight instanceof AttributeNode.VariableValueList) { if (operator.equals("==")) { return evaluateListsTrue((AttributeNode.VariableValueList)oLeft, (AttributeNode.VariableValueList)oRight); } else if (operator.equals("!=")) { return evaluateListsFalse((AttributeNode.VariableValueList)oLeft, (AttributeNode.VariableValueList)oRight); } else { return new ResultList(Result.INVALID); } } else if (oLeft instanceof AttributeNode.VariableValueList) { return evaluateListAndSingle((AttributeNode.VariableValueList)oLeft, oRight); } else if (oRight instanceof AttributeNode.VariableValueList) { return evaluateListAndSingle((AttributeNode.VariableValueList)oRight, oLeft); } return new ResultList(evaluateBool(oLeft, oRight)); }
public ResultList evaluateListsTrue(AttributeNode.VariableValueList lhs, AttributeNode.VariableValueList rhs) { if (lhs.size() != rhs.size()) { return new ResultList(Result.FALSE); } for (int i = 0; i < lhs.size(); i++) { if (!lhs.get(i).getVariables().equals(rhs.get(i).getVariables())) { return new ResultList(Result.FALSE); } if (evaluateEquals(lhs.get(i).getValue(), rhs.get(i).getValue()) == Result.FALSE) { return new ResultList(Result.FALSE); } } return new ResultList(Result.TRUE); }
public ResultList combineOR(ResultList other) { ResultList result = new ResultList(); // TODO: optimize for (ResultPair pair : results) { for (ResultPair otherPair : other.results) { FieldPathIteratorHandler.VariableMap varMap = (FieldPathIteratorHandler.VariableMap)pair.variables.clone(); if (combineVariables(varMap, otherPair.variables)) { result.add(varMap, combineOR(pair.result, otherPair.result)); } } } return result; }
public ResultList combineAND(ResultList other) { ResultList result = new ResultList(); // TODO: optimize for (ResultPair pair : results) { for (ResultPair otherPair : other.results) { FieldPathIteratorHandler.VariableMap varMap = (FieldPathIteratorHandler.VariableMap)pair.variables.clone(); if (combineVariables(varMap, otherPair.variables)) { result.add(varMap, combineAND(pair.result, otherPair.result)); } } } return result; }