ExpressionEvaluatorImpl implements ExpressionEvaluator { @Override public <T extends Comparable<? super T>> T evaluate(Object expression, DataRow dataRow) { Assert.notNull(expression, "Expression can't be null"); if (expression instanceof Null) { return null; } else if (isColumnExpression(expression)) { return evaluateColumnExpression((ColumnExpression) expression, dataRow); } else if (expression instanceof FunctionExpression) { return evaluateFunctionExpression((FunctionExpression) expression, dataRow); } else if (expression instanceof IsNullExpression) { return cast(evaluateIsNullExpression((IsNullExpression) expression, dataRow), Boolean.class, Comparable.class); } else if (expression instanceof InExpression) { return cast(evaluateInExpression((InExpression) expression, dataRow), Boolean.class, Comparable.class); } else if (expression instanceof LiteralBooleanExpression) { return cast(evaluateLiteralBooleanExpression((LiteralBooleanExpression) expression), Boolean.class, Comparable.class); } else if (expression instanceof SimpleBooleanExpression) { return cast(evaluateSimpleBooleanExpression((SimpleBooleanExpression) expression, dataRow), Boolean.class, Comparable.class); } else if (expression instanceof BinaryBooleanExpression) { return cast(evaluateBinaryBooleanExpression((BinaryBooleanExpression) expression, dataRow), Boolean.class, Comparable.class); } else if (expression instanceof UnaryBooleanExpression) { return cast(evaluateUnaryBooleanExpression((UnaryBooleanExpression) expression, dataRow), Boolean.class, Comparable.class); } else if (expression instanceof BinaryArithmeticExpression) { return evaluateBinaryArithmeticExpression((BinaryArithmeticExpression) expression, dataRow); } else if (expression instanceof UnaryArithmeticExpression) { return evaluateUnaryArithmeticExpression((UnaryArithmeticExpression) expression, dataRow); } return evaluateConstant(expression); }  @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 testEvaluateMultipleAliasesExpression() { final String FIRST_ALIAS = "first"; final String SECOND_ALIAS = "second"; Map<String, List<String>> columnsMap = new HashMap<String, List<String>>() { { put( FIRST_ALIAS, Collections.singletonList(NUMERIC_COLUMN_NAME) ); put( SECOND_ALIAS, Collections.singletonList(NUMERIC_COLUMN_NAME) ); } }; final DataContainer DATA_CONTAINER = new DataContainer(columnsMap); final DataRow DATA_ROW = new DataRow(DATA_CONTAINER, Arrays.asList(1, 2)); Object firstValue = expressionEvaluator.evaluate(column(NUMERIC_COLUMN_NAME, FIRST_ALIAS), DATA_ROW); assertThat(firstValue, equalTo(1)); Object secondValue = expressionEvaluator.evaluate(column(NUMERIC_COLUMN_NAME, SECOND_ALIAS), DATA_ROW); assertThat(secondValue, equalTo(2)); }
@Test public void testEvaluateFunctionExpressionWithSimpleArgs() { Object value = expressionEvaluator.evaluate(function(FUNCTION_NAME, Collections.singletonList(-1)), EMPTY_ROW); assertThat(value, is(instanceOf(Double.class))); assertThat(value, equalTo(1d)); }
@Test public void testEvaluateFunctionExpressionWithExpressionArgs() { Object value = expressionEvaluator.evaluate(absFunction(), ROW_WITH_VALUES); assertThat(value, is(instanceOf(Double.class))); assertThat(value, equalTo(3d)); }
@Test public void testEvaluateInteger() { final int VALUE = 1; assertThat(expressionEvaluator.evaluate(VALUE, EMPTY_ROW), equalTo(VALUE)); }
@Test public void testEvaluateString() { final String VALUE = "value"; assertThat(expressionEvaluator.evaluate(VALUE, EMPTY_ROW), equalTo(VALUE)); }
@Test public void testEvaluateFloat() { final float VALUE = 1.0f; assertThat(expressionEvaluator.evaluate(VALUE, EMPTY_ROW), equalTo(VALUE)); }