001 /**
002 * =========================================
003 * LibFormula : a free Java formula library
004 * =========================================
005 *
006 * Project Info: http://reporting.pentaho.org/libformula/
007 *
008 * (C) Copyright 2006-2007, by Pentaho Corporation and Contributors.
009 *
010 * This library is free software; you can redistribute it and/or modify it under the terms
011 * of the GNU Lesser General Public License as published by the Free Software Foundation;
012 * either version 2.1 of the License, or (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016 * See the GNU Lesser General Public License for more details.
017 *
018 * You should have received a copy of the GNU Lesser General Public License along with this
019 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020 * Boston, MA 02111-1307, USA.
021 *
022 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023 * in the United States and other countries.]
024 *
025 *
026 * ------------
027 * $Id: SumFunction.java 3186 2007-08-15 16:54:01Z mimil $
028 * ------------
029 * (C) Copyright 2006-2007, by Pentaho Corporation.
030 */
031 package org.jfree.formula.function.math;
032
033 import org.jfree.formula.EvaluationException;
034 import org.jfree.formula.FormulaContext;
035 import org.jfree.formula.LibFormulaErrorValue;
036 import org.jfree.formula.function.Function;
037 import org.jfree.formula.function.ParameterCallback;
038 import org.jfree.formula.lvalues.TypeValuePair;
039 import org.jfree.formula.typing.Type;
040 import org.jfree.formula.typing.coretypes.NumberType;
041 import org.jfree.formula.typing.sequence.NumberSequence;
042 import org.jfree.formula.util.NumberUtil;
043
044 import java.math.BigDecimal;
045
046 /**
047 * Creation-Date: 31.10.2006, 17:39:19
048 *
049 * @author Thomas Morgner
050 */
051 public class SumFunction implements Function
052 {
053 public static final BigDecimal ZERO = new BigDecimal(0);
054
055 public SumFunction()
056 {
057 }
058
059 public String getCanonicalName()
060 {
061 return "SUM";
062 }
063
064 public TypeValuePair evaluate(final FormulaContext context,
065 final ParameterCallback parameters)
066 throws EvaluationException
067 {
068 BigDecimal computedResult = ZERO;
069 final int parameterCount = parameters.getParameterCount();
070
071 if (parameterCount == 0)
072 {
073 throw new EvaluationException(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE);
074 }
075
076 for (int paramIdx = 0; paramIdx < parameterCount; paramIdx++)
077 {
078 final Type type = parameters.getType(paramIdx);
079 final Object value = parameters.getValue(paramIdx);
080 final NumberSequence sequence = context.getTypeRegistry().convertToNumberSequence(type, value);
081
082 while (sequence.hasNext())
083 {
084 computedResult = compute(sequence.nextNumber(), computedResult);
085 }
086 }
087
088 return new TypeValuePair(NumberType.GENERIC_NUMBER, computedResult);
089 }
090
091 private BigDecimal compute(final Number value,
092 final BigDecimal computedResult) throws EvaluationException
093 {
094 if (value == null)
095 {
096 // no-op ..
097 return computedResult;
098 }
099
100 return computedResult.add(NumberUtil.getAsBigDecimal(value));
101 }
102 }