// Path: hydra-composer/src/main/java/com/flipkart/hydra/composer/exception/ComposerEvaluationException.java
// public class ComposerEvaluationException extends Exception {
// 
//     public ComposerEvaluationException() {
//         super();
//     }
// 
//     public ComposerEvaluationException(String message) {
//         super(message);
//     }
// 
//     public ComposerEvaluationException(String message, Throwable cause) {
//         super(message, cause);
//     }
// 
//     public ComposerEvaluationException(Throwable cause) {
//         super(cause);
//     }
// }
// 
// Path: hydra-composer/src/main/java/com/flipkart/hydra/composer/exception/ComposerInstantiationException.java
// public class ComposerInstantiationException extends Exception {
// 
//     public ComposerInstantiationException() {
//         super();
//     }
// 
//     public ComposerInstantiationException(String message) {
//         super(message);
//     }
// 
//     public ComposerInstantiationException(String message, Throwable cause) {
//         super(message, cause);
//     }
// 
//     public ComposerInstantiationException(Throwable cause) {
//         super(cause);
//     }
// }
// 
// Path: hydra-expression/src/main/java/com/flipkart/hydra/expression/Expression.java
// public interface Expression {
// 
//     public Object calculate(Map<String, Object> values) throws ExpressionEvaluationException;
// 
//     public List<String> getDependencies();
// }
// 
// Path: hydra-composer/src/main/java/com/flipkart/hydra/composer/utils/CompositionEvaluator.java
// public static Object evaluate(Object context, Map<String, Object> values) throws ComposerEvaluationException {
//     if (context instanceof Expression) {
//         try {
//             return ((Expression) context).calculate(values);
//         } catch (ExpressionEvaluationException e) {
//             throw new ComposerEvaluationException("Unable to evaluate composer.", e);
//         }
//     }
// 
//     if (context instanceof Map) {
//         Map mapContext = (Map) context;
//         Map<Object, Object> newMapContext = new HashMap<>();
//         for (Object key : mapContext.keySet()) {
//             newMapContext.put(evaluate(key, values), evaluate(mapContext.get(key), values));
//         }
//         context = newMapContext;
//     } else if (context instanceof List) {
//         List listContext = (List) context;
//         List<Object> newListContext = new ArrayList<>();
//         for (Object value : listContext) {
//             newListContext.add(evaluate(value, values));
//         }
//         context = newListContext;
//     }
// 
//     return context;
// }
// 
// Path: hydra-composer/src/main/java/com/flipkart/hydra/composer/utils/CompositionParser.java
// public static Object parse(Object context) throws ComposerInstantiationException {
//     if (context instanceof String) {
//         String stringContext = (String) context;
//         if (stringContext.startsWith("{{") && stringContext.endsWith("}}")) {
//             String varString = stringContext.substring(2, stringContext.length() - 2);
//             try {
//                 return new DefaultExpression(varString);
//             } catch (ExpressionParseException e) {
//                 throw new ComposerInstantiationException("Unable to instantiate composer.", e);
//             }
//         }
//     }
// 
//     if (context instanceof Map) {
//         Map mapContext = (Map) context;
//         Map<Object, Object> newMapContext = new HashMap<>();
//         for (Object key : mapContext.keySet()) {
//             newMapContext.put(parse(key), parse(mapContext.get(key)));
//         }
//         context = newMapContext;
//     } else if (context instanceof List) {
//         List listContext = (List) context;
//         List<Object> newListContext = new ArrayList<>();
//         for (Object value : listContext) {
//             newListContext.add(parse(value));
//         }
//         context = newListContext;
//     }
// 
//     return context;
// }


import com.flipkart.hydra.composer.exception.ComposerEvaluationException;
import com.flipkart.hydra.composer.exception.ComposerInstantiationException;
import com.flipkart.hydra.expression.Expression;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static com.flipkart.hydra.composer.utils.CompositionEvaluator.evaluate;
import static com.flipkart.hydra.composer.utils.CompositionParser.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.composer;




public class DefaultComposer implements Composer {

    private final Object context;
    private final List<String> dependencies;

    public DefaultComposer(Object context) throws ComposerInstantiationException {
        this(context, false);
    }

    public DefaultComposer(Object context, boolean isAlreadyParsed) throws ComposerInstantiationException {
        this.context = isAlreadyParsed ? context : parse(context);
        this.dependencies = findDependencies(this.context);
    }

    @Override
// Path: hydra-composer/src/main/java/com/flipkart/hydra/composer/exception/ComposerEvaluationException.java
// public class ComposerEvaluationException extends Exception {
// 
//     public ComposerEvaluationException() {
//         super();
//     }
// 
//     public ComposerEvaluationException(String message) {
//         super(message);
//     }
// 
//     public ComposerEvaluationException(String message, Throwable cause) {
//         super(message, cause);
//     }
// 
//     public ComposerEvaluationException(Throwable cause) {
//         super(cause);
//     }
// }
// 
// Path: hydra-composer/src/main/java/com/flipkart/hydra/composer/exception/ComposerInstantiationException.java
// public class ComposerInstantiationException extends Exception {
// 
//     public ComposerInstantiationException() {
//         super();
//     }
// 
//     public ComposerInstantiationException(String message) {
//         super(message);
//     }
// 
//     public ComposerInstantiationException(String message, Throwable cause) {
//         super(message, cause);
//     }
// 
//     public ComposerInstantiationException(Throwable cause) {
//         super(cause);
//     }
// }
// 
// Path: hydra-expression/src/main/java/com/flipkart/hydra/expression/Expression.java
// public interface Expression {
// 
//     public Object calculate(Map<String, Object> values) throws ExpressionEvaluationException;
// 
//     public List<String> getDependencies();
// }
// 
// Path: hydra-composer/src/main/java/com/flipkart/hydra/composer/utils/CompositionEvaluator.java
// public static Object evaluate(Object context, Map<String, Object> values) throws ComposerEvaluationException {
//     if (context instanceof Expression) {
//         try {
//             return ((Expression) context).calculate(values);
//         } catch (ExpressionEvaluationException e) {
//             throw new ComposerEvaluationException("Unable to evaluate composer.", e);
//         }
//     }
// 
//     if (context instanceof Map) {
//         Map mapContext = (Map) context;
//         Map<Object, Object> newMapContext = new HashMap<>();
//         for (Object key : mapContext.keySet()) {
//             newMapContext.put(evaluate(key, values), evaluate(mapContext.get(key), values));
//         }
//         context = newMapContext;
//     } else if (context instanceof List) {
//         List listContext = (List) context;
//         List<Object> newListContext = new ArrayList<>();
//         for (Object value : listContext) {
//             newListContext.add(evaluate(value, values));
//         }
//         context = newListContext;
//     }
// 
//     return context;
// }
// 
// Path: hydra-composer/src/main/java/com/flipkart/hydra/composer/utils/CompositionParser.java
// public static Object parse(Object context) throws ComposerInstantiationException {
//     if (context instanceof String) {
//         String stringContext = (String) context;
//         if (stringContext.startsWith("{{") && stringContext.endsWith("}}")) {
//             String varString = stringContext.substring(2, stringContext.length() - 2);
//             try {
//                 return new DefaultExpression(varString);
//             } catch (ExpressionParseException e) {
//                 throw new ComposerInstantiationException("Unable to instantiate composer.", e);
//             }
//         }
//     }
// 
//     if (context instanceof Map) {
//         Map mapContext = (Map) context;
//         Map<Object, Object> newMapContext = new HashMap<>();
//         for (Object key : mapContext.keySet()) {
//             newMapContext.put(parse(key), parse(mapContext.get(key)));
//         }
//         context = newMapContext;
//     } else if (context instanceof List) {
//         List listContext = (List) context;
//         List<Object> newListContext = new ArrayList<>();
//         for (Object value : listContext) {
//             newListContext.add(parse(value));
//         }
//         context = newListContext;
//     }
// 
//     return context;
// }


// Path: hydra-composer/src/main/java/com/flipkart/hydra/composer/DefaultComposer.java
import com.flipkart.hydra.composer.exception.ComposerEvaluationException;
import com.flipkart.hydra.composer.exception.ComposerInstantiationException;
import com.flipkart.hydra.expression.Expression;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static com.flipkart.hydra.composer.utils.CompositionEvaluator.evaluate;
import static com.flipkart.hydra.composer.utils.CompositionParser.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.composer;




public class DefaultComposer implements Composer {

    private final Object context;
    private final List<String> dependencies;

    public DefaultComposer(Object context) throws ComposerInstantiationException {
        this(context, false);
    }

    public DefaultComposer(Object context, boolean isAlreadyParsed) throws ComposerInstantiationException {
        this.context = isAlreadyParsed ? context : parse(context);
        this.dependencies = findDependencies(this.context);
    }

    @Override
    public Object compose(Map<String, Object> values) throws ComposerEvaluationException {