ExpressionEvaluatorImpl implements ExpressionEvaluator { private boolean evaluateSimpleBooleanExpression(SimpleBooleanExpression simpleBooleanExpression, DataRow dataRow) { if (simpleBooleanExpression == null) { return false; } Object left = simpleBooleanExpression.getLeft(); Object right = simpleBooleanExpression.getRight(); BooleanRelation booleanRelation = simpleBooleanExpression.getBooleanRelation(); if (oneOfIsNull(left, right)) { return false; } if (!isConstant(left.getClass())) { return evaluateSimpleBooleanExpression(new SimpleBooleanExpression(evaluate(left, dataRow), booleanRelation, right), dataRow); } if (!isConstant(right.getClass())) { return evaluateSimpleBooleanExpression(new SimpleBooleanExpression(left, booleanRelation, evaluate(right, dataRow)), dataRow); } Class<?> leftClass = left.getClass(); Class<?> rightClass = right.getClass(); if (bothAreNumbers(leftClass, rightClass)) { double leftAsDouble = asDouble(left); double rightAsDouble = asDouble(right); switch (booleanRelation) { case EQUAL: return leftAsDouble == rightAsDouble; case GREATER_THAN: return leftAsDouble > rightAsDouble; case LESS_THAN: return leftAsDouble < rightAsDouble; case GREATER_THAN_EQUAL: return leftAsDouble >= rightAsDouble; case LESS_THAN_EQUAL: return leftAsDouble <= rightAsDouble; case NOT_EQUAL: return leftAsDouble != rightAsDouble; default: throw new IllegalArgumentException(String.format( "%s operation is not applicable to numbers: %f %s %f", booleanRelation.getText(), leftAsDouble, booleanRelation.getText(), rightAsDouble )); } } else if (bothAreBooleans(leftClass, rightClass)) { boolean leftAsBoolean = asBoolean(left); boolean rightAsBoolean = asBoolean(right); switch (booleanRelation) { case EQUAL: return leftAsBoolean == rightAsBoolean; case NOT_EQUAL: return leftAsBoolean != rightAsBoolean; default: throw new IllegalArgumentException(String.format( "%s operation is not applicable to booleans: %s %s %s", booleanRelation.getText(), leftAsBoolean, booleanRelation.getText(), rightAsBoolean )); } } else { String leftAsString = asString(left); String rightAsString = asString(right); switch (booleanRelation) { case EQUAL: return leftAsString.equals(rightAsString); case NOT_EQUAL: return !leftAsString.equals(rightAsString); case LIKE: { String rightAsRegex = rightAsString .replaceAll("(?<!\\\\)%", ".*") .replaceAll("\\%", "%") .replaceAll("(?<!\\\\)_", ".") .replaceAll("\\_", "_"); return matchPattern(leftAsString, rightAsRegex); } case REGEXP: return matchPattern(leftAsString, rightAsString); default: throw new IllegalArgumentException(String.format( "%s operation is not applicable to strings: '%s' %s '%s'", booleanRelation.getText(), leftAsString, booleanRelation.getText(), rightAsString )); } } }  @Override Map<String, Set<String>> getColumnNames(Object expression); @Override T evaluate(Object expression, DataRow dataRow); @Override T evaluateAs(Object expression, DataRow dataRow, Class<T> cls);  }
@Test public void testEvaluateSimpleBooleanExpression() { assertThat(bool(null, EQUAL, 1), is(false)); assertThat(bool(1, EQUAL, null), is(false)); assertThat(bool(1, EQUAL, 1), is(true)); assertThat(bool(1L, EQUAL, 1d), is(true)); assertThat(bool(2, GREATER_THAN, 1), is(true)); assertThat(bool(2f, GREATER_THAN_EQUAL, 1), is(true)); assertThat(bool(absFunction(), GREATER_THAN_EQUAL, 1, ROW_WITH_VALUES), is(true)); assertThat(bool(1, LESS_THAN, absFunction(), ROW_WITH_VALUES), is(true)); assertThat(bool(1d, LESS_THAN_EQUAL, 2), is(true)); assertThat(bool(1, NOT_EQUAL, 1), is(false)); assertThat(bool(1L, NOT_EQUAL, 2), is(true)); assertThat(bool("123", EQUAL, 123), is(true)); assertThat(bool(123, EQUAL, "123"), is(true)); assertThat(bool("123", EQUAL, "123"), is(true)); assertThat(bool("123", NOT_EQUAL, "456"), is(true)); assertThat(bool("01234", LIKE, "123"), is(true)); assertThat(bool("123", LIKE, "1_3"), is(true)); assertThat(bool("123", LIKE, "1\\_3"), is(false)); assertThat(bool("123", LIKE, "%3%"), is(true)); assertThat(bool("123", LIKE, "\\%3"), is(false)); assertThat(bool("123", REGEXP, "..3"), is(true)); assertThat(bool("123", REGEXP, ".\\.3"), is(false)); assertThat(bool(true, EQUAL, false), is(false)); assertThat(bool(true, NOT_EQUAL, false), is(true)); }