// 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);
//     }
// }


import com.flipkart.hydra.expression.exception.ExpressionEvaluationException;
import com.google.common.base.Joiner;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/*
 * 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.utils;



public class ExpressionEvaluator {

    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/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/utils/ExpressionEvaluator.java
import com.flipkart.hydra.expression.exception.ExpressionEvaluationException;
import com.google.common.base.Joiner;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/*
 * 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.utils;



public class ExpressionEvaluator {

    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;
    }

    private static Object evaluateFunction(String fn, List args, Map<String, Object> values) throws ExpressionEvaluationException {