From b8ec92ee353f3b5ce65c43d87b06d696b0c0bf36 Mon Sep 17 00:00:00 2001 From: Josh Micich Date: Fri, 20 Nov 2009 01:13:17 +0000 Subject: [PATCH] moved lazy ref creation methods to OperationEvaluationContext git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@882375 13f79535-47bb-0310-9956-ffa450edef68 --- .../record/formula/eval/AreaEvalBase.java | 20 +++++-- .../apache/poi/ss/formula/LazyAreaEval.java | 16 ++++-- .../apache/poi/ss/formula/LazyRefEval.java | 8 --- .../formula/OperationEvaluationContext.java | 55 ++++++++----------- .../poi/ss/formula/WorkbookEvaluator.java | 15 +++-- 5 files changed, 57 insertions(+), 57 deletions(-) diff --git a/src/java/org/apache/poi/hssf/record/formula/eval/AreaEvalBase.java b/src/java/org/apache/poi/hssf/record/formula/eval/AreaEvalBase.java index 94f6c9fe4a..19dc1ef4d1 100644 --- a/src/java/org/apache/poi/hssf/record/formula/eval/AreaEvalBase.java +++ b/src/java/org/apache/poi/hssf/record/formula/eval/AreaEvalBase.java @@ -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; } diff --git a/src/java/org/apache/poi/ss/formula/LazyAreaEval.java b/src/java/org/apache/poi/ss/formula/LazyAreaEval.java index 3b891fbbb8..b8e186a7d7 100644 --- a/src/java/org/apache/poi/ss/formula/LazyAreaEval.java +++ b/src/java/org/apache/poi/ss/formula/LazyAreaEval.java @@ -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); } diff --git a/src/java/org/apache/poi/ss/formula/LazyRefEval.java b/src/java/org/apache/poi/ss/formula/LazyRefEval.java index 2a97c67c48..7a4aabea11 100644 --- a/src/java/org/apache/poi/ss/formula/LazyRefEval.java +++ b/src/java/org/apache/poi/ss/formula/LazyRefEval.java @@ -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()); diff --git a/src/java/org/apache/poi/ss/formula/OperationEvaluationContext.java b/src/java/org/apache/poi/ss/formula/OperationEvaluationContext.java index e1a28a6fee..15d59c3a88 100644 --- a/src/java/org/apache/poi/ss/formula/OperationEvaluationContext.java +++ b/src/java/org/apache/poi/ss/formula/OperationEvaluationContext.java @@ -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); + } } diff --git a/src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java b/src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java index 8fc1f3eb99..d043376695 100644 --- a/src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java +++ b/src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java @@ -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) {