package net.sf.jasperreports.engine.fill;

import java.math.BigDecimal;

class JRBigDecimalSumIncrementer extends JRAbstractExtendedIncrementer {
  private static JRBigDecimalSumIncrementer mainInstance = new JRBigDecimalSumIncrementer();
  
  public static JRBigDecimalSumIncrementer getInstance() {
    return mainInstance;
  }
  
  public Object increment(JRCalculable variable, Object expressionValue, AbstractValueProvider valueProvider) {
    BigDecimal value = (BigDecimal)variable.getIncrementedValue();
    BigDecimal newValue = (BigDecimal)expressionValue;
    if (newValue == null) {
      if (variable.isInitialized())
        return null; 
      return value;
    } 
    if (value == null || variable.isInitialized())
      value = JRBigDecimalIncrementerFactory.ZERO; 
    return value.add(newValue);
  }
  
  public Object initialValue() {
    return JRBigDecimalIncrementerFactory.ZERO;
  }
}
