final ParameterCallback parameters) throws EvaluationException
{
final int parameterCount = parameters.getParameterCount();
if (parameterCount < 1 || parameterCount > 2)
{
throw new EvaluationException(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE);
}
final TypeRegistry typeRegistry = context.getTypeRegistry();
final Type textType = parameters.getType(0);
final Object textValue = parameters.getValue(0);
final String text = typeRegistry.convertToText(textType, textValue);
final int length;
if (parameterCount == 2)
{
final Type lengthType = parameters.getType(1);
final Object lengthValue = parameters.getValue(1);
final Number lengthConv = typeRegistry.convertToNumber(lengthType, lengthValue);
if (lengthConv.doubleValue() < 0)
{
throw new EvaluationException(LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
}
length = lengthConv.intValue();
}
else
{
length = 1;
}
if (text == null)
{
throw new EvaluationException(LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
}
// Note that MID(T;1;Length) produces the same results as LEFT(T;Length).
return new TypeValuePair(TextType.TYPE, MidFunction.process(text, 1, length));
}