Support for the Trim function, and a little enhancement to the formula evaluation test

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@603233 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2007-12-11 13:03:53 +00:00
parent 387b770876
commit f6a7cb7483
3 changed files with 77 additions and 19 deletions

View File

@ -14,12 +14,62 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Created on May 15, 2005
*
*/
package org.apache.poi.hssf.record.formula.functions;
public class Trim extends NotImplementedFunction {
import org.apache.poi.hssf.record.formula.eval.BlankEval;
import org.apache.poi.hssf.record.formula.eval.ErrorEval;
import org.apache.poi.hssf.record.formula.eval.Eval;
import org.apache.poi.hssf.record.formula.eval.NumberEval;
import org.apache.poi.hssf.record.formula.eval.StringEval;
import org.apache.poi.hssf.record.formula.eval.StringValueEval;
import org.apache.poi.hssf.record.formula.eval.ValueEval;
/**
* An implementation of the TRIM function:
* Removes leading and trailing spaces from value if evaluated operand
* value is string.
* @author Manda Wilson < wilson at c bio dot msk cc dot org >
*/
public class Trim extends TextFunction {
/**
* Removes leading and trailing spaces from value if evaluated
* operand value is string.
* Returns StringEval only if evaluated operand is of type string
* (and is not blank or null) or number. If evaluated operand is
* of type string and is blank or null, or if evaluated operand is
* of type blank, returns BlankEval. Otherwise returns ErrorEval.
*
* @see org.apache.poi.hssf.record.formula.eval.Eval
*/
public Eval evaluate(Eval[] operands, int srcCellRow, short srcCellCol) {
Eval retval = ErrorEval.VALUE_INVALID;
String str = null;
switch (operands.length) {
default:
break;
case 1:
ValueEval veval = singleOperandEvaluate(operands[0], srcCellRow, srcCellCol);
if (veval instanceof StringValueEval) {
StringValueEval sve = (StringValueEval) veval;
str = sve.getStringValue();
if (str == null || str.trim().equals("")) {
return BlankEval.INSTANCE;
}
}
else if (veval instanceof NumberEval) {
NumberEval neval = (NumberEval) veval;
str = neval.getStringValue();
}
else if (veval instanceof BlankEval) {
return BlankEval.INSTANCE;
}
}
if (str != null) {
retval = new StringEval(str.trim());
}
return retval;
}
}

View File

@ -30,20 +30,28 @@ public class TestEverything extends TestSuite {
public static TestSuite suite() throws Exception {
TestSuite suite = new TestSuite("Tests for OperationEval concrete implementation classes.");
suite.addTest(new GenericFormulaTestCase("D23"));
suite.addTest(new GenericFormulaTestCase("D27"));
suite.addTest(new GenericFormulaTestCase("D31"));
suite.addTest(new GenericFormulaTestCase("D35"));
suite.addTest(new GenericFormulaTestCase("D39"));
suite.addTest(new GenericFormulaTestCase("D43"));
suite.addTest(new GenericFormulaTestCase("D47"));
suite.addTest(new GenericFormulaTestCase("D51"));
suite.addTest(new GenericFormulaTestCase("D55"));
suite.addTest(new GenericFormulaTestCase("D59"));
suite.addTest(new GenericFormulaTestCase("D63"));
suite.addTest(new GenericFormulaTestCase("D67"));
suite.addTest(new GenericFormulaTestCase("D71"));
suite.addTest(new GenericFormulaTestCase("D75"));
suite.addTest(new GenericFormulaTestCase("D23")); // Add
suite.addTest(new GenericFormulaTestCase("D27")); // ConcatEval
suite.addTest(new GenericFormulaTestCase("D31")); // DivideEval
suite.addTest(new GenericFormulaTestCase("D35")); // EqualEval
suite.addTest(new GenericFormulaTestCase("D39")); // GreaterEqualEval
suite.addTest(new GenericFormulaTestCase("D43")); // GreaterThanEval
suite.addTest(new GenericFormulaTestCase("D47")); // LessEqualEval
suite.addTest(new GenericFormulaTestCase("D51")); // LessThanEval
suite.addTest(new GenericFormulaTestCase("D55")); // MultiplyEval
suite.addTest(new GenericFormulaTestCase("D59")); // NotEqualEval
suite.addTest(new GenericFormulaTestCase("D63")); // PowerEval
suite.addTest(new GenericFormulaTestCase("D67")); // SubtractEval
suite.addTest(new GenericFormulaTestCase("D71")); // UnaryMinusEval
suite.addTest(new GenericFormulaTestCase("D75")); // UnaryPlusEval
suite.addTest(new GenericFormulaTestCase("D249")); // Concatenate
suite.addTest(new GenericFormulaTestCase("D741")); // Int
suite.addTest(new GenericFormulaTestCase("D1393")); // Trim
suite.addTest(new GenericFormulaTestCase("D1421")); // Upper
// Add newly implemented formula functions here
return suite;
}
}