package leigh.conflux.nodegraph;

import leigh.eo.EO;
import leigh.mecha.nodegraph.ProcessorState;

import java.io.Serializable;
import java.math.BigDecimal;

public class MaxNode extends AggregateNode {
    public static final String TYPE = "max";

    BigDecimal value;

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

    @Override
    public void start(ProcessorState state) {
        value = null;
    }

    @Override
    public void processRow(EO row, String key, ProcessorState state) throws Exception {
        BigDecimal x = row.getValueBigDecimal(key);

        if (x != null) {
            if (value == null)
                value = x;
            else if (value.compareTo(x) < 0)
                value = x;
        }
    }

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