poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r805971 - in /poi/trunk/src: java/org/apache/poi/hssf/record/formula/eval/ java/org/apache/poi/ss/formula/ testcases/org/apache/poi/hssf/record/formula/eval/ testcases/org/apache/poi/hssf/record/formula/functions/
Date Wed, 19 Aug 2009 20:55:45 GMT
Author: josh
Date: Wed Aug 19 20:55:44 2009
New Revision: 805971

URL: http://svn.apache.org/viewvc?rev=805971&view=rev
Log:
changed interface from OperationEval to Function for basic operators

Added:
    poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/EvalInstances.java
Removed:
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/AddEval.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/DivideEval.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/EqualEval.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/GreaterEqualEval.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/GreaterThanEval.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/LessEqualEval.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/LessThanEval.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/MultiplyEval.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/NotEqualEval.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/PowerEval.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/SubtractEval.java
Modified:
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/ConcatEval.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/RangeEval.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/RelationalOperationEval.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/ss/formula/OperationEvaluatorFactory.java
    poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/TestDivideEval.java
    poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/TestEqualEval.java
    poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/TestMinusZeroResult.java
    poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/TestPercentEval.java
    poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/functions/NumericFunctionInvoker.java

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/ConcatEval.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/ConcatEval.java?rev=805971&r1=805970&r2=805971&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/ConcatEval.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/ConcatEval.java Wed Aug 19
20:55:44 2009
@@ -17,12 +17,14 @@
 
 package org.apache.poi.hssf.record.formula.eval;
 
+import org.apache.poi.hssf.record.formula.functions.Function;
+
 /**
  * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
  */
-public final class ConcatEval implements OperationEval {
+public final class ConcatEval implements Function {
 
-	public static final OperationEval instance = new ConcatEval();
+	public static final Function instance = new ConcatEval();
 
 	private ConcatEval() {
 		// enforce singleton
@@ -53,8 +55,4 @@
 
 		return new StringEval(sb.toString());
 	}
-
-	public int getNumberOfOperands() {
-		return 2;
-	}
 }

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=805971&r1=805970&r2=805971&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 Wed Aug
19 20:55:44 2009
@@ -40,7 +40,7 @@
 	 *  an AreaEval.  If the actual value retrieved is an ErrorEval, a corresponding
 	 *  EvaluationException is thrown.
 	 */
-	public static ValueEval getSingleValue(ValueEval arg, int srcCellRow, short srcCellCol)
+	public static ValueEval getSingleValue(ValueEval arg, int srcCellRow, int srcCellCol)
 			throws EvaluationException {
 		ValueEval result;
 		if (arg instanceof RefEval) {
@@ -101,8 +101,8 @@
 	 *  evaluated cell has an error.
 	 */
 	public static ValueEval chooseSingleElementFromArea(AreaEval ae,
-			int srcCellRow, short srcCellCol) throws EvaluationException {
-		ValueEval result = chooseSingleElementFromAreaInternal(ae, srcCellRow, srcCellCol);
+			int srcCellRow, int srcCellCol) throws EvaluationException {
+		ValueEval result = chooseSingleElementFromAreaInternal(ae, srcCellRow, (short) srcCellCol);
 		if(result == null) {
 			// This seems to be required because AreaEval.values() array may contain nulls.
 			// perhaps that should not be allowed.

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=805971&r1=805970&r2=805971&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 Wed Aug 19
20:55:44 2009
@@ -17,14 +17,16 @@
 
 package org.apache.poi.hssf.record.formula.eval;
 
+import org.apache.poi.hssf.record.formula.functions.Function;
+
 
 /**
  * Implementation of Excel formula token '%'. <p/>
  * @author Josh Micich
  */
-public final class PercentEval implements OperationEval {
+public final class PercentEval implements Function {
 
-	public static final OperationEval instance = new PercentEval();
+	public static final Function instance = new PercentEval();
 
 	private PercentEval() {
 		// enforce singleton
@@ -46,8 +48,4 @@
 		}
 		return new NumberEval(d / 100);
 	}
-
-	public int getNumberOfOperands() {
-		return 1;
-	}
 }

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/RangeEval.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/RangeEval.java?rev=805971&r1=805970&r2=805971&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/RangeEval.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/RangeEval.java Wed Aug 19 20:55:44
2009
@@ -17,20 +17,22 @@
 
 package org.apache.poi.hssf.record.formula.eval;
 
+import org.apache.poi.hssf.record.formula.functions.Function;
+
 
 /**
  *
  * @author Josh Micich
  */
-public final class RangeEval implements OperationEval {
+public final class RangeEval implements Function {
 
-	public static final OperationEval instance = new RangeEval();
+	public static final Function instance = new RangeEval();
 
 	private RangeEval() {
 		// enforces singleton
 	}
 
-	public ValueEval evaluate(ValueEval[] args, int srcCellRow, short srcCellCol) {
+	public ValueEval evaluate(ValueEval[] args, int srcRow, short srcCol) {
 		if(args.length != 2) {
 			return ErrorEval.VALUE_INVALID;
 		}
@@ -72,8 +74,4 @@
 		}
 		throw new IllegalArgumentException("Unexpected ref arg class (" + arg.getClass().getName()
+ ")");
 	}
-
-	public int getNumberOfOperands() {
-		return 2;
-	}
 }

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/RelationalOperationEval.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/RelationalOperationEval.java?rev=805971&r1=805970&r2=805971&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/RelationalOperationEval.java
(original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/RelationalOperationEval.java
Wed Aug 19 20:55:44 2009
@@ -17,6 +17,7 @@
 
 package org.apache.poi.hssf.record.formula.eval;
 
+import org.apache.poi.hssf.record.formula.functions.Function;
 import org.apache.poi.ss.util.NumberComparer;
 
 /**
@@ -24,7 +25,7 @@
  *
  * @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt;
  */
-public abstract class RelationalOperationEval implements OperationEval {
+public abstract class RelationalOperationEval implements Function {
 
 	/**
 	 * Converts a standard compare result (-1, 0, 1) to <code>true</code> or <code>false</code>
@@ -136,7 +137,34 @@
 		throw new IllegalArgumentException("bad value class (" + v.getClass().getName() + ")");
 	}
 
-	public final int getNumberOfOperands() {
-		return 2;
-	}
+	public static final Function EqualEval = new RelationalOperationEval() {
+		protected boolean convertComparisonResult(int cmpResult) {
+			return cmpResult == 0;
+		}
+	};
+	public static final Function GreaterEqualEval = new RelationalOperationEval() {
+		protected boolean convertComparisonResult(int cmpResult) {
+			return cmpResult >= 0;
+		}
+	};
+	public static final Function GreaterThanEval = new RelationalOperationEval() {
+		protected boolean convertComparisonResult(int cmpResult) {
+			return cmpResult > 0;
+		}
+	};
+	public static final Function LessEqualEval = new RelationalOperationEval() {
+		protected boolean convertComparisonResult(int cmpResult) {
+			return cmpResult <= 0;
+		}
+	};
+	public static final Function LessThanEval = new RelationalOperationEval() {
+		protected boolean convertComparisonResult(int cmpResult) {
+			return cmpResult < 0;
+		}
+	};
+	public static final Function NotEqualEval = new RelationalOperationEval() {
+		protected boolean convertComparisonResult(int cmpResult) {
+			return cmpResult != 0;
+		}
+	};
 }

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=805971&r1=805970&r2=805971&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
Wed Aug 19 20:55:44 2009
@@ -17,12 +17,14 @@
 
 package org.apache.poi.hssf.record.formula.eval;
 
+import org.apache.poi.hssf.record.formula.functions.Function;
+
 /**
  * @author Josh Micich
  */
-abstract class TwoOperandNumericOperation implements OperationEval {
+public abstract class TwoOperandNumericOperation implements Function {
 
-	protected final double singleOperandEvaluate(ValueEval arg, int srcCellRow, short srcCellCol)
throws EvaluationException {
+	protected final double singleOperandEvaluate(ValueEval arg, int srcCellRow, int srcCellCol)
throws EvaluationException {
 		ValueEval ve = OperandResolver.getSingleValue(arg, srcCellRow, srcCellCol);
 		return OperandResolver.coerceValueToDouble(ve);
 	}
@@ -35,7 +37,7 @@
 			result = evaluate(d0, d1);
 			if (result == 0.0) { // this '==' matches +0.0 and -0.0
 				// Excel converts -0.0 to +0.0 for '*', '/', '%', '+' and '^'
-				if (!(this instanceof SubtractEval)) {
+				if (!(this instanceof SubtractEvalClass)) {
 					return NumberEval.ZERO;
 				}
 			}
@@ -48,7 +50,37 @@
 		return new NumberEval(result);
 	}
 	protected abstract double evaluate(double d0, double d1) throws EvaluationException;
-	public final int getNumberOfOperands() {
-		return 2;
+
+	public static final Function AddEval = new TwoOperandNumericOperation() {
+		protected double evaluate(double d0, double d1) {
+			return d0+d1;
+		}
+	};
+	public static final Function DivideEval = new TwoOperandNumericOperation() {
+		protected double evaluate(double d0, double d1) throws EvaluationException {
+			if (d1 == 0.0) {
+				throw new EvaluationException(ErrorEval.DIV_ZERO);
+			}
+			return d0/d1;
+		}
+	};
+	public static final Function MultiplyEval = new TwoOperandNumericOperation() {
+		protected double evaluate(double d0, double d1) {
+			return d0*d1;
+		}
+	};
+	public static final Function PowerEval = new TwoOperandNumericOperation() {
+		protected double evaluate(double d0, double d1) {
+			return Math.pow(d0, d1);
+		}
+	};
+	private static final class SubtractEvalClass extends TwoOperandNumericOperation {
+		public SubtractEvalClass() {
+			//
+		}
+		protected double evaluate(double d0, double d1) {
+			return d0-d1;
+		}
 	}
+	public static final Function SubtractEval = new SubtractEvalClass();
 }

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=805971&r1=805970&r2=805971&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 Wed Aug
19 20:55:44 2009
@@ -17,14 +17,16 @@
 
 package org.apache.poi.hssf.record.formula.eval;
 
+import org.apache.poi.hssf.record.formula.functions.Function;
+
 
 /**
  * @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt;
  *
  */
-public final class UnaryMinusEval implements OperationEval {
+public final class UnaryMinusEval implements Function {
 
-	public static final OperationEval instance = new UnaryMinusEval();
+	public static final Function instance = new UnaryMinusEval();
 
 	private UnaryMinusEval() {
 		// enforce singleton
@@ -46,8 +48,4 @@
 		}
 		return new NumberEval(-d);
 	}
-
-	public int getNumberOfOperands() {
-		return 1;
-	}
 }

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=805971&r1=805970&r2=805971&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 Wed Aug
19 20:55:44 2009
@@ -17,14 +17,16 @@
 
 package org.apache.poi.hssf.record.formula.eval;
 
+import org.apache.poi.hssf.record.formula.functions.Function;
+
 
 /**
  * @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt;
  *
  */
-public final class UnaryPlusEval implements OperationEval {
+public final class UnaryPlusEval implements Function {
 
-	public static final OperationEval instance = new UnaryPlusEval();
+	public static final Function instance = new UnaryPlusEval();
 
 	private UnaryPlusEval() {
 		// enforce singleton
@@ -49,8 +51,4 @@
 		}
 		return new NumberEval(+d);
 	}
-
-	public int getNumberOfOperands() {
-		return 1;
-	}
 }

Modified: poi/trunk/src/java/org/apache/poi/ss/formula/OperationEvaluatorFactory.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/OperationEvaluatorFactory.java?rev=805971&r1=805970&r2=805971&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/OperationEvaluatorFactory.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/OperationEvaluatorFactory.java Wed Aug 19
20:55:44 2009
@@ -39,24 +39,17 @@
 import org.apache.poi.hssf.record.formula.SubtractPtg;
 import org.apache.poi.hssf.record.formula.UnaryMinusPtg;
 import org.apache.poi.hssf.record.formula.UnaryPlusPtg;
-import org.apache.poi.hssf.record.formula.eval.AddEval;
 import org.apache.poi.hssf.record.formula.eval.ConcatEval;
-import org.apache.poi.hssf.record.formula.eval.DivideEval;
-import org.apache.poi.hssf.record.formula.eval.EqualEval;
 import org.apache.poi.hssf.record.formula.eval.FunctionEval;
-import org.apache.poi.hssf.record.formula.eval.GreaterEqualEval;
-import org.apache.poi.hssf.record.formula.eval.GreaterThanEval;
-import org.apache.poi.hssf.record.formula.eval.LessEqualEval;
-import org.apache.poi.hssf.record.formula.eval.LessThanEval;
-import org.apache.poi.hssf.record.formula.eval.MultiplyEval;
-import org.apache.poi.hssf.record.formula.eval.NotEqualEval;
 import org.apache.poi.hssf.record.formula.eval.OperationEval;
 import org.apache.poi.hssf.record.formula.eval.PercentEval;
-import org.apache.poi.hssf.record.formula.eval.PowerEval;
 import org.apache.poi.hssf.record.formula.eval.RangeEval;
-import org.apache.poi.hssf.record.formula.eval.SubtractEval;
+import org.apache.poi.hssf.record.formula.eval.RelationalOperationEval;
+import org.apache.poi.hssf.record.formula.eval.TwoOperandNumericOperation;
 import org.apache.poi.hssf.record.formula.eval.UnaryMinusEval;
 import org.apache.poi.hssf.record.formula.eval.UnaryPlusEval;
+import org.apache.poi.hssf.record.formula.eval.ValueEval;
+import org.apache.poi.hssf.record.formula.functions.Function;
 
 /**
  * This class creates <tt>OperationEval</tt> instances to help evaluate <tt>OperationPtg</tt>
@@ -74,28 +67,54 @@
 
 	private static Map<Class<? extends Ptg>, OperationEval> initialiseInstancesMap()
{
 		Map<Class<? extends Ptg>, OperationEval> m = new HashMap<Class<? extends
Ptg>, OperationEval>(32);
-		m.put(EqualPtg.class, EqualEval.instance);
 
-		m.put(EqualPtg.class, EqualEval.instance);
-		m.put(GreaterEqualPtg.class, GreaterEqualEval.instance);
-		m.put(GreaterThanPtg.class, GreaterThanEval.instance);
-		m.put(LessEqualPtg.class, LessEqualEval.instance);
-		m.put(LessThanPtg.class, LessThanEval.instance);
-		m.put(NotEqualPtg.class, NotEqualEval.instance);
-
-		m.put(ConcatPtg.class, ConcatEval.instance);
-		m.put(AddPtg.class, AddEval.instance);
-		m.put(DividePtg.class, DivideEval.instance);
-		m.put(MultiplyPtg.class, MultiplyEval.instance);
-		m.put(PercentPtg.class, PercentEval.instance);
-		m.put(PowerPtg.class, PowerEval.instance);
-		m.put(SubtractPtg.class, SubtractEval.instance);
-		m.put(UnaryMinusPtg.class, UnaryMinusEval.instance);
-		m.put(UnaryPlusPtg.class, UnaryPlusEval.instance);
-		m.put(RangePtg.class, RangeEval.instance);
+		put(m, 2, EqualPtg.class, RelationalOperationEval.EqualEval);
+		put(m, 2, GreaterEqualPtg.class, RelationalOperationEval.GreaterEqualEval);
+		put(m, 2, GreaterThanPtg.class, RelationalOperationEval.GreaterThanEval);
+		put(m, 2, LessEqualPtg.class, RelationalOperationEval.LessEqualEval);
+		put(m, 2, LessThanPtg.class, RelationalOperationEval.LessThanEval);
+		put(m, 2, NotEqualPtg.class, RelationalOperationEval.NotEqualEval);
+
+		put(m, 2, ConcatPtg.class, ConcatEval.instance);
+		put(m, 2, AddPtg.class, TwoOperandNumericOperation.AddEval);
+		put(m, 2, DividePtg.class, TwoOperandNumericOperation.DivideEval);
+		put(m, 2, MultiplyPtg.class, TwoOperandNumericOperation.MultiplyEval);
+		put(m, 1, PercentPtg.class, PercentEval.instance);
+		put(m, 2, PowerPtg.class, TwoOperandNumericOperation.PowerEval);
+		put(m, 2, SubtractPtg.class, TwoOperandNumericOperation.SubtractEval);
+		put(m, 1, UnaryMinusPtg.class, UnaryMinusEval.instance);
+		put(m, 1, UnaryPlusPtg.class, UnaryPlusEval.instance);
+		put(m, 2, RangePtg.class, RangeEval.instance);
 		return m;
 	}
 
+	private static void put(Map<Class<? extends Ptg>, OperationEval> m, int argCount,
+			Class<? extends Ptg> ptgClass, Function instance) {
+		m.put(ptgClass, new OperationFunctionEval(instance, argCount));
+	}
+
+	/**
+	 * Simple adapter from {@link OperationEval} to {@link Function}
+	 */
+	private static final class OperationFunctionEval implements OperationEval {
+
+		private final Function _function;
+		private final int _numberOfOperands;
+
+		public OperationFunctionEval(Function function, int argCount) {
+			_function = function;
+			_numberOfOperands = argCount;
+		}
+
+		public ValueEval evaluate(ValueEval[] args, int rowIndex, short columnIndex) {
+			return _function.evaluate(args, rowIndex, columnIndex);
+		}
+
+		public int getNumberOfOperands() {
+			return _numberOfOperands;
+		}
+	}
+
 	/**
 	 * returns the OperationEval concrete impl instance corresponding
 	 * to the supplied operationPtg

Added: poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/EvalInstances.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/EvalInstances.java?rev=805971&view=auto
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/EvalInstances.java (added)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/EvalInstances.java Wed
Aug 19 20:55:44 2009
@@ -0,0 +1,53 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.hssf.record.formula.eval;
+
+import org.apache.poi.hssf.record.formula.functions.Function;
+
+/**
+ * Collects eval instances for easy access by tests in this package
+ *
+ * @author Josh Micich
+ */
+final class EvalInstances {
+	private EvalInstances() {
+		// no instances of this class
+	}
+
+	public static final Function Add = TwoOperandNumericOperation.AddEval;
+	public static final Function Subtract = TwoOperandNumericOperation.SubtractEval;
+	public static final Function Multiply = TwoOperandNumericOperation.MultiplyEval;
+	public static final Function Divide = TwoOperandNumericOperation.DivideEval;
+
+	public static final Function Power = TwoOperandNumericOperation.PowerEval;
+
+	public static final Function Percent = PercentEval.instance;
+
+	public static final Function UnaryMinus = UnaryMinusEval.instance;
+	public static final Function UnaryPlus = UnaryPlusEval.instance;
+
+	public static final Function Equal = RelationalOperationEval.EqualEval;
+	public static final Function LessThan = RelationalOperationEval.LessThanEval;
+	public static final Function LessEqual = RelationalOperationEval.LessEqualEval;
+	public static final Function GreaterThan = RelationalOperationEval.GreaterThanEval;
+	public static final Function GreaterEqual = RelationalOperationEval.GreaterEqualEval;
+	public static final Function NotEqual = RelationalOperationEval.NotEqualEval;
+
+	public static final Function Range = RangeEval.instance;
+	public static final Function Concat = ConcatEval.instance;
+}

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/TestDivideEval.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/TestDivideEval.java?rev=805971&r1=805970&r2=805971&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/TestDivideEval.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/TestDivideEval.java Wed
Aug 19 20:55:44 2009
@@ -34,7 +34,7 @@
 			arg0, arg1,
 		};
 
-		double result = NumericFunctionInvoker.invoke(DivideEval.instance, args, 0, 0);
+		double result = NumericFunctionInvoker.invoke(EvalInstances.Divide, args, 0, 0);
 
 		assertEquals(expectedResult, result, 0);
 	}
@@ -56,7 +56,7 @@
 		ValueEval[] args = {
 			new NumberEval(5), NumberEval.ZERO,
 		};
-		ValueEval result = DivideEval.instance.evaluate(args, 0, (short) 0);
+		ValueEval result = EvalInstances.Divide.evaluate(args, 0, (short) 0);
 		assertEquals(ErrorEval.DIV_ZERO, result);
 	}
 }

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/TestEqualEval.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/TestEqualEval.java?rev=805971&r1=805970&r2=805971&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/TestEqualEval.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/TestEqualEval.java Wed
Aug 19 20:55:44 2009
@@ -21,6 +21,7 @@
 import junit.framework.TestCase;
 
 import org.apache.poi.hssf.record.formula.functions.EvalFactory;
+import org.apache.poi.hssf.record.formula.functions.Function;
 
 /**
  * Test for {@link EqualEval}
@@ -28,6 +29,8 @@
  * @author Josh Micich
  */
 public final class TestEqualEval extends TestCase {
+	// convenient access to namepace
+	private static final EvalInstances EI = null;
 
 	/**
 	 * Test for bug observable at svn revision 692218 (Sep 2008)<br/>
@@ -40,7 +43,7 @@
 			EvalFactory.createAreaEval("B1:B1", values),
 			BoolEval.FALSE,
 		};
-		ValueEval result = EqualEval.instance.evaluate(args, 10, (short)20);
+		ValueEval result = evaluate(EI.Equal, args, 10, 10);
 		if (result instanceof ErrorEval) {
 			if (result == ErrorEval.VALUE_INVALID) {
 				throw new AssertionFailedError("Identified bug in evaluation of 1x1 area");
@@ -58,7 +61,7 @@
 			new StringEval(""),
 			BlankEval.INSTANCE,
 		};
-		ValueEval result = EqualEval.instance.evaluate(args, 10, (short)20);
+		ValueEval result = evaluate(EI.Equal, args, 10, 10);
 		assertEquals(BoolEval.class, result.getClass());
 		BoolEval be = (BoolEval) result;
 		if (!be.getBooleanValue()) {
@@ -71,17 +74,17 @@
 	 * Test for bug 46613 (observable at svn r737248)
 	 */
 	public void testStringInsensitive_bug46613() {
-		if (!evalStringCmp("abc", "aBc", EqualEval.instance)) {
+		if (!evalStringCmp("abc", "aBc", EI.Equal)) {
 			throw new AssertionFailedError("Identified bug 46613");
 		}
-		assertTrue(evalStringCmp("abc", "aBc", EqualEval.instance));
-		assertTrue(evalStringCmp("ABC", "azz", LessThanEval.instance));
-		assertTrue(evalStringCmp("abc", "AZZ", LessThanEval.instance));
-		assertTrue(evalStringCmp("ABC", "aaa", GreaterThanEval.instance));
-		assertTrue(evalStringCmp("abc", "AAA", GreaterThanEval.instance));
+		assertTrue(evalStringCmp("abc", "aBc", EI.Equal));
+		assertTrue(evalStringCmp("ABC", "azz", EI.LessThan));
+		assertTrue(evalStringCmp("abc", "AZZ", EI.LessThan));
+		assertTrue(evalStringCmp("ABC", "aaa", EI.GreaterThan));
+		assertTrue(evalStringCmp("abc", "AAA", EI.GreaterThan));
 	}
 
-	private static boolean evalStringCmp(String a, String b, OperationEval cmpOp) {
+	private static boolean evalStringCmp(String a, String b, Function cmpOp) {
 		ValueEval[] args = {
 			new StringEval(a),
 			new StringEval(b),
@@ -103,13 +106,12 @@
 	 */
 	public void testZeroEquality_bug47198() {
 		NumberEval zero = new NumberEval(0.0);
-		NumberEval mZero = (NumberEval) UnaryMinusEval.instance.evaluate(new ValueEval[] { zero,
}, 0,
-				(short) 0);
+		NumberEval mZero = (NumberEval) evaluate(UnaryMinusEval.instance, new ValueEval[] { zero,
}, 0, 0);
 		if (Double.doubleToLongBits(mZero.getNumberValue()) == 0x8000000000000000L) {
 			throw new AssertionFailedError("Identified bug 47198: unary minus should convert -0.0
to 0.0");
 		}
 		ValueEval[] args = { zero, mZero, };
-		BoolEval result = (BoolEval) EqualEval.instance.evaluate(args, 0, (short) 0);
+		BoolEval result = (BoolEval) evaluate(EI.Equal, args, 0, 0);
 		if (!result.getBooleanValue()) {
 			throw new AssertionFailedError("Identified bug 47198: -0.0 != 0.0");
 		}
@@ -124,9 +126,13 @@
 		assertEquals("1.0055", b.getStringValue());
 
 		ValueEval[] args = { a, b, };
-		BoolEval result = (BoolEval) EqualEval.instance.evaluate(args, 0, (short) 0);
+		BoolEval result = (BoolEval) evaluate(EI.Equal, args, 0, 0);
 		if (!result.getBooleanValue()) {
 			throw new AssertionFailedError("Identified bug 47598: 1+1.0028-0.9973 != 1.0055");
 		}
 	}
+
+	private static ValueEval evaluate(Function oper, ValueEval[] args, int srcRowIx, int srcColIx)
{
+		return oper.evaluate(args, srcRowIx, (short) srcColIx);
+	}
 }

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/TestMinusZeroResult.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/TestMinusZeroResult.java?rev=805971&r1=805970&r2=805971&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/TestMinusZeroResult.java
(original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/TestMinusZeroResult.java
Wed Aug 19 20:55:44 2009
@@ -20,6 +20,7 @@
 import junit.framework.ComparisonFailure;
 import junit.framework.TestCase;
 
+import org.apache.poi.hssf.record.formula.functions.Function;
 import org.apache.poi.util.HexDump;
 
 /**
@@ -39,6 +40,8 @@
 public final class TestMinusZeroResult extends TestCase {
 	private static final double MINUS_ZERO = -0.0;
 
+	// convenient access to namepace
+	private static final EvalInstances EI = null;
 
 	public void testSimpleOperators() {
 
@@ -46,15 +49,15 @@
 		checkEval(MINUS_ZERO, UnaryPlusEval.instance, MINUS_ZERO);
 
 		// most simple operators convert -0.0 to +0.0
-		checkEval(0.0, UnaryMinusEval.instance, 0.0);
-		checkEval(0.0, PercentEval.instance, MINUS_ZERO);
-		checkEval(0.0, MultiplyEval.instance, MINUS_ZERO, 1.0);
-		checkEval(0.0, DivideEval.instance, MINUS_ZERO, 1.0);
-		checkEval(0.0, PowerEval.instance, MINUS_ZERO, 1.0);
+		checkEval(0.0, EI.UnaryMinus, 0.0);
+		checkEval(0.0, EI.Percent, MINUS_ZERO);
+		checkEval(0.0, EI.Multiply, MINUS_ZERO, 1.0);
+		checkEval(0.0, EI.Divide, MINUS_ZERO, 1.0);
+		checkEval(0.0, EI.Power, MINUS_ZERO, 1.0);
 
 		// but SubtractEval does not convert -0.0, so '-' and '+' work like java
-		checkEval(MINUS_ZERO, SubtractEval.instance, MINUS_ZERO, 0.0); // this is the main point
of bug 47198
-		checkEval(0.0, AddEval.instance, MINUS_ZERO, 0.0);
+		checkEval(MINUS_ZERO, EI.Subtract, MINUS_ZERO, 0.0); // this is the main point of bug 47198
+		checkEval(0.0, EI.Add, MINUS_ZERO, 0.0);
 	}
 
 	/**
@@ -62,9 +65,9 @@
 	 * gets to the comparison operator)
 	 */
 	public void testComparisonOperators() {
-		checkEval(false, EqualEval.instance, 0.0, MINUS_ZERO);
-		checkEval(true, GreaterThanEval.instance, 0.0, MINUS_ZERO);
-		checkEval(true, LessThanEval.instance, MINUS_ZERO, 0.0);
+		checkEval(false, EI.Equal, 0.0, MINUS_ZERO);
+		checkEval(true, EI.GreaterThan, 0.0, MINUS_ZERO);
+		checkEval(true, EI.LessThan, MINUS_ZERO, 0.0);
 	}
 
 	public void testTextRendering() {
@@ -78,20 +81,20 @@
 	 */
 	private static void confirmTextRendering(String expRendering, double d) {
 		ValueEval[] args = { StringEval.EMPTY_INSTANCE, new NumberEval(d), };
-		StringEval se = (StringEval) ConcatEval.instance.evaluate(args, -1, (short)-1);
+		StringEval se = (StringEval) EI.Concat.evaluate(args, -1, (short)-1);
 		String result = se.getStringValue();
 		assertEquals(expRendering, result);
 	}
 
-	private static void checkEval(double expectedResult, OperationEval instance, double... dArgs)
{
+	private static void checkEval(double expectedResult, Function instance, double... dArgs)
{
 		NumberEval result = (NumberEval) evaluate(instance, dArgs);
 		assertDouble(expectedResult, result.getNumberValue());
 	}
-	private static void checkEval(boolean expectedResult, OperationEval instance, double...
dArgs) {
+	private static void checkEval(boolean expectedResult, Function instance, double... dArgs)
{
 		BoolEval result = (BoolEval) evaluate(instance, dArgs);
 		assertEquals(expectedResult, result.getBooleanValue());
 	}
-	private static ValueEval evaluate(OperationEval instance, double... dArgs) {
+	private static ValueEval evaluate(Function instance, double... dArgs) {
 		ValueEval[] evalArgs;
 		evalArgs = new ValueEval[dArgs.length];
 		for (int i = 0; i < evalArgs.length; i++) {

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/TestPercentEval.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/TestPercentEval.java?rev=805971&r1=805970&r2=805971&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/TestPercentEval.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/TestPercentEval.java Wed
Aug 19 20:55:44 2009
@@ -41,8 +41,7 @@
 			arg,
 		};
 
-		OperationEval opEval = PercentEval.instance;
-		double result = NumericFunctionInvoker.invoke(opEval, args, 0, 0);
+		double result = NumericFunctionInvoker.invoke(PercentEval.instance, args, 0, 0);
 
 		assertEquals(expectedResult, result, 0);
 	}

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/functions/NumericFunctionInvoker.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/functions/NumericFunctionInvoker.java?rev=805971&r1=805970&r2=805971&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/functions/NumericFunctionInvoker.java
(original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/functions/NumericFunctionInvoker.java
Wed Aug 19 20:55:44 2009
@@ -21,7 +21,6 @@
 
 import org.apache.poi.hssf.record.formula.eval.ErrorEval;
 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;
 
@@ -53,13 +52,7 @@
 	 * result causes the current junit test to fail.
 	 */
 	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()
-					+ ") failed: " + e.getMessage());
-		}
-
+		return invoke(f, args, -1, -1);
 	}
 	/**
 	 * Invokes the specified operator with the arguments.
@@ -67,30 +60,22 @@
 	 * 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, ValueEval[] args, int srcCellRow, int srcCellCol)
{
+	public static double invoke(Function 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()
 					+ ") failed: " + e.getMessage());
 		}
-
 	}
 	/**
 	 * Formats nicer error messages for the junit output
 	 */
-	private static double invokeInternal(Object target, ValueEval[] args, int srcCellRow, int
srcCellCol)
+	private static double invokeInternal(Function target, ValueEval[] args, int srcCellRow,
int srcCellCol)
 				throws NumericEvalEx {
 		ValueEval evalResult;
-		// TODO - make OperationEval extend Function
 		try {
-			if (target instanceof Function) {
-				Function ff = (Function) target;
-				evalResult = ff.evaluate(args, srcCellRow, (short)srcCellCol);
-			} else {
-				OperationEval ff = (OperationEval) target;
-				evalResult = ff.evaluate(args, srcCellRow, (short)srcCellCol);
-			}
+			evalResult = target.evaluate(args, srcCellRow, (short)srcCellCol);
 		} catch (NotImplementedException e) {
 			throw new NumericEvalEx("Not implemented:" + e.getMessage());
 		}



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


Mime
View raw message