/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package reportgen.math;
import reportgen.math.complex.MathExpressionComplex;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import reportgen.utils.ReportException;
import reportgen.math.agregate.agregate.AggregateFunction;
import reportgen.math.agregate.agregate.Aggregate;
/**
*
* @author axe
*/
public class ExpressionCalculator {
/**
*
* @return
* @throws reportgen.exception.ReportException
*/
public static Object getValue(List<Map> models, Map extraModel, MathExpressionOperand exp,
AggregateFunction function) throws ReportException {
if(exp instanceof MathExpressionComplex
&& ((MathExpressionComplex)exp).isEmpty()) {
return null;
}
if(models.size() == 0 && extraModel == null) {
//cells can contain something like string constants and another
// elements independent on models
extraModel = new HashMap();
}
Aggregate calc = function.getCalculator();
if(calc == null) {
if(models.size() > 0) {
Map localModel = makeCompoundModel(models.get(0), extraModel);
return exp.getValue(localModel);
}
return exp.getValue(extraModel);
} else {
for(Map iModel :models) {
Map localModel = makeCompoundModel(iModel, extraModel);
Object iVal = exp.getValue(localModel);
calc.add(iVal);
}
return calc.calculate();
}
}
/**
* сливает 2 модели данных в одну
* @param main
* @param extra
* @return
*/
public static Map makeCompoundModel(Map main, Map extra) {
Map res = main;
if(extra != null) {
res = new HashMap();
if(main != null) {
res.putAll(main);
}
res.putAll(extra);
}
return res;
}
}