package org.pentaho.reporting.libraries.formula.function.math;
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;
import java.math.BigDecimal;
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;
}
}
}