Source for org.jfree.formula.function.math.SumFunction

   1: /**
   2:  * =========================================
   3:  * LibFormula : a free Java formula library
   4:  * =========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/libformula/
   7:  *
   8:  * (C) Copyright 2006-2007, by Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  *
  26:  * ------------
  27:  * $Id: SumFunction.java,v 1.10 2007/05/07 23:00:37 mimil Exp $
  28:  * ------------
  29:  * (C) Copyright 2006-2007, by Pentaho Corporation.
  30:  */
  31: package org.jfree.formula.function.math;
  32: 
  33: import java.math.BigDecimal;
  34: 
  35: import org.jfree.formula.EvaluationException;
  36: import org.jfree.formula.FormulaContext;
  37: import org.jfree.formula.LibFormulaErrorValue;
  38: import org.jfree.formula.function.Function;
  39: import org.jfree.formula.function.ParameterCallback;
  40: import org.jfree.formula.lvalues.TypeValuePair;
  41: import org.jfree.formula.typing.Type;
  42: import org.jfree.formula.typing.TypeConversionException;
  43: import org.jfree.formula.typing.coretypes.NumberType;
  44: 
  45: /**
  46:  * Creation-Date: 31.10.2006, 17:39:19
  47:  *
  48:  * @author Thomas Morgner
  49:  */
  50: public class SumFunction implements Function
  51: {
  52:   public static final BigDecimal ZERO = new BigDecimal(0);
  53: 
  54:   public SumFunction()
  55:   {
  56:   }
  57: 
  58:   public String getCanonicalName()
  59:   {
  60:     return "SUM";
  61:   }
  62: 
  63:   public TypeValuePair evaluate(final FormulaContext context,
  64:                                 final ParameterCallback parameters)
  65:       throws EvaluationException
  66:   {
  67:     BigDecimal computedResult = ZERO;
  68:     final int parameterCount = parameters.getParameterCount();
  69:     
  70:     if(parameterCount == 0)
  71:     {
  72:       throw new EvaluationException(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE);
  73:     }
  74: 
  75:     for (int paramIdx = 0; paramIdx < parameterCount; paramIdx++)
  76:     {
  77:       final Object value = parameters.getValue(paramIdx);
  78:       final Type type = parameters.getType(paramIdx);
  79:       if (value == null)
  80:       {
  81:         continue;
  82:       }
  83: 
  84:       try
  85:       {
  86:         final Number n = context.getTypeRegistry().convertToNumber(type, value);
  87:         computedResult = compute(n, computedResult);
  88:       }
  89:       catch(TypeConversionException tce)
  90:       {
  91:         // The sum function ignores invalid values ..
  92:       }
  93:     }
  94: 
  95:       //    TODO: Array not yet supported
  96:       /*final Type type = parameters.getType(paramIdx);
  97:       if (type.isFlagSet(Type.ARRAY_TYPE) == false)
  98:       {
  99:         continue;
 100:       }
 101:       if (type.isFlagSet(Type.NUMERIC_TYPE) == false)
 102:       {
 103:         continue;
 104:       }
 105: 
 106:       final Object[] values = (Object[]) value;
 107:       for (int arrayIdx = 0; arrayIdx < values.length; arrayIdx++)
 108:       {
 109:         computedResult = compute(values[arrayIdx], computedResult);
 110:       }
 111:     }*/
 112: 
 113:     return new TypeValuePair(NumberType.GENERIC_NUMBER, computedResult);
 114:   }
 115: 
 116:   private BigDecimal compute(final Object value,
 117:                              final BigDecimal computedResult)
 118:   {
 119:     if (value == null)
 120:     {
 121:       // no-op ..
 122:       return computedResult;
 123:     }
 124: 
 125:     if (value instanceof Number == false)
 126:     {
 127:       // Paranoid checks can be disabled later ..
 128:       throw new IllegalArgumentException(String.valueOf(value));
 129:     }
 130: 
 131:     if (value instanceof BigDecimal)
 132:     {
 133:       return computedResult.add((BigDecimal) value);
 134:     }
 135:     else
 136:     {
 137:       final Number n = (Number) value;
 138:       final BigDecimal nval = new BigDecimal(n.toString());
 139:       return computedResult.add(nval);
 140:     }
 141:   }
 142: }