initial work on removing obsolete Eval interface

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@805091 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Josh Micich 2009-08-17 18:55:05 +00:00
parent f4c028c5a3
commit 6d65572fa8
26 changed files with 147 additions and 156 deletions

View File

@ -35,12 +35,12 @@ public final class AnalysisToolPak {
_functionName = functionName;
}
public ValueEval evaluate(Eval[] args, EvaluationWorkbook workbook, int srcCellSheet,
public ValueEval evaluate(ValueEval[] args, EvaluationWorkbook workbook, int srcCellSheet,
int srcCellRow, int srcCellCol) {
throw new NotImplementedException(_functionName);
}
};
private static Map<String, FreeRefFunction> _functionsByName = createFunctionsMap();
private AnalysisToolPak() {
@ -50,7 +50,7 @@ public final class AnalysisToolPak {
public static FreeRefFunction findFunction(String name) {
return _functionsByName.get(name);
}
private static Map<String, FreeRefFunction> createFunctionsMap() {
Map<String, FreeRefFunction> m = new HashMap<String, FreeRefFunction>(100);
@ -147,7 +147,7 @@ public final class AnalysisToolPak {
r(m, "YIELD", null);
r(m, "YIELDDISC", null);
r(m, "YIELDMAT", null);
return m;
}

View File

@ -27,7 +27,7 @@ import org.apache.poi.hssf.record.formula.functions.FreeRefFunction;
import org.apache.poi.ss.formula.EvaluationWorkbook;
/**
* Implementation of Excel 'Analysis ToolPak' function ISEVEN() ISODD()<br/>
*
*
* @author Josh Micich
*/
final class ParityFunction implements FreeRefFunction {
@ -35,17 +35,17 @@ final class ParityFunction implements FreeRefFunction {
public static final FreeRefFunction IS_EVEN = new ParityFunction(0);
public static final FreeRefFunction IS_ODD = new ParityFunction(1);
private final int _desiredParity;
private ParityFunction(int desiredParity) {
_desiredParity = desiredParity;
}
public ValueEval evaluate(Eval[] args, EvaluationWorkbook workbook, int srcCellSheet, int srcCellRow,
public ValueEval evaluate(ValueEval[] args, EvaluationWorkbook workbook, int srcCellSheet, int srcCellRow,
int srcCellCol) {
if (args.length != 1) {
return ErrorEval.VALUE_INVALID;
return ErrorEval.VALUE_INVALID;
}
int val;
try {
val = evaluateArgParity(args[0], srcCellRow, srcCellCol);
@ -58,7 +58,7 @@ final class ParityFunction implements FreeRefFunction {
private static int evaluateArgParity(Eval arg, int srcCellRow, int srcCellCol) throws EvaluationException {
ValueEval ve = OperandResolver.getSingleValue(arg, srcCellRow, (short)srcCellCol);
double d = OperandResolver.coerceValueToDouble(ve);
if (d < 0) {
d = -d;

View File

@ -33,14 +33,14 @@ import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.formula.EvaluationWorkbook;
/**
* Implementation of Excel 'Analysis ToolPak' function YEARFRAC()<br/>
*
*
* Returns the fraction of the year spanned by two dates.<p/>
*
*
* <b>Syntax</b><br/>
* <b>YEARFRAC</b>(<b>startDate</b>, <b>endDate</b>, basis)<p/>
*
*
* The <b>basis</b> optionally specifies the behaviour of YEARFRAC as follows:
*
*
* <table border="0" cellpadding="1" cellspacing="0" summary="basis parameter description">
* <tr><th>Value</th><th>Days per Month</th><th>Days per Year</th></tr>
* <tr align='center'><td>0 (default)</td><td>30</td><td>360</td></tr>
@ -49,17 +49,17 @@ import org.apache.poi.ss.formula.EvaluationWorkbook;
* <tr align='center'><td>3</td><td>actual</td><td>365</td></tr>
* <tr align='center'><td>4</td><td>30</td><td>360</td></tr>
* </table>
*
*
*/
final class YearFrac implements FreeRefFunction {
public static final FreeRefFunction instance = new YearFrac();
private YearFrac() {
// enforce singleton
}
public ValueEval evaluate(Eval[] args, EvaluationWorkbook workbook, int srcCellSheet, int srcCellRow,
public ValueEval evaluate(ValueEval[] args, EvaluationWorkbook workbook, int srcCellSheet, int srcCellRow,
int srcCellCol) {
double result;
@ -71,7 +71,7 @@ final class YearFrac implements FreeRefFunction {
case 2:
break;
default:
return ErrorEval.VALUE_INVALID;
return ErrorEval.VALUE_INVALID;
}
double startDateVal = evaluateDateArg(args[0], srcCellRow, srcCellCol);
double endDateVal = evaluateDateArg(args[1], srcCellRow, srcCellCol);
@ -79,7 +79,7 @@ final class YearFrac implements FreeRefFunction {
} catch (EvaluationException e) {
return e.getErrorEval();
}
return new NumberEval(result);
}
@ -123,7 +123,7 @@ final class YearFrac implements FreeRefFunction {
// easy to see this cannot be a valid date
throw new EvaluationException(ErrorEval.VALUE_INVALID);
}
if (f0 >= 1900 && f0 < 9999) {
// when 4 digit value appears first, the format is YYYY/MM/DD, regardless of OS settings
return makeDate(f0, f1, f2);

View File

@ -28,7 +28,7 @@ public final class ConcatEval implements OperationEval {
// enforce singleton
}
public Eval evaluate(Eval[] args, int srcRow, short srcCol) {
public ValueEval evaluate(ValueEval[] args, int srcRow, short srcCol) {
if(args.length != 2) {
return ErrorEval.VALUE_INVALID;
}

View File

@ -243,12 +243,12 @@ public final class FunctionEval implements OperationEval {
_delegate = funcPtg;
}
public Eval evaluate(Eval[] operands, int srcRow, short srcCol) {
public ValueEval evaluate(ValueEval[] operands, int srcRow, short srcCol) {
Function f = getFunction();
if (f == null) {
throw new NotImplementedException("FuncIx=" + getFunctionIndex());
}
return f.evaluate(operands, srcRow, srcCol);
return (ValueEval) f.evaluate(operands, srcRow, srcCol);
}
public int getNumberOfOperands() {

View File

@ -19,21 +19,21 @@ package org.apache.poi.hssf.record.formula.eval;
/**
* Common interface for implementations of Excel formula operations.
*
*
* @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt;
*
*
*/
public interface OperationEval {
/**
* @param args the evaluated operation arguments. Elements of this array typically implement
* {@link ValueEval}. Empty values are represented with {@link BlankEval} or {@link
* @param args the evaluated operation arguments. Elements of this array typically implement
* {@link ValueEval}. Empty values are represented with {@link BlankEval} or {@link
* MissingArgEval}, never <code>null</code>.
* @param srcRowIndex row index of the cell containing the formula under evaluation
* @param srcColumnIndex column index of the cell containing the formula under evaluation
* @return The evaluated result, possibly an {@link ErrorEval}, never <code>null</code>.
*/
Eval evaluate(Eval[] args, int srcRowIndex, short srcColumnIndex);
ValueEval evaluate(ValueEval[] args, int srcRowIndex, short srcColumnIndex);
int getNumberOfOperands();
}

View File

@ -30,7 +30,7 @@ public final class PercentEval implements OperationEval {
// enforce singleton
}
public Eval evaluate(Eval[] args, int srcRow, short srcCol) {
public ValueEval evaluate(ValueEval[] args, int srcRow, short srcCol) {
if (args.length != 1) {
return ErrorEval.VALUE_INVALID;
}

View File

@ -19,18 +19,18 @@ package org.apache.poi.hssf.record.formula.eval;
/**
*
* @author Josh Micich
*
* @author Josh Micich
*/
public final class RangeEval implements OperationEval {
public static final OperationEval instance = new RangeEval();
private RangeEval() {
// enforces singleton
}
public Eval evaluate(Eval[] args, int srcCellRow, short srcCellCol) {
public ValueEval evaluate(ValueEval[] args, int srcCellRow, short srcCellCol) {
if(args.length != 2) {
return ErrorEval.VALUE_INVALID;
}
@ -45,18 +45,18 @@ public final class RangeEval implements OperationEval {
}
/**
* @return simple rectangular {@link AreaEval} which fully encloses both areas
* @return simple rectangular {@link AreaEval} which fully encloses both areas
* <tt>aeA</tt> and <tt>aeB</tt>
*/
private static AreaEval resolveRange(AreaEval aeA, AreaEval aeB) {
int aeAfr = aeA.getFirstRow();
int aeAfc = aeA.getFirstColumn();
int top = Math.min(aeAfr, aeB.getFirstRow());
int bottom = Math.max(aeA.getLastRow(), aeB.getLastRow());
int left = Math.min(aeAfc, aeB.getFirstColumn());
int right = Math.max(aeA.getLastColumn(), aeB.getLastColumn());
return aeA.offset(top-aeAfr, bottom-aeAfr, left-aeAfc, right-aeAfc);
}

View File

@ -54,7 +54,7 @@ public abstract class RelationalOperationEval implements OperationEval {
* Blank < Positive numbers
* </pre>
*/
public final Eval evaluate(Eval[] operands, int srcRow, short srcCol) {
public final ValueEval evaluate(ValueEval[] operands, int srcRow, short srcCol) {
if (operands.length != 2) {
return ErrorEval.VALUE_INVALID;
}

View File

@ -27,7 +27,7 @@ abstract class TwoOperandNumericOperation implements OperationEval {
return OperandResolver.coerceValueToDouble(ve);
}
public final Eval evaluate(Eval[] args, int srcCellRow, short srcCellCol) {
public final ValueEval evaluate(ValueEval[] args, int srcCellRow, short srcCellCol) {
double result;
try {
double d0 = singleOperandEvaluate(args[0], srcCellRow, srcCellCol);

View File

@ -30,7 +30,7 @@ public final class UnaryMinusEval implements OperationEval {
// enforce singleton
}
public Eval evaluate(Eval[] args, int srcRow, short srcCol) {
public ValueEval evaluate(ValueEval[] args, int srcRow, short srcCol) {
if (args.length != 1) {
return ErrorEval.VALUE_INVALID;
}

View File

@ -20,17 +20,17 @@ package org.apache.poi.hssf.record.formula.eval;
/**
* @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt;
*
*
*/
public final class UnaryPlusEval implements OperationEval {
public static final OperationEval instance = new UnaryPlusEval();
private UnaryPlusEval() {
// enforce singleton
}
public Eval evaluate(Eval[] args, int srcCellRow, short srcCellCol) {
public ValueEval evaluate(ValueEval[] args, int srcCellRow, short srcCellCol) {
if(args.length != 1) {
return ErrorEval.VALUE_INVALID;
}

View File

@ -36,7 +36,7 @@ final class UserDefinedFunction implements FreeRefFunction {
// enforce singleton
}
public ValueEval evaluate(Eval[] args, EvaluationWorkbook workbook,
public ValueEval evaluate(ValueEval[] args, EvaluationWorkbook workbook,
int srcCellSheet, int srcCellRow,int srcCellCol) {
int nIncomingArgs = args.length;
@ -55,7 +55,7 @@ final class UserDefinedFunction implements FreeRefFunction {
+ nameArg.getClass().getName() + ")");
}
int nOutGoingArgs = nIncomingArgs -1;
Eval[] outGoingArgs = new Eval[nOutGoingArgs];
ValueEval[] outGoingArgs = new ValueEval[nOutGoingArgs];
System.arraycopy(args, 1, outGoingArgs, 0, nOutGoingArgs);
return targetFunc.evaluate(outGoingArgs, workbook, srcCellSheet, srcCellRow, srcCellCol);
}

View File

@ -17,7 +17,6 @@
package org.apache.poi.hssf.record.formula.functions;
import org.apache.poi.hssf.record.formula.eval.Eval;
import org.apache.poi.hssf.record.formula.eval.ValueEval;
import org.apache.poi.ss.formula.EvaluationWorkbook;
@ -52,5 +51,5 @@ public interface FreeRefFunction {
* a specified Excel error (Exceptions are never thrown to represent Excel errors).
*
*/
ValueEval evaluate(Eval[] args, EvaluationWorkbook workbook, int srcCellSheet, int srcCellRow, int srcCellCol);
ValueEval evaluate(ValueEval[] args, EvaluationWorkbook workbook, int srcCellSheet, int srcCellRow, int srcCellCol);
}

View File

@ -17,7 +17,6 @@
package org.apache.poi.hssf.record.formula.functions;
import org.apache.poi.hssf.record.formula.eval.Eval;
import org.apache.poi.hssf.record.formula.eval.ValueEval;
import org.apache.poi.ss.formula.EvaluationWorkbook;
import org.apache.poi.ss.formula.eval.NotImplementedException;
@ -46,7 +45,7 @@ public final class Indirect implements FreeRefFunction {
// enforce singleton
}
public ValueEval evaluate(Eval[] args, EvaluationWorkbook workbook, int srcCellSheet, int srcCellRow, int srcCellCol) {
public ValueEval evaluate(ValueEval[] args, EvaluationWorkbook workbook, int srcCellSheet, int srcCellRow, int srcCellCol) {
// TODO - implement INDIRECT()
throw new NotImplementedException("INDIRECT");
}

View File

@ -51,7 +51,6 @@ import org.apache.poi.hssf.record.formula.eval.AreaEval;
import org.apache.poi.hssf.record.formula.eval.BlankEval;
import org.apache.poi.hssf.record.formula.eval.BoolEval;
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.FunctionEval;
import org.apache.poi.hssf.record.formula.eval.MissingArgEval;
import org.apache.poi.hssf.record.formula.eval.NameEval;
@ -237,20 +236,20 @@ public final class WorkbookEvaluator {
CellReference cr = new CellReference(rowIndex, columnIndex);
logDebug("Evaluated " + sheetName + "!" + cr.formatAsString() + " to " + result.toString());
}
// Usually (result === cce.getValue())
// Usually (result === cce.getValue())
// But sometimes: (result==ErrorEval.CIRCULAR_REF_ERROR, cce.getValue()==null)
// When circular references are detected, the cache entry is only updated for
// the top evaluation frame
// When circular references are detected, the cache entry is only updated for
// the top evaluation frame
return result;
}
/**
* Adds the current cell reference to the exception for easier debugging.
* Adds the current cell reference to the exception for easier debugging.
* Would be nice to get the formula text as well, but that seems to require
* too much digging around and casting to get the FormulaRenderingWorkbook.
* too much digging around and casting to get the FormulaRenderingWorkbook.
*/
private NotImplementedException addExceptionInfo(NotImplementedException inner, int sheetIndex, int rowIndex, int columnIndex) {
try {
String sheetName = _workbook.getSheetName(sheetIndex);
CellReference cr = new CellReference(sheetName, rowIndex, columnIndex, false, false);
@ -385,7 +384,7 @@ public final class WorkbookEvaluator {
return evaluationResult;
}
private static ValueEval invokeOperation(OperationEval operation, Eval[] ops,
private static ValueEval invokeOperation(OperationEval operation, ValueEval[] ops,
EvaluationWorkbook workbook, int sheetIndex, int srcRowNum, int srcColNum) {
if(operation instanceof FunctionEval) {

View File

@ -24,18 +24,18 @@ import org.apache.poi.hssf.record.formula.functions.NumericFunctionInvoker;
/**
* Test for divide operator evaluator.
*
*
* @author Josh Micich
*/
public final class TestDivideEval extends TestCase {
private static void confirm(ValueEval arg0, ValueEval arg1, double expectedResult) {
Eval[] args = {
arg0, arg1,
ValueEval[] args = {
arg0, arg1,
};
double result = NumericFunctionInvoker.invoke(DivideEval.instance, args, 0, 0);
assertEquals(expectedResult, result, 0);
}
@ -53,10 +53,10 @@ public final class TestDivideEval extends TestCase {
confirm(ae0, ae1, 5);
}
public void testDivZero() {
Eval[] args = {
new NumberEval(5), NumberEval.ZERO,
ValueEval[] args = {
new NumberEval(5), NumberEval.ZERO,
};
Eval result = DivideEval.instance.evaluate(args, 0, (short) 0);
ValueEval result = DivideEval.instance.evaluate(args, 0, (short) 0);
assertEquals(ErrorEval.DIV_ZERO, result);
}
}

View File

@ -36,11 +36,11 @@ public final class TestEqualEval extends TestCase {
public void test1x1AreaOperand() {
ValueEval[] values = { BoolEval.FALSE, };
Eval[] args = {
ValueEval[] args = {
EvalFactory.createAreaEval("B1:B1", values),
BoolEval.FALSE,
};
Eval result = EqualEval.instance.evaluate(args, 10, (short)20);
ValueEval result = EqualEval.instance.evaluate(args, 10, (short)20);
if (result instanceof ErrorEval) {
if (result == ErrorEval.VALUE_INVALID) {
throw new AssertionFailedError("Identified bug in evaluation of 1x1 area");
@ -54,11 +54,11 @@ public final class TestEqualEval extends TestCase {
*/
public void testBlankEqualToEmptyString() {
Eval[] args = {
ValueEval[] args = {
new StringEval(""),
BlankEval.INSTANCE,
};
Eval result = EqualEval.instance.evaluate(args, 10, (short)20);
ValueEval result = EqualEval.instance.evaluate(args, 10, (short)20);
assertEquals(BoolEval.class, result.getClass());
BoolEval be = (BoolEval) result;
if (!be.getBooleanValue()) {
@ -82,11 +82,11 @@ public final class TestEqualEval extends TestCase {
}
private static boolean evalStringCmp(String a, String b, OperationEval cmpOp) {
Eval[] args = {
ValueEval[] args = {
new StringEval(a),
new StringEval(b),
};
Eval result = cmpOp.evaluate(args, 10, (short)20);
ValueEval result = cmpOp.evaluate(args, 10, (short)20);
assertEquals(BoolEval.class, result.getClass());
BoolEval be = (BoolEval) result;
return be.getBooleanValue();
@ -103,12 +103,12 @@ public final class TestEqualEval extends TestCase {
*/
public void testZeroEquality_bug47198() {
NumberEval zero = new NumberEval(0.0);
NumberEval mZero = (NumberEval) UnaryMinusEval.instance.evaluate(new Eval[] { zero, }, 0,
NumberEval mZero = (NumberEval) UnaryMinusEval.instance.evaluate(new ValueEval[] { zero, }, 0,
(short) 0);
if (Double.doubleToLongBits(mZero.getNumberValue()) == 0x8000000000000000L) {
throw new AssertionFailedError("Identified bug 47198: unary minus should convert -0.0 to 0.0");
}
Eval[] args = { zero, mZero, };
ValueEval[] args = { zero, mZero, };
BoolEval result = (BoolEval) EqualEval.instance.evaluate(args, 0, (short) 0);
if (!result.getBooleanValue()) {
throw new AssertionFailedError("Identified bug 47198: -0.0 != 0.0");
@ -123,7 +123,7 @@ public final class TestEqualEval extends TestCase {
NumberEval b = new NumberEval(1.0055);
assertEquals("1.0055", b.getStringValue());
Eval[] args = { a, b, };
ValueEval[] args = { a, b, };
BoolEval result = (BoolEval) EqualEval.instance.evaluate(args, 0, (short) 0);
if (!result.getBooleanValue()) {
throw new AssertionFailedError("Identified bug 47598: 1+1.0028-0.9973 != 1.0055");

View File

@ -77,7 +77,7 @@ public final class TestMinusZeroResult extends TestCase {
* Uses {@link ConcatEval} to force number-to-text conversion
*/
private static void confirmTextRendering(String expRendering, double d) {
Eval[] args = { StringEval.EMPTY_INSTANCE, new NumberEval(d), };
ValueEval[] args = { StringEval.EMPTY_INSTANCE, new NumberEval(d), };
StringEval se = (StringEval) ConcatEval.instance.evaluate(args, -1, (short)-1);
String result = se.getStringValue();
assertEquals(expRendering, result);
@ -91,13 +91,13 @@ public final class TestMinusZeroResult extends TestCase {
BoolEval result = (BoolEval) evaluate(instance, dArgs);
assertEquals(expectedResult, result.getBooleanValue());
}
private static Eval evaluate(OperationEval instance, double... dArgs) {
Eval[] evalArgs;
evalArgs = new Eval[dArgs.length];
private static ValueEval evaluate(OperationEval instance, double... dArgs) {
ValueEval[] evalArgs;
evalArgs = new ValueEval[dArgs.length];
for (int i = 0; i < evalArgs.length; i++) {
evalArgs[i] = new NumberEval(dArgs[i]);
}
Eval r = instance.evaluate(evalArgs, -1, (short)-1);
ValueEval r = instance.evaluate(evalArgs, -1, (short)-1);
return r;
}

View File

@ -31,19 +31,19 @@ import org.apache.poi.ss.usermodel.CellValue;
/**
* Test for percent operator evaluator.
*
*
* @author Josh Micich
*/
public final class TestPercentEval extends TestCase {
private static void confirm(ValueEval arg, double expectedResult) {
Eval[] args = {
arg,
ValueEval[] args = {
arg,
};
OperationEval opEval = PercentEval.instance;
double result = NumericFunctionInvoker.invoke(opEval, args, 0, 0);
assertEquals(expectedResult, result, 0);
}
@ -66,7 +66,7 @@ public final class TestPercentEval extends TestCase {
HSSFCell cell = row.createCell(0);
cell.setCellFormula("B1%");
row.createCell(1).setCellValue(50.0);
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
CellValue cv;
try {
@ -81,5 +81,4 @@ public final class TestPercentEval extends TestCase {
assertEquals(HSSFCell.CELL_TYPE_NUMERIC, cv.getCellType());
assertEquals(0.5, cv.getNumberValue(), 0.0);
}
}

View File

@ -59,12 +59,12 @@ public final class TestRangeEval extends TestCase {
private static void confirm(String refA, String refB, String expectedAreaRef) {
Eval[] args = {
ValueEval[] args = {
createRefEval(refA),
createRefEval(refB),
};
AreaReference ar = new AreaReference(expectedAreaRef);
Eval result = RangeEval.instance.evaluate(args, 0, (short)0);
ValueEval result = RangeEval.instance.evaluate(args, 0, (short)0);
assertTrue(result instanceof AreaEval);
AreaEval ae = (AreaEval) result;
assertEquals(ar.getFirstCell().getRow(), ae.getFirstRow());
@ -73,7 +73,7 @@ public final class TestRangeEval extends TestCase {
assertEquals(ar.getLastCell().getCol(), ae.getLastColumn());
}
private static Eval createRefEval(String refStr) {
private static ValueEval createRefEval(String refStr) {
CellReference cr = new CellReference(refStr);
return new MockRefEval(cr.getRow(), cr.getCol());

View File

@ -47,7 +47,7 @@ public final class TestUnaryPlusEval extends TestCase {
new NumberEval(37),
new NumberEval(38),
};
Eval[] args = {
ValueEval[] args = {
EvalFactory.createAreaEval(areaPtg, values),
};

View File

@ -14,7 +14,6 @@
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.record.formula.functions;
@ -24,11 +23,12 @@ 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.NumericValueEval;
import org.apache.poi.hssf.record.formula.eval.OperationEval;
import org.apache.poi.hssf.record.formula.eval.ValueEval;
import org.apache.poi.ss.formula.eval.NotImplementedException;
/**
* Test helper class for invoking functions with numeric results.
*
*
* @author Josh Micich
*/
public final class NumericFunctionInvoker {
@ -36,13 +36,13 @@ public final class NumericFunctionInvoker {
private NumericFunctionInvoker() {
// no instances of this class
}
private static final class NumericEvalEx extends Exception {
public NumericEvalEx(String msg) {
super(msg);
}
}
/**
* Invokes the specified function with the arguments.
* <p/>
@ -53,14 +53,14 @@ public final class NumericFunctionInvoker {
* This method cannot be used for confirming error return codes. Any non-numeric evaluation
* result causes the current junit test to fail.
*/
public static double invoke(Function f, Eval[] args) {
public static double invoke(Function f, ValueEval[] args) {
try {
return invokeInternal(f, args, -1, -1);
} catch (NumericEvalEx e) {
throw new AssertionFailedError("Evaluation of function (" + f.getClass().getName()
throw new AssertionFailedError("Evaluation of function (" + f.getClass().getName()
+ ") failed: " + e.getMessage());
}
}
/**
* Invokes the specified operator with the arguments.
@ -68,19 +68,19 @@ public final class NumericFunctionInvoker {
* This method cannot be used for confirming error return codes. Any non-numeric evaluation
* result causes the current junit test to fail.
*/
public static double invoke(OperationEval f, Eval[] args, int srcCellRow, int srcCellCol) {
public static double invoke(OperationEval f, ValueEval[] args, int srcCellRow, int srcCellCol) {
try {
return invokeInternal(f, args, srcCellRow, srcCellCol);
} catch (NumericEvalEx e) {
throw new AssertionFailedError("Evaluation of function (" + f.getClass().getName()
throw new AssertionFailedError("Evaluation of function (" + f.getClass().getName()
+ ") failed: " + e.getMessage());
}
}
/**
* Formats nicer error messages for the junit output
*/
private static double invokeInternal(Object target, Eval[] args, int srcCellRow, int srcCellCol)
private static double invokeInternal(Object target, ValueEval[] args, int srcCellRow, int srcCellCol)
throws NumericEvalEx {
Eval evalResult;
// TODO - make OperationEval extend Function
@ -95,7 +95,7 @@ public final class NumericFunctionInvoker {
} catch (NotImplementedException e) {
throw new NumericEvalEx("Not implemented:" + e.getMessage());
}
if(evalResult == null) {
throw new NumericEvalEx("Result object was null");
}
@ -105,10 +105,10 @@ public final class NumericFunctionInvoker {
}
if(!(evalResult instanceof NumericValueEval)) {
throw new NumericEvalEx("Result object type (" + evalResult.getClass().getName()
+ ") is invalid. Expected implementor of ("
+ ") is invalid. Expected implementor of ("
+ NumericValueEval.class.getName() + ")");
}
NumericValueEval result = (NumericValueEval) evalResult;
return result.getNumberValue();
}
@ -124,5 +124,4 @@ public final class NumericFunctionInvoker {
}
return a.getErrorCode() == b.getErrorCode();
}
}

View File

@ -25,7 +25,6 @@ import org.apache.poi.hssf.record.formula.eval.AreaEval;
import org.apache.poi.hssf.record.formula.eval.BlankEval;
import org.apache.poi.hssf.record.formula.eval.BoolEval;
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.OperandResolver;
import org.apache.poi.hssf.record.formula.eval.StringEval;
@ -44,31 +43,31 @@ import org.apache.poi.ss.usermodel.CellValue;
* @author Josh Micich
*/
public final class TestCountFuncs extends TestCase {
private static final String NULL = null;
public void testCountA() {
Eval[] args;
ValueEval[] args;
args = new Eval[] {
args = new ValueEval[] {
new NumberEval(0),
};
confirmCountA(1, args);
args = new Eval[] {
args = new ValueEval[] {
new NumberEval(0),
new NumberEval(0),
new StringEval(""),
};
confirmCountA(3, args);
args = new Eval[] {
args = new ValueEval[] {
EvalFactory.createAreaEval("D2:F5", new ValueEval[12]),
};
confirmCountA(12, args);
args = new Eval[] {
args = new ValueEval[] {
EvalFactory.createAreaEval("D1:F5", new ValueEval[15]),
EvalFactory.createRefEval("A1"),
EvalFactory.createAreaEval("A1:G6", new ValueEval[42]),
@ -163,7 +162,6 @@ public final class TestCountFuncs extends TestCase {
range = EvalFactory.createAreaEval("A1:A5", values);
confirmCountIf(4, range, new StringEval("<>111"));
}
/**
@ -182,57 +180,57 @@ public final class TestCountFuncs extends TestCase {
AreaEval arg0 = EvalFactory.createAreaEval("C1:C6", values);
ValueEval criteriaArg = EvalFactory.createRefEval("A1", new NumberEval(25));
Eval[] args= { arg0, criteriaArg, };
ValueEval[] args= { arg0, criteriaArg, };
double actual = NumericFunctionInvoker.invoke(new Countif(), args);
assertEquals(4, actual, 0D);
}
private static void confirmCountA(int expected, Eval[] args) {
private static void confirmCountA(int expected, ValueEval[] args) {
double result = NumericFunctionInvoker.invoke(new Counta(), args);
assertEquals(expected, result, 0);
}
private static void confirmCountIf(int expected, AreaEval range, Eval criteria) {
private static void confirmCountIf(int expected, AreaEval range, ValueEval criteria) {
Eval[] args = { range, criteria, };
ValueEval[] args = { range, criteria, };
double result = NumericFunctionInvoker.invoke(new Countif(), args);
assertEquals(expected, result, 0);
}
private static I_MatchPredicate createCriteriaPredicate(Eval ev) {
private static I_MatchPredicate createCriteriaPredicate(ValueEval ev) {
return Countif.createCriteriaPredicate(ev, 0, 0);
}
/**
* the criteria arg is mostly handled by {@link OperandResolver#getSingleValue(Eval, int, short)}
*/
public void testCountifAreaCriteria() {
int srcColIx = 2; // anything but column A
ValueEval v0 = new NumberEval(2.0);
ValueEval v1 = new StringEval("abc");
ValueEval v2 = ErrorEval.DIV_ZERO;
AreaEval ev = EvalFactory.createAreaEval("A10:A12", new ValueEval[] { v0, v1, v2, });
I_MatchPredicate mp;
mp = Countif.createCriteriaPredicate(ev, 9, srcColIx);
confirmPredicate(true, mp, srcColIx);
confirmPredicate(false, mp, "abc");
confirmPredicate(false, mp, ErrorEval.DIV_ZERO);
mp = Countif.createCriteriaPredicate(ev, 10, srcColIx);
confirmPredicate(false, mp, srcColIx);
confirmPredicate(true, mp, "abc");
confirmPredicate(false, mp, ErrorEval.DIV_ZERO);
mp = Countif.createCriteriaPredicate(ev, 11, srcColIx);
confirmPredicate(false, mp, srcColIx);
confirmPredicate(false, mp, "abc");
confirmPredicate(true, mp, ErrorEval.DIV_ZERO);
confirmPredicate(false, mp, ErrorEval.VALUE_INVALID);
// tricky: indexing outside of A10:A12
// tricky: indexing outside of A10:A12
// even this #VALUE! error gets used by COUNTIF as valid criteria
mp = Countif.createCriteriaPredicate(ev, 12, srcColIx);
confirmPredicate(false, mp, srcColIx);
@ -240,7 +238,7 @@ public final class TestCountFuncs extends TestCase {
confirmPredicate(false, mp, ErrorEval.DIV_ZERO);
confirmPredicate(true, mp, ErrorEval.VALUE_INVALID);
}
public void testCountifEmptyStringCriteria() {
I_MatchPredicate mp;
@ -296,7 +294,7 @@ public final class TestCountFuncs extends TestCase {
confirmPredicate(true, mp, "500");
confirmPredicate(true, mp, "4t4");
}
/**
* the criteria arg value can be an error code (the error does not
* propagate to the COUNTIF result).
@ -315,7 +313,7 @@ public final class TestCountFuncs extends TestCase {
confirmPredicate(false, mp, "#REF!");
confirmPredicate(true, mp, ErrorEval.DIV_ZERO);
confirmPredicate(false, mp, ErrorEval.REF_INVALID);
// not quite an error literal, should be treated as plain text
mp = createCriteriaPredicate(new StringEval("<=#REF!a"));
confirmPredicate(false, mp, 4);
@ -382,7 +380,7 @@ public final class TestCountFuncs extends TestCase {
assertEquals(expectedResult, matchPredicate.matches(new NumberEval(value)));
}
private static void confirmPredicate(boolean expectedResult, I_MatchPredicate matchPredicate, String value) {
Eval ev = value == null ? (Eval)BlankEval.INSTANCE : new StringEval(value);
ValueEval ev = value == null ? BlankEval.INSTANCE : new StringEval(value);
assertEquals(expectedResult, matchPredicate.matches(ev));
}
private static void confirmPredicate(boolean expectedResult, I_MatchPredicate matchPredicate, ErrorEval value) {

View File

@ -21,7 +21,6 @@ import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.apache.poi.hssf.record.formula.eval.AreaEval;
import org.apache.poi.hssf.record.formula.eval.Eval;
import org.apache.poi.hssf.record.formula.eval.MissingArgEval;
import org.apache.poi.hssf.record.formula.eval.NumberEval;
import org.apache.poi.hssf.record.formula.eval.ValueEval;
@ -77,11 +76,11 @@ public final class TestIndex extends TestCase {
}
AreaEval arg0 = EvalFactory.createAreaEval(areaRefString, values);
Eval[] args;
ValueEval[] args;
if (colNum > 0) {
args = new Eval[] { arg0, new NumberEval(rowNum), new NumberEval(colNum), };
args = new ValueEval[] { arg0, new NumberEval(rowNum), new NumberEval(colNum), };
} else {
args = new Eval[] { arg0, new NumberEval(rowNum), };
args = new ValueEval[] { arg0, new NumberEval(rowNum), };
}
double actual = NumericFunctionInvoker.invoke(FUNC_INST, args);
@ -99,10 +98,10 @@ public final class TestIndex extends TestCase {
new NumberEval(28.0),
};
AreaEval arg0 = EvalFactory.createAreaEval("A10:C10", values);
Eval[] args = new Eval[] { arg0, MissingArgEval.instance, new NumberEval(2), };
Eval actualResult;
ValueEval[] args = new ValueEval[] { arg0, MissingArgEval.instance, new NumberEval(2), };
ValueEval actualResult;
try {
actualResult = FUNC_INST.evaluate(args, 1, (short)1);
actualResult = (ValueEval) FUNC_INST.evaluate(args, 1, (short)1);
} catch (RuntimeException e) {
if (e.getMessage().equals("Unexpected arg eval type (org.apache.poi.hssf.record.formula.eval.MissingArgEval")) {
throw new AssertionFailedError("Identified bug 47048b - INDEX() should support missing-arg");

View File

@ -19,12 +19,11 @@ package org.apache.poi.hssf.record.formula.functions;
import junit.framework.TestCase;
import org.apache.poi.hssf.record.formula.eval.Eval;
import org.apache.poi.hssf.record.formula.eval.ValueEval;
/**
* Tests for ROW(), ROWS(), COLUMN(), COLUMNS()
*
*
* @author Josh Micich
*/
public final class TestRowCol extends TestCase {
@ -32,65 +31,65 @@ public final class TestRowCol extends TestCase {
public void testCol() {
Function target = new Column();
{
Eval[] args = { EvalFactory.createRefEval("C5"), };
ValueEval[] args = { EvalFactory.createRefEval("C5"), };
double actual = NumericFunctionInvoker.invoke(target, args);
assertEquals(3, actual, 0D);
}
{
Eval[] args = { EvalFactory.createAreaEval("E2:H12", new ValueEval[44]), };
ValueEval[] args = { EvalFactory.createAreaEval("E2:H12", new ValueEval[44]), };
double actual = NumericFunctionInvoker.invoke(target, args);
assertEquals(5, actual, 0D);
}
}
public void testRow() {
Function target = new Row();
{
Eval[] args = { EvalFactory.createRefEval("C5"), };
ValueEval[] args = { EvalFactory.createRefEval("C5"), };
double actual = NumericFunctionInvoker.invoke(target, args);
assertEquals(5, actual, 0D);
}
{
Eval[] args = { EvalFactory.createAreaEval("E2:H12", new ValueEval[44]), };
ValueEval[] args = { EvalFactory.createAreaEval("E2:H12", new ValueEval[44]), };
double actual = NumericFunctionInvoker.invoke(target, args);
assertEquals(2, actual, 0D);
}
}
public void testColumns() {
confirmColumnsFunc("A1:F1", 6, 1);
confirmColumnsFunc("A1:C2", 3, 2);
confirmColumnsFunc("A1:B3", 2, 3);
confirmColumnsFunc("A1:A6", 1, 6);
Eval[] args = { EvalFactory.createRefEval("C5"), };
ValueEval[] args = { EvalFactory.createRefEval("C5"), };
double actual = NumericFunctionInvoker.invoke(new Columns(), args);
assertEquals(1, actual, 0D);
}
public void testRows() {
confirmRowsFunc("A1:F1", 6, 1);
confirmRowsFunc("A1:C2", 3, 2);
confirmRowsFunc("A1:B3", 2, 3);
confirmRowsFunc("A1:A6", 1, 6);
Eval[] args = { EvalFactory.createRefEval("C5"), };
ValueEval[] args = { EvalFactory.createRefEval("C5"), };
double actual = NumericFunctionInvoker.invoke(new Rows(), args);
assertEquals(1, actual, 0D);
}
private static void confirmRowsFunc(String areaRefStr, int nCols, int nRows) {
Eval[] args = { EvalFactory.createAreaEval(areaRefStr, new ValueEval[nCols * nRows]), };
ValueEval[] args = { EvalFactory.createAreaEval(areaRefStr, new ValueEval[nCols * nRows]), };
double actual = NumericFunctionInvoker.invoke(new Rows(), args);
assertEquals(nRows, actual, 0D);
}
private static void confirmColumnsFunc(String areaRefStr, int nCols, int nRows) {
Eval[] args = { EvalFactory.createAreaEval(areaRefStr, new ValueEval[nCols * nRows]), };
ValueEval[] args = { EvalFactory.createAreaEval(areaRefStr, new ValueEval[nCols * nRows]), };
double actual = NumericFunctionInvoker.invoke(new Columns(), args);
assertEquals(nCols, actual, 0D);