001/*
002 * Copyright (c) 2005 Einar Pehrson <einar@pehrson.nu>.
003 *
004 * This file is part of
005 * CleanSheets - a spreadsheet application for the Java platform.
006 *
007 * CleanSheets is free software; you can redistribute it and/or modify
008 * it under the terms of the GNU General Public License as published by
009 * the Free Software Foundation; either version 2 of the License, or
010 * (at your option) any later version.
011 *
012 * CleanSheets is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015 * GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU General Public License
018 * along with CleanSheets; if not, write to the Free Software
019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
020 */
021package csheets.core.formula.lang;
022
023import csheets.core.IllegalValueTypeException;
024import csheets.core.Value;
025import csheets.core.formula.Expression;
026import csheets.core.formula.Function;
027import csheets.core.formula.FunctionParameter;
028
029/**
030 * A function that returns the numeric average of its arguments.
031 * @author Einar Pehrson
032 */
033public class Average implements Function {
034
035        /** The only (but repeatable) parameter: a numeric term */
036        public static final FunctionParameter[] parameters = new FunctionParameter[] {
037                new FunctionParameter(Value.Type.NUMERIC, "Term", false,
038                        "A number to be included in the average")
039        };
040
041        /**
042         * Creates a new instance of the AVERAGE function.
043         */
044        public Average() {}
045
046        public String getIdentifier() {
047                return "AVERAGE";
048        }
049
050        public Value applyTo(Expression[] arguments) throws IllegalValueTypeException {
051                double count = 0;
052                double sum = 0;
053                for (Expression expression : arguments) {
054                        Value value = expression.evaluate();
055                        if (value.getType() == Value.Type.NUMERIC) {
056                                count++;
057                                sum += value.toDouble();
058                        } else if (value.getType() == Value.Type.MATRIX)
059                                for (Value[] vector : value.toMatrix()) {
060                                        for (Value item : vector)
061                                                if (item.getType() == Value.Type.NUMERIC) {
062                                                        count++;
063                                                        sum += item.toDouble();
064                                                } else
065                                                        throw new IllegalValueTypeException(item, Value.Type.NUMERIC);
066                        } else
067                                throw new IllegalValueTypeException(value, Value.Type.NUMERIC);
068                }
069                return new Value(sum / count);
070        }
071
072        public FunctionParameter[] getParameters() {
073                return parameters;
074        }
075
076        public boolean isVarArg() {
077                return true;
078        }
079}