package net.sf.jasperreports.engine.fill;

import java.math.BigDecimal;

class JRBigDecimalAverageIncrementer extends JRAbstractExtendedIncrementer {
  private static JRBigDecimalAverageIncrementer mainInstance = new JRBigDecimalAverageIncrementer();
  
  public static JRBigDecimalAverageIncrementer getInstance() {
    return mainInstance;
  }
  
  public Object increment(JRCalculable variable, Object expressionValue, AbstractValueProvider valueProvider) {
    if (expressionValue == null) {
      if (variable.isInitialized())
        return null; 
      return variable.getValue();
    } 
    BigDecimal countValue = (BigDecimal)valueProvider.getValue(variable.getHelperVariable((byte)0));
    BigDecimal sumValue = (BigDecimal)valueProvider.getValue(variable.getHelperVariable((byte)1));
    return sumValue.divide(countValue, 4);
  }
  
  public Object initialValue() {
    return JRBigDecimalIncrementerFactory.ZERO;
  }
}
