]> source.dussan.org Git - poi.git/commitdiff
moved lazy ref creation methods to OperationEvaluationContext
authorJosh Micich <josh@apache.org>
Fri, 20 Nov 2009 01:13:17 +0000 (01:13 +0000)
committerJosh Micich <josh@apache.org>
Fri, 20 Nov 2009 01:13:17 +0000 (01:13 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@882375 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/record/formula/eval/AreaEvalBase.java
src/java/org/apache/poi/ss/formula/LazyAreaEval.java
src/java/org/apache/poi/ss/formula/LazyRefEval.java
src/java/org/apache/poi/ss/formula/OperationEvaluationContext.java
src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java

index 94f6c9fe4a16e973a794d04b0e55bd120768e76d..19dc1ef4d1dc6e1f34120f1df0ad416a7d7732f4 100644 (file)
@@ -31,12 +31,22 @@ public abstract class AreaEvalBase implements AreaEval {
        private final int _nColumns;
        private final int _nRows;
 
+       protected AreaEvalBase(int firstRow, int firstColumn, int lastRow, int lastColumn) {
+               _firstColumn = firstColumn;
+               _firstRow = firstRow;
+               _lastColumn = lastColumn;
+               _lastRow = lastRow;
+
+               _nColumns = _lastColumn - _firstColumn + 1;
+               _nRows = _lastRow - _firstRow + 1;
+       }
+
        protected AreaEvalBase(AreaI ptg) {
                _firstRow = ptg.getFirstRow();
                _firstColumn = ptg.getFirstColumn();
                _lastRow = ptg.getLastRow();
                _lastColumn = ptg.getLastColumn();
-               
+
                _nColumns = _lastColumn - _firstColumn + 1;
                _nRows = _lastRow - _firstRow + 1;
        }
@@ -60,20 +70,20 @@ public abstract class AreaEvalBase implements AreaEval {
        public final ValueEval getValueAt(int row, int col) {
                int rowOffsetIx = row - _firstRow;
                int colOffsetIx = col - _firstColumn;
-               
+
                if(rowOffsetIx < 0 || rowOffsetIx >= _nRows) {
-                       throw new IllegalArgumentException("Specified row index (" + row 
+                       throw new IllegalArgumentException("Specified row index (" + row
                                        + ") is outside the allowed range (" + _firstRow + ".." + _lastRow + ")");
                }
                if(colOffsetIx < 0 || colOffsetIx >= _nColumns) {
-                       throw new IllegalArgumentException("Specified column index (" + col 
+                       throw new IllegalArgumentException("Specified column index (" + col
                                        + ") is outside the allowed range (" + _firstColumn + ".." + col + ")");
                }
                return getRelativeValue(rowOffsetIx, colOffsetIx);
        }
 
        public final boolean contains(int row, int col) {
-               return _firstRow <= row && _lastRow >= row 
+               return _firstRow <= row && _lastRow >= row
                        && _firstColumn <= col && _lastColumn >= col;
        }
 
index 3b891fbbb8317e5e5083d1fb1f9b9b5d9c509778..b8e186a7d755fed0ce31d79dc3cf917ce78e7c30 100644 (file)
@@ -26,22 +26,28 @@ import org.apache.poi.hssf.util.CellReference;
 
 /**
  *
- * @author Josh Micich 
+ * @author Josh Micich
  */
 final class LazyAreaEval extends AreaEvalBase {
 
        private final SheetRefEvaluator _evaluator;
 
-       public LazyAreaEval(AreaI ptg, SheetRefEvaluator evaluator) {
+       LazyAreaEval(AreaI ptg, SheetRefEvaluator evaluator) {
                super(ptg);
                _evaluator = evaluator;
        }
 
-       public ValueEval getRelativeValue(int relativeRowIndex, int relativeColumnIndex) { 
-               
+       public LazyAreaEval(int firstRowIndex, int firstColumnIndex, int lastRowIndex,
+                       int lastColumnIndex, SheetRefEvaluator evaluator) {
+               super(firstRowIndex, firstColumnIndex, lastRowIndex, lastColumnIndex);
+               _evaluator = evaluator;
+       }
+
+       public ValueEval getRelativeValue(int relativeRowIndex, int relativeColumnIndex) {
+
                int rowIx = (relativeRowIndex + getFirstRow() ) & 0xFFFF;
                int colIx = (relativeColumnIndex + getFirstColumn() ) & 0x00FF;
-               
+
                return _evaluator.getEvalForCell(rowIx, colIx);
        }
 
index 2a97c67c4818dbe5e0b38a2ddd0e669d878d9973..7a4aabea11804581dfa3478f9d3a3f015d2d8628 100644 (file)
@@ -18,8 +18,6 @@
 package org.apache.poi.ss.formula;
 
 import org.apache.poi.hssf.record.formula.AreaI;
-import org.apache.poi.hssf.record.formula.Ref3DPtg;
-import org.apache.poi.hssf.record.formula.RefPtg;
 import org.apache.poi.hssf.record.formula.AreaI.OffsetArea;
 import org.apache.poi.hssf.record.formula.eval.AreaEval;
 import org.apache.poi.hssf.record.formula.eval.RefEvalBase;
@@ -41,12 +39,6 @@ final class LazyRefEval extends RefEvalBase {
                }
                _evaluator = sre;
        }
-       public LazyRefEval(RefPtg ptg, SheetRefEvaluator sre) {
-               this(ptg.getRow(), ptg.getColumn(), sre);
-       }
-       public LazyRefEval(Ref3DPtg ptg, SheetRefEvaluator sre) {
-               this(ptg.getRow(), ptg.getColumn(), sre);
-       }
 
        public ValueEval getInnerValueEval() {
                return _evaluator.getEvalForCell(getRow(), getColumn());
index e1a28a6fee8ab6185624f4e3552f6d5d66cca4ba..15d59c3a8831b318a40d89ec59bc363365ddb635 100644 (file)
@@ -17,7 +17,6 @@
 
 package org.apache.poi.ss.formula;
 
-import org.apache.poi.hssf.record.formula.AreaI;
 import org.apache.poi.hssf.record.formula.eval.AreaEval;
 import org.apache.poi.hssf.record.formula.eval.ErrorEval;
 import org.apache.poi.hssf.record.formula.eval.RefEval;
@@ -68,8 +67,10 @@ public final class OperationEvaluationContext {
                return _columnIndex;
        }
 
-       /* package */ SheetRefEvaluator createExternSheetRefEvaluator(ExternSheetReferenceToken ptg) {
-               int externSheetIndex = ptg.getExternSheetIndex();
+       SheetRefEvaluator createExternSheetRefEvaluator(ExternSheetReferenceToken ptg) {
+               return createExternSheetRefEvaluator(ptg.getExternSheetIndex());
+       }
+       SheetRefEvaluator createExternSheetRefEvaluator(int externSheetIndex) {
                ExternalSheet externalSheet = _workbook.getExternalSheet(externSheetIndex);
                WorkbookEvaluator targetEvaluator;
                int otherSheetIndex;
@@ -212,7 +213,7 @@ public final class OperationEvaluationContext {
                        default:
                                throw new IllegalStateException("Unexpected reference classification of '" + refStrPart1 + "'.");
                }
-               return new LazyAreaEval(new AI(firstRow, firstCol, lastRow, lastCol), sre);
+               return new LazyAreaEval(firstRow, firstCol, lastRow, lastCol, sre);
        }
 
        private static int parseRowRef(String refStrPart) {
@@ -223,33 +224,6 @@ public final class OperationEvaluationContext {
                return Integer.parseInt(refStrPart) - 1;
        }
 
-       private static final class AI implements AreaI {
-
-               private final int _fr;
-               private final int _lr;
-               private final int _fc;
-               private final int _lc;
-
-               public AI(int fr, int fc, int lr, int lc) {
-                       _fr = Math.min(fr, lr);
-                       _lr = Math.max(fr, lr);
-                       _fc = Math.min(fc, lc);
-                       _lc = Math.max(fc, lc);
-               }
-               public int getFirstColumn() {
-                       return _fc;
-               }
-               public int getFirstRow() {
-                       return _fr;
-               }
-               public int getLastColumn() {
-                       return _lc;
-               }
-               public int getLastRow() {
-                       return _lr;
-               }
-       }
-
        private static NameType classifyCellReference(String str, SpreadsheetVersion ssVersion) {
                int len = str.length();
                if (len < 1) {
@@ -261,4 +235,23 @@ public final class OperationEvaluationContext {
        public FreeRefFunction findUserDefinedFunction(String functionName) {
                return _bookEvaluator.findUserDefinedFunction(functionName);
        }
+
+       public ValueEval getRefEval(int rowIndex, int columnIndex) {
+               SheetRefEvaluator sre = getRefEvaluatorForCurrentSheet();
+               return new LazyRefEval(rowIndex, columnIndex, sre);
+       }
+       public ValueEval getRef3DEval(int rowIndex, int columnIndex, int extSheetIndex) {
+               SheetRefEvaluator sre = createExternSheetRefEvaluator(extSheetIndex);
+               return new LazyRefEval(rowIndex, columnIndex, sre);
+       }
+       public ValueEval getAreaEval(int firstRowIndex, int firstColumnIndex,
+                       int lastRowIndex, int lastColumnIndex) {
+               SheetRefEvaluator sre = getRefEvaluatorForCurrentSheet();
+               return new LazyAreaEval(firstRowIndex, firstColumnIndex, lastRowIndex, lastColumnIndex, sre);
+       }
+       public ValueEval getArea3DEval(int firstRowIndex, int firstColumnIndex,
+                       int lastRowIndex, int lastColumnIndex, int extSheetIndex) {
+               SheetRefEvaluator sre = createExternSheetRefEvaluator(extSheetIndex);
+               return new LazyAreaEval(firstRowIndex, firstColumnIndex, lastRowIndex, lastColumnIndex, sre);
+       }
 }
index 8fc1f3eb998e6c7e3bf6e7add8e71a8db5d42484..d043376695d8ffc3bb85558e5e509bad1df887b7 100644 (file)
@@ -560,21 +560,20 @@ public final class WorkbookEvaluator {
                                return ErrorEval.REF_INVALID;
                }
                if (ptg instanceof Ref3DPtg) {
-                       Ref3DPtg refPtg = (Ref3DPtg) ptg;
-                       SheetRefEvaluator sre = ec.createExternSheetRefEvaluator(refPtg);
-                       return new LazyRefEval(refPtg, sre);
+                       Ref3DPtg rptg = (Ref3DPtg) ptg;
+                       return ec.getRef3DEval(rptg.getRow(), rptg.getColumn(), rptg.getExternSheetIndex());
                }
                if (ptg instanceof Area3DPtg) {
                        Area3DPtg aptg = (Area3DPtg) ptg;
-                       SheetRefEvaluator sre = ec.createExternSheetRefEvaluator(aptg);
-                       return new LazyAreaEval(aptg, sre);
+                       return ec.getArea3DEval(aptg.getFirstRow(), aptg.getFirstColumn(), aptg.getLastRow(), aptg.getLastColumn(), aptg.getExternSheetIndex());
                }
-               SheetRefEvaluator sre = ec.getRefEvaluatorForCurrentSheet();
                if (ptg instanceof RefPtg) {
-                       return new LazyRefEval(((RefPtg) ptg), sre);
+                       RefPtg rptg = (RefPtg) ptg;
+                       return ec.getRefEval(rptg.getRow(), rptg.getColumn());
                }
                if (ptg instanceof AreaPtg) {
-                       return new LazyAreaEval(((AreaPtg) ptg), sre);
+                       AreaPtg aptg = (AreaPtg) ptg;
+                       return ec.getAreaEval(aptg.getFirstRow(), aptg.getFirstColumn(), aptg.getLastRow(), aptg.getLastColumn());
                }
 
                if (ptg instanceof UnknownPtg) {