You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by jo...@apache.org on 2008/09/08 23:34:46 UTC

svn commit: r693289 - in /poi/trunk/src: java/org/apache/poi/hssf/record/formula/atp/ java/org/apache/poi/hssf/record/formula/eval/ java/org/apache/poi/hssf/record/formula/functions/ testcases/org/apache/poi/hssf/data/ testcases/org/apache/poi/hssf/rec...

Author: josh
Date: Mon Sep  8 14:34:45 2008
New Revision: 693289

URL: http://svn.apache.org/viewvc?rev=693289&view=rev
Log:
Refactored OperandResolver coerce functions to convert BlankEval to 0.0

Modified:
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/atp/ParityFunction.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/atp/YearFrac.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/OperandResolver.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/PercentEval.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/TwoOperandNumericOperation.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/UnaryMinusEval.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/UnaryPlusEval.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/CalendarFieldFunction.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/LookupUtils.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/Mid.java
    poi/trunk/src/testcases/org/apache/poi/hssf/data/FormulaEvalTestData.xls
    poi/trunk/src/testcases/org/apache/poi/hssf/data/externalFunctionExample.xls
    poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/TestExternalFunctionFormulas.java

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/formula/atp/ParityFunction.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/atp/ParityFunction.java?rev=693289&r1=693288&r2=693289&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/atp/ParityFunction.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/atp/ParityFunction.java Mon Sep  8 14:34:45 2008
@@ -61,9 +61,6 @@
 	private static int evaluateArgParity(Eval arg, int srcCellRow, short srcCellCol) throws EvaluationException {
 		ValueEval ve = OperandResolver.getSingleValue(arg, srcCellRow, srcCellCol);
 		
-		if (ve == BlankEval.INSTANCE) {
-			return 0;
-		}
 		double d = OperandResolver.coerceValueToDouble(ve);
 		if (d < 0) {
 			d = -d;

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/formula/atp/YearFrac.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/atp/YearFrac.java?rev=693289&r1=693288&r2=693289&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/atp/YearFrac.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/atp/YearFrac.java Mon Sep  8 14:34:45 2008
@@ -97,9 +97,6 @@
 			Calendar date = parseDate(strVal);
 			return HSSFDateUtil.getExcelDate(date, false);
 		}
-		if (ve instanceof BlankEval) {
-			return 0.0;
-		}
 		return OperandResolver.coerceValueToDouble(ve);
 	}
 

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/OperandResolver.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/OperandResolver.java?rev=693289&r1=693288&r2=693289&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/OperandResolver.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/OperandResolver.java Mon Sep  8 14:34:45 2008
@@ -171,7 +171,8 @@
 
 	/**
 	 * Applies some conversion rules if the supplied value is not already an integer.<br/>
-	 * Value is first coerced to a <tt>double</tt> ( See <tt>coerceValueToDouble()</tt> ).<p/>
+	 * Value is first coerced to a <tt>double</tt> ( See <tt>coerceValueToDouble()</tt> ).
+	 * Note - <tt>BlankEval</tt> is converted to <code>0</code>.<p/> 
 	 * 
 	 * Excel typically converts doubles to integers by truncating toward negative infinity.<br/>
 	 * The equivalent java code is:<br/>
@@ -181,6 +182,9 @@
 	 * 
 	 */
 	public static int coerceValueToInt(ValueEval ev) throws EvaluationException {
+		if (ev == BlankEval.INSTANCE) {
+			return 0;
+		}
 		double d = coerceValueToDouble(ev);
 		// Note - the standard java type conversion from double to int truncates toward zero.
 		// but Math.floor() truncates toward negative infinity
@@ -189,16 +193,20 @@
 
 	/**
 	 * Applies some conversion rules if the supplied value is not already a number.
-	 * Note - <tt>BlankEval</tt> is not supported and must be handled by the caller. 
-	 * @param ev must be a <tt>NumberEval</tt>, <tt>StringEval</tt> or <tt>BoolEval</tt>
+	 * Note - <tt>BlankEval</tt> is converted to {@link NumberEval#ZERO}. 
+	 * @param ev must be a {@link NumberEval}, {@link StringEval}, {@link BoolEval} or 
+	 * {@link BlankEval}
 	 * @return actual, parsed or interpreted double value (respectively).
 	 * @throws EvaluationException(#VALUE!) only if a StringEval is supplied and cannot be parsed
 	 * as a double (See <tt>parseDouble()</tt> for allowable formats).
-	 * @throws RuntimeException if the supplied parameter is not <tt>NumberEval</tt>,
-	 *  <tt>StringEval</tt> or <tt>BoolEval</tt>
+	 * @throws RuntimeException if the supplied parameter is not {@link NumberEval}, 
+	 * {@link StringEval}, {@link BoolEval} or {@link BlankEval}
 	 */
 	public static double coerceValueToDouble(ValueEval ev) throws EvaluationException {
 
+		if (ev == BlankEval.INSTANCE) {
+			return 0.0;
+		}
 		if (ev instanceof NumericValueEval) {
 			// this also handles booleans
 			return ((NumericValueEval)ev).getNumberValue();

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/PercentEval.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/PercentEval.java?rev=693289&r1=693288&r2=693289&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/PercentEval.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/PercentEval.java Mon Sep  8 14:34:45 2008
@@ -33,12 +33,9 @@
 		if (args.length != 1) {
 			return ErrorEval.VALUE_INVALID;
 		}
-    	double d0;
+		double d0;
 		try {
 			ValueEval ve = OperandResolver.getSingleValue(args[0], srcRow, srcCol);
-			if (ve instanceof BlankEval) {
-				return NumberEval.ZERO;
-			}
 			d0 = OperandResolver.coerceValueToDouble(ve);
 		} catch (EvaluationException e) {
 			return e.getErrorEval();
@@ -50,7 +47,7 @@
 		return 1;
 	}
 	public final int getType() {
-    	// TODO - remove
-        throw new RuntimeException("obsolete code should not be called");
-    }
+		// TODO - remove
+		throw new RuntimeException("obsolete code should not be called");
+	}
 }

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/TwoOperandNumericOperation.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/TwoOperandNumericOperation.java?rev=693289&r1=693288&r2=693289&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/TwoOperandNumericOperation.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/TwoOperandNumericOperation.java Mon Sep  8 14:34:45 2008
@@ -23,18 +23,15 @@
 abstract class TwoOperandNumericOperation implements OperationEval {
 
 	public final int getType() {
-    	// TODO - remove
-        throw new RuntimeException("obsolete code should not be called");
-    }
-    protected final double singleOperandEvaluate(Eval arg, int srcCellRow, short srcCellCol) throws EvaluationException {
-    	ValueEval ve = OperandResolver.getSingleValue(arg, srcCellRow, srcCellCol);
-		if (ve instanceof BlankEval) {
-			return 0.0;
-		}
-   		return OperandResolver.coerceValueToDouble(ve);
-    }
-    
-    public final Eval evaluate(Eval[] args, int srcCellRow, short srcCellCol) {
+		// TODO - remove
+		throw new RuntimeException("obsolete code should not be called");
+	}
+	protected final double singleOperandEvaluate(Eval arg, int srcCellRow, short srcCellCol) throws EvaluationException {
+		ValueEval ve = OperandResolver.getSingleValue(arg, srcCellRow, srcCellCol);
+		return OperandResolver.coerceValueToDouble(ve);
+	}
+	
+	public final Eval evaluate(Eval[] args, int srcCellRow, short srcCellCol) {
 		double result;
 		try {
 			double d0 = singleOperandEvaluate(args[0], srcCellRow, srcCellCol);
@@ -46,8 +43,8 @@
 		} catch (EvaluationException e) {
 			return e.getErrorEval();
 		}
-    	return new NumberEval(result);
-    }
+		return new NumberEval(result);
+	}
 	protected abstract double evaluate(double d0, double d1) throws EvaluationException;
 	public final int getNumberOfOperands() {
 		return 2;

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/UnaryMinusEval.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/UnaryMinusEval.java?rev=693289&r1=693288&r2=693289&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/UnaryMinusEval.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/UnaryMinusEval.java Mon Sep  8 14:34:45 2008
@@ -33,12 +33,9 @@
 		if (args.length != 1) {
 			return ErrorEval.VALUE_INVALID;
 		}
-    	double d;
+		double d;
 		try {
 			ValueEval ve = OperandResolver.getSingleValue(args[0], srcRow, srcCol);
-			if (ve instanceof BlankEval) {
-				return NumberEval.ZERO;
-			}
 			d = OperandResolver.coerceValueToDouble(ve);
 		} catch (EvaluationException e) {
 			return e.getErrorEval();
@@ -50,7 +47,7 @@
 		return 1;
 	}
 	public final int getType() {
-    	// TODO - remove
-        throw new RuntimeException("obsolete code should not be called");
-    }
+		// TODO - remove
+		throw new RuntimeException("obsolete code should not be called");
+	}
 }

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/UnaryPlusEval.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/UnaryPlusEval.java?rev=693289&r1=693288&r2=693289&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/UnaryPlusEval.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/UnaryPlusEval.java Mon Sep  8 14:34:45 2008
@@ -24,21 +24,18 @@
  */
 public final class UnaryPlusEval implements OperationEval {
 
-    public static final OperationEval instance = new UnaryPlusEval();
-    
-    private UnaryPlusEval() {
-    }
+	public static final OperationEval instance = new UnaryPlusEval();
+	
+	private UnaryPlusEval() {
+	}
 
-    public Eval evaluate(Eval[] args, int srcCellRow, short srcCellCol) {
-    	if(args.length != 1) {
-    		return ErrorEval.VALUE_INVALID;
-    	}
-    	double d;
+	public Eval evaluate(Eval[] args, int srcCellRow, short srcCellCol) {
+		if(args.length != 1) {
+			return ErrorEval.VALUE_INVALID;
+		}
+		double d;
 		try {
 			ValueEval ve = OperandResolver.getSingleValue(args[0], srcCellRow, srcCellCol);
-			if(ve instanceof BlankEval) {
-				return NumberEval.ZERO;
-			}
 			if(ve instanceof StringEval) {
 				// Note - asymmetric with UnaryMinus
 				// -"hello" evaluates to #VALUE!
@@ -49,14 +46,14 @@
 		} catch (EvaluationException e) {
 			return e.getErrorEval();
 		}
-    	return new NumberEval(+d);    	
-    }
+		return new NumberEval(+d);
+	}
 
-    public int getNumberOfOperands() {
-        return 1;
-    }
+	public int getNumberOfOperands() {
+		return 1;
+	}
 
-    public int getType() {
-        throw new RuntimeException("obsolete code should not be called");
-    }
+	public int getType() {
+		throw new RuntimeException("obsolete code should not be called");
+	}
 }

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/CalendarFieldFunction.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/CalendarFieldFunction.java?rev=693289&r1=693288&r2=693289&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/CalendarFieldFunction.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/CalendarFieldFunction.java Mon Sep  8 14:34:45 2008
@@ -21,7 +21,6 @@
 import java.util.Date;
 import java.util.GregorianCalendar;
 
-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.EvaluationException;
@@ -58,12 +57,7 @@
 		int val;
 		try {
 			ValueEval ve = OperandResolver.getSingleValue(operands[0], srcCellRow, srcCellCol);
-
-			if (ve == BlankEval.INSTANCE) {
-				val = 0;
-			} else {
-				val = OperandResolver.coerceValueToInt(ve);
-			}
+			val = OperandResolver.coerceValueToInt(ve);
 		} catch (EvaluationException e) {
 			return e.getErrorEval();
 		}

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/LookupUtils.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/LookupUtils.java?rev=693289&r1=693288&r2=693289&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/LookupUtils.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/LookupUtils.java Mon Sep  8 14:34:45 2008
@@ -339,23 +339,19 @@
 			throw EvaluationException.invalidRef();
 		}
 		int oneBasedIndex;
-		if(veRowColIndexArg instanceof BlankEval) {
-			oneBasedIndex = 0;
-		} else {
-			if(veRowColIndexArg instanceof StringEval) {
-				StringEval se = (StringEval) veRowColIndexArg;
-				String strVal = se.getStringValue();
-				Double dVal = OperandResolver.parseDouble(strVal);
-				if(dVal == null) {
-					// String does not resolve to a number. Raise #REF! error.
-					throw EvaluationException.invalidRef();
-					// This includes text booleans "TRUE" and "FALSE".  They are not valid.
-				}
-				// else - numeric value parses OK
+		if(veRowColIndexArg instanceof StringEval) {
+			StringEval se = (StringEval) veRowColIndexArg;
+			String strVal = se.getStringValue();
+			Double dVal = OperandResolver.parseDouble(strVal);
+			if(dVal == null) {
+				// String does not resolve to a number. Raise #REF! error.
+				throw EvaluationException.invalidRef();
+				// This includes text booleans "TRUE" and "FALSE".  They are not valid.
 			}
-			// actual BoolEval values get interpreted as FALSE->0 and TRUE->1
-			oneBasedIndex = OperandResolver.coerceValueToInt(veRowColIndexArg);
+			// else - numeric value parses OK
 		}
+		// actual BoolEval values get interpreted as FALSE->0 and TRUE->1
+		oneBasedIndex = OperandResolver.coerceValueToInt(veRowColIndexArg);
 		if (oneBasedIndex < 1) {
 			// note this is asymmetric with the errors when the index is too large (#REF!)  
 			throw EvaluationException.invalidValue();

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/Mid.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/Mid.java?rev=693289&r1=693288&r2=693289&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/Mid.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/Mid.java Mon Sep  8 14:34:45 2008
@@ -17,7 +17,6 @@
 
 package org.apache.poi.hssf.record.formula.functions;
 
-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.EvaluationException;
@@ -81,12 +80,8 @@
 
 	private static int evaluateNumberArg(Eval arg, int srcCellRow, short srcCellCol) throws EvaluationException {
 		ValueEval ev = OperandResolver.getSingleValue(arg, srcCellRow, srcCellCol);
-		if (ev instanceof BlankEval) {
-			// Note - for start_num arg, blank causes error(#VALUE!),
-			// but for num_chars causes empty string to be returned.
-			return 0;
-		}
-
+		// Note - for start_num arg, blank/zero causes error(#VALUE!),
+		// but for num_chars causes empty string to be returned.
 		return OperandResolver.coerceValueToInt(ev);
 	}
 }
\ No newline at end of file

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/data/FormulaEvalTestData.xls
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/data/FormulaEvalTestData.xls?rev=693289&r1=693288&r2=693289&view=diff
==============================================================================
Binary files - no diff available.

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/data/externalFunctionExample.xls
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/data/externalFunctionExample.xls?rev=693289&r1=693288&r2=693289&view=diff
==============================================================================
Binary files - no diff available.

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/TestExternalFunctionFormulas.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/TestExternalFunctionFormulas.java?rev=693289&r1=693288&r2=693289&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/TestExternalFunctionFormulas.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/TestExternalFunctionFormulas.java Mon Sep  8 14:34:45 2008
@@ -77,8 +77,9 @@
 		HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(sheet, wb);
 		confirmCellEval(sheet, 0, 0, fe, "YEARFRAC(B1,C1)", 29.0/90.0);
 		confirmCellEval(sheet, 1, 0, fe, "YEARFRAC(B2,C2)", 0.0);
-		confirmCellEval(sheet, 2, 0, fe, "IF(ISEVEN(3),1.2,1.6)", 1.6);
-		confirmCellEval(sheet, 3, 0, fe, "IF(ISODD(3),1.2,1.6)", 1.2);
+		confirmCellEval(sheet, 2, 0, fe, "YEARFRAC(B3,C3,D3)", 0.0);
+		confirmCellEval(sheet, 3, 0, fe, "IF(ISEVEN(3),1.2,1.6)", 1.6);
+		confirmCellEval(sheet, 4, 0, fe, "IF(ISODD(3),1.2,1.6)", 1.2);
 	}
 
 	private static void confirmCellEval(HSSFSheet sheet, int rowIx, int colIx, 



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org