poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r805088 - in /poi/trunk/src/java/org/apache/poi: hssf/record/formula/eval/ hssf/record/formula/functions/ ss/formula/
Date Mon, 17 Aug 2009 18:29:41 GMT
Author: josh
Date: Mon Aug 17 18:29:40 2009
New Revision: 805088

URL: http://svn.apache.org/viewvc?rev=805088&view=rev
Log:
Combined FuncVarEval into FunctionEval. Simplified selection of free ref functions.

Added:
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/UserDefinedFunction.java
      - copied, changed from r805081, poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/ExternalFunction.java
Removed:
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/ExternalFunction.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/FuncVarEval.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/Indirect.java
    poi/trunk/src/java/org/apache/poi/ss/formula/OperationEvaluatorFactory.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=805088&r1=805087&r2=805088&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 Mon Aug 17
18:29:40 2009
@@ -17,17 +17,16 @@
 
 package org.apache.poi.hssf.record.formula.eval;
 
-import java.util.HashMap;
-import java.util.Map;
-
+import org.apache.poi.hssf.record.formula.AbstractFunctionPtg;
 import org.apache.poi.hssf.record.formula.function.FunctionMetadata;
 import org.apache.poi.hssf.record.formula.function.FunctionMetadataRegistry;
 import org.apache.poi.hssf.record.formula.functions.*;
+import org.apache.poi.ss.formula.eval.NotImplementedException;
 
 /**
  * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
  */
-public abstract class FunctionEval implements OperationEval {
+public final class FunctionEval implements OperationEval {
 	/**
 	 * Some function IDs that require special treatment
 	 */
@@ -44,35 +43,27 @@
 	// convenient access to namespace
 	private static final FunctionID ID = null;
 
-	protected static final Function[] functions ;
-
-	private static Map<Integer, FreeRefFunction> freeRefFunctionsByIdMap;
-
-	static {
-		Map<Integer, FreeRefFunction> m = new HashMap<Integer, FreeRefFunction>();
-		m.put(createFRFKey(ID.INDIRECT), new Indirect());
-		m.put(createFRFKey(ID.EXTERNAL_FUNC), new ExternalFunction());
-		freeRefFunctionsByIdMap = m;
-		functions = produceFunctions();
-	}
-	private static Integer createFRFKey(int functionIndex) {
-		return new Integer(functionIndex);
-	}
+	protected static final Function[] functions = produceFunctions();
 
 
-	public Function getFunction() {
+	/**
+	 * @return <code>null</code> if specified function
+	 */
+	private Function getFunction() {
 		short fidx = getFunctionIndex();
 		return functions[fidx];
 	}
 	public boolean isFreeRefFunction() {
-		return freeRefFunctionsByIdMap.containsKey(createFRFKey(getFunctionIndex()));
+		return getFreeRefFunction() != null;
 	}
 	public FreeRefFunction getFreeRefFunction() {
-		return freeRefFunctionsByIdMap.get(createFRFKey(getFunctionIndex()));
+		switch (getFunctionIndex()) {
+			case FunctionID.INDIRECT: return Indirect.instance;
+			case FunctionID.EXTERNAL_FUNC: return UserDefinedFunction.instance;
+		}
+		return null;
 	}
 
-	public abstract short getFunctionIndex();
-
 	private static Function[] produceFunctions() {
 		Function[] retval = new Function[368];
 
@@ -245,4 +236,26 @@
 		}
 		return retval;
 	}
+
+	private AbstractFunctionPtg _delegate;
+
+	public FunctionEval(AbstractFunctionPtg funcPtg) {
+		_delegate = funcPtg;
+	}
+
+	public Eval evaluate(Eval[] operands, int srcRow, short srcCol) {
+		Function f = getFunction();
+		if (f == null) {
+			throw new NotImplementedException("FuncIx=" + getFunctionIndex());
+		}
+		return f.evaluate(operands, srcRow, srcCol);
+	}
+
+	public int getNumberOfOperands() {
+		return _delegate.getNumberOfOperands();
+	}
+
+	private short getFunctionIndex() {
+		return _delegate.getFunctionIndex();
+	}
 }

Copied: poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/UserDefinedFunction.java
(from r805081, poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/ExternalFunction.java)
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/UserDefinedFunction.java?p2=poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/UserDefinedFunction.java&p1=poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/ExternalFunction.java&r1=805081&r2=805088&rev=805088&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/ExternalFunction.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/eval/UserDefinedFunction.java Mon
Aug 17 18:29:40 2009
@@ -22,23 +22,28 @@
 import org.apache.poi.ss.formula.EvaluationWorkbook;
 import org.apache.poi.ss.formula.eval.NotImplementedException;
 /**
- * 
- * Common entry point for all user-defined (non-built-in) functions (where 
+ *
+ * Common entry point for all user-defined (non-built-in) functions (where
  * <tt>AbstractFunctionPtg.field_2_fnc_index</tt> == 255)
- * 
- * TODO rename to UserDefinedFunction
+ *
  * @author Josh Micich
  */
-final class ExternalFunction implements FreeRefFunction {
+final class UserDefinedFunction implements FreeRefFunction {
 
-	public ValueEval evaluate(Eval[] args, EvaluationWorkbook workbook, 
+	public static final FreeRefFunction instance = new UserDefinedFunction();
+
+	private UserDefinedFunction() {
+		// enforce singleton
+	}
+
+	public ValueEval evaluate(Eval[] args, EvaluationWorkbook workbook,
 			int srcCellSheet, int srcCellRow,int srcCellCol) {
-		
+
 		int nIncomingArgs = args.length;
 		if(nIncomingArgs < 1) {
 			throw new RuntimeException("function name argument missing");
 		}
-		
+
 		Eval nameArg = args[0];
 		FreeRefFunction targetFunc;
 		if (nameArg instanceof NameEval) {
@@ -78,7 +83,7 @@
 			System.out.println("received call to internal user defined function  (" + functionName
+ ")");
 		}
 		// TODO find the implementation for the user defined function
-		
+
 		throw new NotImplementedException(functionName);
 	}
 }

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/Indirect.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/Indirect.java?rev=805088&r1=805087&r2=805088&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/Indirect.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/Indirect.java Mon Aug
17 18:29:40 2009
@@ -40,6 +40,12 @@
  */
 public final class Indirect implements FreeRefFunction {
 
+	public static final FreeRefFunction instance = new Indirect();
+
+	private Indirect() {
+		// enforce singleton
+	}
+
 	public ValueEval evaluate(Eval[] args, EvaluationWorkbook workbook, int srcCellSheet, int
srcCellRow, int srcCellCol) {
 		// TODO - implement INDIRECT()
 		throw new NotImplementedException("INDIRECT");

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=805088&r1=805087&r2=805088&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 Mon Aug 17
18:29:40 2009
@@ -20,12 +20,11 @@
 import java.util.HashMap;
 import java.util.Map;
 
+import org.apache.poi.hssf.record.formula.AbstractFunctionPtg;
 import org.apache.poi.hssf.record.formula.AddPtg;
 import org.apache.poi.hssf.record.formula.ConcatPtg;
 import org.apache.poi.hssf.record.formula.DividePtg;
 import org.apache.poi.hssf.record.formula.EqualPtg;
-import org.apache.poi.hssf.record.formula.FuncPtg;
-import org.apache.poi.hssf.record.formula.FuncVarPtg;
 import org.apache.poi.hssf.record.formula.GreaterEqualPtg;
 import org.apache.poi.hssf.record.formula.GreaterThanPtg;
 import org.apache.poi.hssf.record.formula.LessEqualPtg;
@@ -44,7 +43,7 @@
 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.FuncVarEval;
+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;
@@ -114,11 +113,8 @@
 			return  result;
 		}
 
-		if (ptgClass == FuncPtg.class) {
-			return new FuncVarEval((FuncPtg)ptg);
-		}
-		if (ptgClass == FuncVarPtg.class) {
-			return new FuncVarEval((FuncVarPtg)ptg);
+		if (ptg instanceof AbstractFunctionPtg) {
+			return new FunctionEval((AbstractFunctionPtg)ptg);
 		}
 		throw new RuntimeException("Unexpected operation ptg class (" + ptgClass.getName() + ")");
 	}



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


Mime
View raw message