package leigh.conflux.nodegraph;

import leigh.eo.EO;
import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;
import leigh.mecha.nodegraph.ProcessorState;

import java.math.BigDecimal;

/**
 * Implementation of a sum aggregation.
 *
 * @author Alex Leigh
 */
public class SumNode extends AggregateNode {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(SumNode.class);
    private BigDecimal value = BigDecimal.ZERO;

    public SumNode(String id) {
        super(id);
    }

    @Override
    public void start(ProcessorState state) {
        value = BigDecimal.ZERO;
    }

    public void processRow(EO row, String key, ProcessorState state) throws Exception {
        BigDecimal x = row.getValueBigDecimal(key);
        logger.debug("Processing row: [key: {}] [x: {}] {}", key, x, row);
        if (x != null)
            value = value.add(x);
    }

    @Override
    public BigDecimal finish(ProcessorState state) {
        return value;
    }
}
