]> source.dussan.org Git - poi.git/commitdiff
Simplifying ValueEval hierarchy, added comments to Function and OperationEval.
authorJosh Micich <josh@apache.org>
Wed, 31 Dec 2008 19:32:44 +0000 (19:32 +0000)
committerJosh Micich <josh@apache.org>
Wed, 31 Dec 2008 19:32:44 +0000 (19:32 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@730450 13f79535-47bb-0310-9956-ffa450edef68

15 files changed:
src/java/org/apache/poi/hssf/record/formula/OperationPtg.java
src/java/org/apache/poi/hssf/record/formula/eval/ConcatEval.java
src/java/org/apache/poi/hssf/record/formula/eval/Eval.java
src/java/org/apache/poi/hssf/record/formula/eval/FuncVarEval.java
src/java/org/apache/poi/hssf/record/formula/eval/NameEval.java
src/java/org/apache/poi/hssf/record/formula/eval/NameXEval.java
src/java/org/apache/poi/hssf/record/formula/eval/OperationEval.java
src/java/org/apache/poi/hssf/record/formula/eval/PercentEval.java
src/java/org/apache/poi/hssf/record/formula/eval/RangeEval.java
src/java/org/apache/poi/hssf/record/formula/eval/RelationalOperationEval.java
src/java/org/apache/poi/hssf/record/formula/eval/TwoOperandNumericOperation.java
src/java/org/apache/poi/hssf/record/formula/eval/UnaryMinusEval.java
src/java/org/apache/poi/hssf/record/formula/eval/UnaryPlusEval.java
src/java/org/apache/poi/hssf/record/formula/functions/Function.java
src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java

index f8acc81df315a8ac26eb40d50778d54847db42e6..ac0dcb45ffcd764573c476082a677928026ec0ce 100644 (file)
@@ -42,8 +42,4 @@ public abstract class OperationPtg extends Ptg {
     public byte getDefaultOperandClass() {
         return Ptg.CLASS_VALUE;
     }
-    public final int getType() {
-        // TODO remove "int getType();" from Eval hierarchy
-        throw new RuntimeException("remove this method");
-    }
 }
index e08d8112facb8a090660cb32c57b58ce1324c06c..3833f94b9e055dd31bdddc44e203601f3f1be228 100644 (file)
@@ -60,8 +60,4 @@ public final class ConcatEval implements OperationEval {
     public int getNumberOfOperands() {
         return delegate.getNumberOfOperands();
     }
-
-    public int getType() {
-        return delegate.getType();
-    }
 }
index 086b765a305234f144e443f8a07ee44628f65069..1cf9047f7e3745c88a55c62a9e061bebb926f08f 100644 (file)
@@ -25,5 +25,5 @@ package org.apache.poi.hssf.record.formula.eval;
  *  
  */
 public interface Eval {
-
+       // TODO - remove this interface
 }
index feeb456bd1da534066c0b985e4950b1db82e5f14..d30c353599a2419aa60aa9f1fcdd1b76b6ce6ad5 100644 (file)
@@ -44,10 +44,6 @@ public final class FuncVarEval extends FunctionEval {
         return delegate.getNumberOfOperands();
     }
 
-    public int getType() {
-        return delegate.getType();
-    }
-
     public short getFunctionIndex() {
         return delegate.getFunctionIndex();
     }
index 1f77deec2aad3922c5e5484bcba2d5e6ca91986f..9b5e21b184adc69fa047baccf06254523e857eb8 100755 (executable)
@@ -20,7 +20,7 @@ package org.apache.poi.hssf.record.formula.eval;
 /**
  * @author Josh Micich
  */
-public final class NameEval implements Eval {
+public final class NameEval implements ValueEval {
 
        private final String _functionName;
 
index b67f4eb3d3ea1fc0ff4b5147b498e5b75c2eecbb..d1b8ffef768067c25381a9e124cd328a15fa6d1d 100644 (file)
@@ -22,7 +22,7 @@ import org.apache.poi.hssf.record.formula.NameXPtg;
 /**
  * @author Josh Micich
  */
-public final class NameXEval implements Eval {
+public final class NameXEval implements ValueEval {
 
        private final NameXPtg _ptg;
 
index aefd5aefadb84dc3a02026c2dcd890f376e26368..22f7d364ae9a1ad0dd3161345ce9146895852358 100644 (file)
@@ -1,53 +1,39 @@
-/*
-* 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.
-*/
-/*
- * Created on May 8, 2005
- *
- */
+/* ====================================================================
+   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;
 
 /**
+ * Common interface for implementations of Excel formula operations.
+ * 
  * @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt;
  *  
  */
-public interface OperationEval extends Eval {
-
-    /*
-     * Read this, this will make your work easier when coding 
-     * an "evaluate()"
-     * 
-     * Things to note when implementing evaluate():
-     * 1. Check the length of operands
-     *    (use "switch(operands[x])" if possible)
-     * 
-     * 2. The possible Evals that you can get as args to evaluate are one of:
-     * NumericValueEval, StringValueEval, RefEval, AreaEval
-     * 3. If it is RefEval, the innerValueEval could be one of:
-     * NumericValueEval, StringValueEval, BlankEval
-     * 4. If it is AreaEval, each of the values could be one of:
-     * NumericValueEval, StringValueEval, BlankEval, RefEval
-     * 
-     * 5. For numeric functions/operations, keep the result in double
-     * till the end and before returning a new NumberEval, check to see
-     * if the double is a NaN - if NaN, return ErrorEval.ERROR_503 
-     */
-    public abstract Eval evaluate(Eval[] evals, int srcCellRow, short srcCellCol);
+public interface OperationEval {
 
-    public abstract int getNumberOfOperands();
+       /**
+        * @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);
 
-    public abstract int getType();
+       int getNumberOfOperands();
 }
index d8a579c42dcbad32deaa6caba6f2a0c891f304a1..a7c0cbad4bb2f3d222fa57806bf9911d91012955 100755 (executable)
@@ -27,6 +27,7 @@ public final class PercentEval implements OperationEval {
        public static final OperationEval instance = new PercentEval();
 
        private PercentEval() {
+               // enforce singleton
        }
 
        public Eval evaluate(Eval[] args, int srcRow, short srcCol) {
@@ -46,8 +47,4 @@ public final class PercentEval implements OperationEval {
        public int getNumberOfOperands() {
                return 1;
        }
-       public final int getType() {
-               // TODO - remove
-               throw new RuntimeException("obsolete code should not be called");
-       }
 }
index 9398108fdcef61c8e0b0910a8fd8f2f31c913da8..938241c281450bacc77949c31327319b457b9cae 100644 (file)
@@ -27,6 +27,7 @@ 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) {
@@ -64,8 +65,4 @@ public final class RangeEval implements OperationEval {
        public int getNumberOfOperands() {
                return 2;
        }
-
-       public int getType() {
-               throw new RuntimeException("obsolete code should not be called");
-       }
 }
index 058b00e1ad10500c9f48537b94c534ab5388e0bc..a1397f07f42a49953b3e964052a566a6098a38be 100644 (file)
@@ -137,9 +137,4 @@ public abstract class RelationalOperationEval implements OperationEval {
        public final int getNumberOfOperands() {
                return 2;
        }
-
-       public final int getType() {
-               // TODO - get rid of this method
-               throw new RuntimeException("Obsolete code - should not be called");
-       }
 }
index 0f2933faea074fc7638bbca72d0e7891bd25fc70..5e0a91cc6fe106d9b8e3588694dba9ac4ce99cd2 100644 (file)
@@ -22,10 +22,6 @@ package org.apache.poi.hssf.record.formula.eval;
  */
 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);
                return OperandResolver.coerceValueToDouble(ve);
index 780334ae8d1cc5b24eecbbfd17e6876f56d83873..1354a78a369f30fe6d4b705da930f9beb0f9f588 100644 (file)
@@ -27,6 +27,7 @@ public final class UnaryMinusEval implements OperationEval {
        public static final OperationEval instance = new UnaryMinusEval();
 
        private UnaryMinusEval() {
+               // enforce singleton
        }
 
        public Eval evaluate(Eval[] args, int srcRow, short srcCol) {
@@ -46,8 +47,4 @@ public final class UnaryMinusEval implements OperationEval {
        public int getNumberOfOperands() {
                return 1;
        }
-       public final int getType() {
-               // TODO - remove
-               throw new RuntimeException("obsolete code should not be called");
-       }
 }
index 831d342866d2c74f45066ede088d9bab1e13cfde..508fb0d060e94b6f09898117a287a4aaafacf22d 100644 (file)
@@ -1,19 +1,19 @@
-/*
-* 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.
-*/
+/* ====================================================================
+   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;
 
@@ -27,6 +27,7 @@ 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) {
@@ -52,8 +53,4 @@ public final class UnaryPlusEval implements OperationEval {
        public int getNumberOfOperands() {
                return 1;
        }
-
-       public int getType() {
-               throw new RuntimeException("obsolete code should not be called");
-       }
 }
index e33514a463ff9eaf00c1ce2cde1f6d48139448f9..3bce8ffa83ed3ac6219f992603b67f4e76491306 100644 (file)
@@ -1,33 +1,46 @@
-/*
-* 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.
-*/
-/*
- * Created on May 9, 2005
- *
- */
+/* ====================================================================
+   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;
 
+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.MissingArgEval;
+import org.apache.poi.hssf.record.formula.eval.ValueEval;
 
 /**
+ * Common interface for all implementations of Excel built-in functions.
+ * 
  * @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt;
- * Function serves as a marker interface.
  */
 public interface Function {
 
-    public Eval evaluate(Eval[] evals, int srcCellRow, short srcCellCol);
-
+       /**
+        * @param args the evaluated function 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>.
+        * <b>Note</b> - Excel uses the error code <i>#NUM!</i> instead of IEEE <i>NaN</i>, so when
+        * numeric functions evaluate to {@link Double#NaN} be sure to translate the result to {@link 
+        * ErrorEval#NUM_ERROR}. 
+        * 
+        */
+       Eval evaluate(Eval[] args, int srcRowIndex, short srcColumnIndex);
 }
index fac5715b1dd1771024c1dd740993c227ade1d3ee..f913f924e2edd615638f98f3a6dc38bb437c1be7 100644 (file)
@@ -263,7 +263,7 @@ public final class WorkbookEvaluator {
        // visibility raised for testing
        /* package */ ValueEval evaluateFormula(int sheetIndex, int srcRowNum, int srcColNum, Ptg[] ptgs, EvaluationTracker tracker) {
 
-               Stack<Eval> stack = new Stack<Eval>();
+               Stack<ValueEval> stack = new Stack<ValueEval>();
                for (int i = 0, iSize = ptgs.length; i < iSize; i++) {
 
                        // since we don't know how to handle these yet :(
@@ -287,7 +287,7 @@ public final class WorkbookEvaluator {
                        }
                        if (ptg instanceof MemErrPtg) { continue; }
 
-                       Eval opResult;
+                       ValueEval opResult;
                        if (ptg instanceof OperationPtg) {
                                OperationPtg optg = (OperationPtg) ptg;
 
@@ -296,11 +296,11 @@ public final class WorkbookEvaluator {
                                OperationEval operation = OperationEvaluatorFactory.create(optg);
 
                                int numops = operation.getNumberOfOperands();
-                               Eval[] ops = new Eval[numops];
+                               ValueEval[] ops = new ValueEval[numops];
 
                                // storing the ops in reverse order since they are popping
                                for (int j = numops - 1; j >= 0; j--) {
-                                       Eval p = stack.pop();
+                                       ValueEval p = stack.pop();
                                        ops[j] = p;
                                }
 //                             logDebug("invoke " + operation + " (nAgs=" + numops + ")");
@@ -318,7 +318,7 @@ public final class WorkbookEvaluator {
                        stack.push(opResult);
                }
 
-               ValueEval value = (ValueEval) stack.pop();
+               ValueEval value = stack.pop();
                if (!stack.isEmpty()) {
                        throw new IllegalStateException("evaluation stack not empty");
                }
@@ -359,7 +359,7 @@ public final class WorkbookEvaluator {
                return evaluationResult;
        }
 
-       private static Eval invokeOperation(OperationEval operation, Eval[] ops,
+       private static ValueEval invokeOperation(OperationEval operation, Eval[] ops,
                        EvaluationWorkbook workbook, int sheetIndex, int srcRowNum, int srcColNum) {
 
                if(operation instanceof FunctionEval) {
@@ -368,7 +368,8 @@ public final class WorkbookEvaluator {
                                return fe.getFreeRefFunction().evaluate(ops, workbook, sheetIndex, srcRowNum, srcColNum);
                        }
                }
-               return operation.evaluate(ops, srcRowNum, (short)srcColNum);
+               // TODO - fix return type of this evaluate method:
+               return (ValueEval) operation.evaluate(ops, srcRowNum, (short)srcColNum);
        }
        private SheetRefEvaluator createExternSheetRefEvaluator(EvaluationTracker tracker,
                        ExternSheetReferenceToken ptg) {
@@ -391,7 +392,7 @@ public final class WorkbookEvaluator {
         * StringPtg, BoolPtg <br/>special Note: OperationPtg subtypes cannot be
         * passed here!
         */
-       private Eval getEvalForPtg(Ptg ptg, int sheetIndex, EvaluationTracker tracker) {
+       private ValueEval getEvalForPtg(Ptg ptg, int sheetIndex, EvaluationTracker tracker) {
                //  consider converting all these (ptg instanceof XxxPtg) expressions to (ptg.getClass() == XxxPtg.class)
 
                if (ptg instanceof NamePtg) {
@@ -465,7 +466,7 @@ public final class WorkbookEvaluator {
 
                throw new RuntimeException("Unexpected ptg class (" + ptg.getClass().getName() + ")");
        }
-       private Eval evaluateNameFormula(Ptg[] ptgs, int sheetIndex, EvaluationTracker tracker) {
+       private ValueEval evaluateNameFormula(Ptg[] ptgs, int sheetIndex, EvaluationTracker tracker) {
                if (ptgs.length > 1) {
                        throw new RuntimeException("Complex name formulas not supported yet");
                }