Package org.pentaho.reporting.libraries.formula.function.math

Source Code of org.pentaho.reporting.libraries.formula.function.math.NFunction

package org.pentaho.reporting.libraries.formula.function.math;

import java.math.BigDecimal;

import org.pentaho.reporting.libraries.formula.EvaluationException;
import org.pentaho.reporting.libraries.formula.FormulaContext;
import org.pentaho.reporting.libraries.formula.LibFormulaErrorValue;
import org.pentaho.reporting.libraries.formula.function.Function;
import org.pentaho.reporting.libraries.formula.function.ParameterCallback;
import org.pentaho.reporting.libraries.formula.lvalues.TypeValuePair;
import org.pentaho.reporting.libraries.formula.typing.Type;
import org.pentaho.reporting.libraries.formula.typing.coretypes.NumberType;

public class NFunction implements Function
{
  private static final TypeValuePair ZERO = new TypeValuePair(NumberType.GENERIC_NUMBER, BigDecimal.ZERO);

  public NFunction()
  {
  }

  public String getCanonicalName()
  {
    return "N";
  }

  public TypeValuePair evaluate(final FormulaContext context,
                                final ParameterCallback parameters) throws EvaluationException
  {
    final int parameterCount = parameters.getParameterCount();
    if (parameterCount < 1)
    {
      throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE);
    }
    final Type type1 = parameters.getType(0);
    final Object value1 = parameters.getValue(0);


    try
    {
      return new TypeValuePair(NumberType.GENERIC_NUMBER, context.getTypeRegistry().convertToNumber(type1, value1));
    }
    catch (EvaluationException e)
    {
      return ZERO;
    }
  }
}
TOP

Related Classes of org.pentaho.reporting.libraries.formula.function.math.NFunction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.