<MyMathCalcPlugin Project>/src/org/myorg/mymathcalcplugin/ComputeAverageAction.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.myorg.mymathcalcplugin;

import java.text.MessageFormat;
import org.openide.util.NbBundle;

/**
 *
 */
public class ComputeAverageAction extends AbstractComputationAction {

    private static final MessageFormat RESULT_FORMAT = new MessageFormat(NbBundle.getMessage(ComputeSumAction.class, "MSG_AvgResult"));

    public ComputeAverageAction() {
        super(new AverageComputer());
    }

    @Override
    public String getName() {
        return NbBundle.getMessage(ComputeAverageAction.class, "NAME_Avg");
    }

    private static class AverageComputer extends AbstractComputationPerformer {
        private double sum = 0;

        @Override
        public void reset() {
            super.reset();
            sum = 0;
        }

        @Override
        public void addValue(Object value) {
            super.addValue(value);
            Number n = (Number) value;
            sum += n.doubleValue();
        }

        @Override
        public String getResultDisplayMessage() {
            return RESULT_FORMAT.format(new Object[] { getCount(), getResult() } );
        }

        @Override
        public Object getResult() {
            return sum / getCount();
        }

        @Override
        public String getProgressName() {
            return NbBundle.getMessage(ComputeAverageAction.class, "MSG_ProgressComputingAvg");
        }
    }
}