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/11 09:16:21 UTC

svn commit: r694153 - in /poi/trunk/src/java/org/apache/poi/hssf/record/formula: eval/ functions/

Author: josh
Date: Thu Sep 11 00:16:20 2008
New Revision: 694153

URL: http://svn.apache.org/viewvc?rev=694153&view=rev
Log:
Refactoring MultiOperandNumericFunction - removed Ref2DEval.

Added:
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/MinaMaxa.java
Removed:
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/Ref2DEval.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/ValueEvalToNumericXlator.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/Maxa.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/Mina.java
Modified:
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/FunctionEval.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/AggregateFunction.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/MultiOperandNumericFunction.java

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/FunctionEval.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/FunctionEval.java?rev=694153&r1=694152&r2=694153&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/FunctionEval.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/FunctionEval.java Thu Sep 11 00:16:20 2008
@@ -420,8 +420,8 @@
         retval[359] = new Hyperlink(); // HYPERLINK
         retval[360] = new NotImplementedFunction(); // PHONETIC
         retval[361] = new Averagea(); // AVERAGEA
-        retval[362] = new Maxa(); // MAXA
-        retval[363] = new Mina(); // MINA
+        retval[362] = MinaMaxa.MAXA;
+        retval[363] = MinaMaxa.MINA;
         retval[364] = new Stdevpa(); // STDEVPA
         retval[365] = new Varpa(); // VARPA
         retval[366] = new Stdeva(); // STDEVA

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/AggregateFunction.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/AggregateFunction.java?rev=694153&r1=694152&r2=694153&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/AggregateFunction.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/AggregateFunction.java Thu Sep 11 00:16:20 2008
@@ -19,22 +19,17 @@
 
 import org.apache.poi.hssf.record.formula.eval.ErrorEval;
 import org.apache.poi.hssf.record.formula.eval.EvaluationException;
-import org.apache.poi.hssf.record.formula.eval.ValueEval;
-import org.apache.poi.hssf.record.formula.eval.ValueEvalToNumericXlator;
 
 /**
  * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
  *
  */
 public abstract class AggregateFunction extends MultiOperandNumericFunction {
-	private static final ValueEvalToNumericXlator DEFAULT_NUM_XLATOR =
-		new ValueEvalToNumericXlator(0);
 
-	protected ValueEval attemptXlateToNumeric(ValueEval ve) {
-		return DEFAULT_NUM_XLATOR.attemptXlateToNumeric(ve);
+	protected AggregateFunction() {
+		super(false, false);
 	}
 
-
 	/* ---------------------------------------------------------------------- */
 
 	public static final Function AVEDEV = new AggregateFunction() {

Added: poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/MinaMaxa.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/MinaMaxa.java?rev=694153&view=auto
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/MinaMaxa.java (added)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/MinaMaxa.java Thu Sep 11 00:16:20 2008
@@ -0,0 +1,40 @@
+/* ====================================================================
+   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.functions;
+
+/**
+ * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
+ * 
+ */
+public abstract class MinaMaxa extends MultiOperandNumericFunction {
+
+	protected MinaMaxa() {
+		super(true, true);
+	}
+
+	public static final Function MAXA = new MinaMaxa() {
+		protected double evaluate(double[] values) {
+			return values.length > 0 ? MathX.max(values) : 0;
+		}
+	};
+	public static final Function MINA = new MinaMaxa() {
+		protected double evaluate(double[] values) {
+			return values.length > 0 ? MathX.min(values) : 0;
+		}
+	};
+}

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/MultiOperandNumericFunction.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/MultiOperandNumericFunction.java?rev=694153&r1=694152&r2=694153&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/MultiOperandNumericFunction.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/MultiOperandNumericFunction.java Thu Sep 11 00:16:20 2008
@@ -19,13 +19,14 @@
 
 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.EvaluationException;
 import org.apache.poi.hssf.record.formula.eval.NumberEval;
-import org.apache.poi.hssf.record.formula.eval.NumericValueEval;
-import org.apache.poi.hssf.record.formula.eval.Ref2DEval;
+import org.apache.poi.hssf.record.formula.eval.OperandResolver;
 import org.apache.poi.hssf.record.formula.eval.RefEval;
+import org.apache.poi.hssf.record.formula.eval.StringEval;
 import org.apache.poi.hssf.record.formula.eval.ValueEval;
 
 /**
@@ -35,6 +36,16 @@
  * where the order of operands does not matter
  */
 public abstract class MultiOperandNumericFunction implements Function {
+
+	private final boolean _isReferenceBoolCounted;
+	private final boolean _isBlankCounted;
+
+	protected MultiOperandNumericFunction(boolean isReferenceBoolCounted, boolean isBlankCounted) {
+		_isReferenceBoolCounted = isReferenceBoolCounted;
+		_isBlankCounted = isBlankCounted;
+	}
+
+
 	static final double[] EMPTY_DOUBLE_ARRAY = { };
 
 	private static class DoubleList {
@@ -74,7 +85,7 @@
 	private static final int DEFAULT_MAX_NUM_OPERANDS = 30;
 
 	public final Eval evaluate(Eval[] args, int srcCellRow, short srcCellCol) {
-		
+
 		double d;
 		try {
 			double[] values = getNumberArray(args);
@@ -82,16 +93,16 @@
 		} catch (EvaluationException e) {
 			return e.getErrorEval();
 		}
-		
+
 		if (Double.isNaN(d) || Double.isInfinite(d))
 			return ErrorEval.NUM_ERROR;
-		
+
 		return new NumberEval(d);
 	}
 
 	protected abstract double evaluate(double[] values) throws EvaluationException;
-	
-	
+
+
 	/**
 	 * Maximum number of operands accepted by this function.
 	 * Subclasses may override to change default value.
@@ -132,54 +143,58 @@
 			int height = ae.getHeight();
 			for (int rrIx=0; rrIx<height; rrIx++) {
 				for (int rcIx=0; rcIx<width; rcIx++) {
-					ValueEval ve1 = ae.getRelativeValue(rrIx, rcIx);
-					 /*
-					 * TODO: For an AreaEval, we are constructing a RefEval
-					 * per element.
-					 * For now this is a tempfix solution since this may
-					 * require a more generic fix at the level of
-					 * HSSFFormulaEvaluator where we store an array
-					 * of RefEvals as the "values" array.
-					 */
-					RefEval re = new Ref2DEval(null, ve1);
-					ValueEval ve = attemptXlateToNumeric(re);
-					if (ve instanceof ErrorEval) {
-						throw new EvaluationException((ErrorEval)ve);
-					}
-					if (ve instanceof BlankEval) {
-						// note - blanks are ignored, so returned array will be smaller.
-						continue;
-					}
-					if (ve instanceof NumericValueEval) {
-						NumericValueEval nve = (NumericValueEval) ve;
-						temp.add(nve.getNumberValue());
-					} else {
-						throw new RuntimeException("Unexpected value class (" + ve.getClass().getName() + ")");
-					}
+					ValueEval ve = ae.getRelativeValue(rrIx, rcIx);
+					collectValue(ve, true, temp);
 				}
 			}
 			return;
 		}
-
-		// for ValueEvals other than AreaEval
-		ValueEval ve = attemptXlateToNumeric((ValueEval) operand);
-
-		if (ve instanceof NumericValueEval) {
-			NumericValueEval nve = (NumericValueEval) ve;
-			temp.add(nve.getNumberValue());
+		if (operand instanceof RefEval) {
+			RefEval re = (RefEval) operand;
+			collectValue(re.getInnerValueEval(), true, temp);
 			return;
 		}
-
-		if (ve instanceof BlankEval) {
-			// ignore blanks
+		collectValue((ValueEval)operand, false, temp);
+	}
+	private void collectValue(ValueEval ve, boolean isViaReference, DoubleList temp)  throws EvaluationException {
+		if (ve == null) {
+			throw new IllegalArgumentException("ve must not be null");
+		}
+		if (ve instanceof NumberEval) {
+			NumberEval ne = (NumberEval) ve;
+			temp.add(ne.getNumberValue());
 			return;
 		}
 		if (ve instanceof ErrorEval) {
-			throw new EvaluationException((ErrorEval)ve);
+			throw new EvaluationException((ErrorEval) ve);
 		}
-		throw new RuntimeException("Unexpected value class (" + ve.getClass().getName() + ")");
+		if (ve instanceof StringEval) {
+			if (isViaReference) {
+				// ignore all ref strings
+				return;
+			}
+			String s = ((StringEval) ve).getStringValue();
+			Double d = OperandResolver.parseDouble(s);
+			if(d == null) {
+				throw new EvaluationException(ErrorEval.VALUE_INVALID);
+			}
+			temp.add(d.doubleValue());
+			return;
+		}
+		if (ve instanceof BoolEval) {
+			if (!isViaReference || _isReferenceBoolCounted) {
+				BoolEval boolEval = (BoolEval) ve;
+				temp.add(boolEval.getNumberValue());
+			}
+			return;
+		}
+		if (ve == BlankEval.INSTANCE) {
+			if (_isBlankCounted) {
+				temp.add(0.0);
+			}
+			return;
+		}
+		throw new RuntimeException("Invalid ValueEval type passed for conversion: ("
+				+ ve.getClass() + ")");
 	}
-
-
-	protected abstract ValueEval attemptXlateToNumeric(ValueEval ve);
 }



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