// Path: hydra-expression/src/main/java/com/flipkart/hydra/expression/exception/ExpressionEvaluationException.java
// public class ExpressionEvaluationException extends Exception {
// 
//     public ExpressionEvaluationException() {
//         super();
//     }
// 
//     public ExpressionEvaluationException(String message) {
//         super(message);
//     }
// 
//     public ExpressionEvaluationException(String expected, String found) {
//         super("Expected " + expected + ", Found " + found);
//     }
// 
//     public ExpressionEvaluationException(String message, Throwable cause) {
//         super(message, cause);
//     }
// 
//     public ExpressionEvaluationException(Throwable cause) {
//         super(cause);
//     }
// }
// 
// Path: hydra-expression/src/main/java/com/flipkart/hydra/expression/exception/ExpressionParseException.java
// public class ExpressionParseException extends Exception {
// 
//     public ExpressionParseException() {
//         super();
//     }
// 
//     public ExpressionParseException(String message) {
//         super(message);
//     }
// 
//     public ExpressionParseException(String expression, String expected, char found, int position) {
//         this(expression, expected, "" + found, position);
//     }
// 
//     public ExpressionParseException(String expression, String expected, String found, int position) {
//         super("Expected " + expected + ", Found '" + found + "' at position " + position + ". (Expression = " + expression + ")");
//     }
// 
//     public ExpressionParseException(String expression, String subExpression, int position) {
//         super("Unable to parse sub-expression '" + subExpression + "' starting at position " + position + ". (Expression = " + expression + ")");
//     }
// 
//     public ExpressionParseException(String message, Throwable cause) {
//         super(message, cause);
//     }
// 
//     public ExpressionParseException(Throwable cause) {
//         super(cause);
//     }
// }
// 
// Path: hydra-expression/src/main/java/com/flipkart/hydra/expression/utils/ExpressionEvaluator.java
// public static Object evaluate(List operands, Map<String, Object> values) throws ExpressionEvaluationException {
//     Object response = null;
// 
//     Object firstOperand = operands.get(0);
//     if (firstOperand instanceof String) {
//         if (((String) firstOperand).endsWith("()")) {
//             List arguments = operands.size() > 1 ? operands.subList(1, operands.size()) : new ArrayList();
//             return evaluateFunction((String) firstOperand, arguments, values);
//         }
//     }
// 
//     for (Object operand : operands) {
//         Object calculatedOperand = operand;
//         if (operand instanceof List) {
//             calculatedOperand = evaluate((List) operand, values);
//         }
// 
//         response = calculateOperandResponse(calculatedOperand, values, response);
//         if (response == null) {
//             break;
//         }
//     }
// 
//     return response;
// }
// 
// Path: hydra-expression/src/main/java/com/flipkart/hydra/expression/utils/ExpressionParser.java
// public static List parse(final String expression) throws ExpressionParseException {
//     if (expression.startsWith("$")) {
//         return parseExpression(expression);
//     } else {
//         return new ArrayList<Object>() {{
//             add(getValue(expression));
//         }};
//     }
// }


import com.flipkart.hydra.expression.exception.ExpressionEvaluationException;
import com.flipkart.hydra.expression.exception.ExpressionParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static com.flipkart.hydra.expression.utils.ExpressionEvaluator.evaluate;
import static com.flipkart.hydra.expression.utils.ExpressionParser.parse;
/*
 * Copyright 2015 Flipkart Internet, pvt ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flipkart.hydra.expression;




public class DefaultExpression implements Expression {

    private final String expression;
    private final List operands;
    private final List<String> dependencies;
    private final boolean isOptional;

    public DefaultExpression(String expression) throws ExpressionParseException {
        expression = expression.replaceAll("\\s", "");

        this.isOptional = expression.startsWith("#");
        this.expression = expression.substring(isOptional ? 1 : 0);
        this.operands = parse(this.expression);
        this.dependencies = findDependencies(operands);
    }

    @Override
    public Object calculate(Map<String, Object> values) throws ExpressionEvaluationException {
// Path: hydra-expression/src/main/java/com/flipkart/hydra/expression/exception/ExpressionEvaluationException.java
// public class ExpressionEvaluationException extends Exception {
// 
//     public ExpressionEvaluationException() {
//         super();
//     }
// 
//     public ExpressionEvaluationException(String message) {
//         super(message);
//     }
// 
//     public ExpressionEvaluationException(String expected, String found) {
//         super("Expected " + expected + ", Found " + found);
//     }
// 
//     public ExpressionEvaluationException(String message, Throwable cause) {
//         super(message, cause);
//     }
// 
//     public ExpressionEvaluationException(Throwable cause) {
//         super(cause);
//     }
// }
// 
// Path: hydra-expression/src/main/java/com/flipkart/hydra/expression/exception/ExpressionParseException.java
// public class ExpressionParseException extends Exception {
// 
//     public ExpressionParseException() {
//         super();
//     }
// 
//     public ExpressionParseException(String message) {
//         super(message);
//     }
// 
//     public ExpressionParseException(String expression, String expected, char found, int position) {
//         this(expression, expected, "" + found, position);
//     }
// 
//     public ExpressionParseException(String expression, String expected, String found, int position) {
//         super("Expected " + expected + ", Found '" + found + "' at position " + position + ". (Expression = " + expression + ")");
//     }
// 
//     public ExpressionParseException(String expression, String subExpression, int position) {
//         super("Unable to parse sub-expression '" + subExpression + "' starting at position " + position + ". (Expression = " + expression + ")");
//     }
// 
//     public ExpressionParseException(String message, Throwable cause) {
//         super(message, cause);
//     }
// 
//     public ExpressionParseException(Throwable cause) {
//         super(cause);
//     }
// }
// 
// Path: hydra-expression/src/main/java/com/flipkart/hydra/expression/utils/ExpressionEvaluator.java
// public static Object evaluate(List operands, Map<String, Object> values) throws ExpressionEvaluationException {
//     Object response = null;
// 
//     Object firstOperand = operands.get(0);
//     if (firstOperand instanceof String) {
//         if (((String) firstOperand).endsWith("()")) {
//             List arguments = operands.size() > 1 ? operands.subList(1, operands.size()) : new ArrayList();
//             return evaluateFunction((String) firstOperand, arguments, values);
//         }
//     }
// 
//     for (Object operand : operands) {
//         Object calculatedOperand = operand;
//         if (operand instanceof List) {
//             calculatedOperand = evaluate((List) operand, values);
//         }
// 
//         response = calculateOperandResponse(calculatedOperand, values, response);
//         if (response == null) {
//             break;
//         }
//     }
// 
//     return response;
// }
// 
// Path: hydra-expression/src/main/java/com/flipkart/hydra/expression/utils/ExpressionParser.java
// public static List parse(final String expression) throws ExpressionParseException {
//     if (expression.startsWith("$")) {
//         return parseExpression(expression);
//     } else {
//         return new ArrayList<Object>() {{
//             add(getValue(expression));
//         }};
//     }
// }


// Path: hydra-expression/src/main/java/com/flipkart/hydra/expression/DefaultExpression.java
import com.flipkart.hydra.expression.exception.ExpressionEvaluationException;
import com.flipkart.hydra.expression.exception.ExpressionParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static com.flipkart.hydra.expression.utils.ExpressionEvaluator.evaluate;
import static com.flipkart.hydra.expression.utils.ExpressionParser.parse;

/*
 * Copyright 2015 Flipkart Internet, pvt ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flipkart.hydra.expression;




public class DefaultExpression implements Expression {

    private final String expression;
    private final List operands;
    private final List<String> dependencies;
    private final boolean isOptional;

    public DefaultExpression(String expression) throws ExpressionParseException {
        expression = expression.replaceAll("\\s", "");

        this.isOptional = expression.startsWith("#");
        this.expression = expression.substring(isOptional ? 1 : 0);
        this.operands = parse(this.expression);
        this.dependencies = findDependencies(operands);
    }

    @Override
    public Object calculate(Map<String, Object> values) throws ExpressionEvaluationException {
        Object value = evaluate(operands, values);