1:
31: package ;
32:
33: import ;
34: import ;
35: import ;
36: import ;
37: import ;
38: import ;
39: import ;
40: import ;
41: import ;
42:
43:
49: public class MidFunction implements Function
50: {
51: public MidFunction()
52: {
53: }
54:
55: public TypeValuePair evaluate(final FormulaContext context,
56: final ParameterCallback parameters) throws EvaluationException
57: {
58: final int parameterCount = parameters.getParameterCount();
59: if (parameterCount != 3)
60: {
61: throw new EvaluationException(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE);
62: }
63: final TypeRegistry typeRegistry = context.getTypeRegistry();
64:
65: final Type textType = parameters.getType(0);
66: final Object textValue = parameters.getValue(0);
67: final Type startType = parameters.getType(1);
68: final Object startValue = parameters.getValue(1);
69: final Type lengthType = parameters.getType(2);
70: final Object lengthValue = parameters.getValue(2);
71:
72: final String text = typeRegistry.convertToText(textType, textValue);
73: final Number start = typeRegistry.convertToNumber(startType, startValue);
74: final Number length = typeRegistry.convertToNumber(lengthType, lengthValue);
75:
76: if(length.doubleValue() < 0 || start.doubleValue() < 1)
77: {
78: throw new EvaluationException(LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
79: }
80:
81: return new TypeValuePair(TextType.TYPE, process(text, start, length));
82: }
83:
84: public String process(final String text, final Number start, final Number length)
85: {
86: int lengthValue = length.intValue();
87: final int startValue = start.intValue()-1;
88: if(startValue >= text.length())
89: {
90: return "";
91: }
92:
93: if((lengthValue +startValue) > text.length())
94: {
95: lengthValue = text.length()-startValue;
96: }
97:
98: return text.substring(startValue, startValue +lengthValue);
99: }
100:
101: public String getCanonicalName()
102: {
103: return "MID";
104: }
105: