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 2009/05/19 18:29:53 UTC
svn commit: r776377 [4/6] - in /poi/trunk/src/java/org/apache/poi: ddf/
hssf/model/ hssf/record/ hssf/record/aggregates/ hssf/record/cont/
hssf/record/formula/ hssf/record/formula/eval/ hssf/usermodel/ hssf/util/
poifs/dev/ ss/ ss/formula/ ss/formula/e...
Modified: poi/trunk/src/java/org/apache/poi/ss/formula/FormulaRenderer.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/FormulaRenderer.java?rev=776377&r1=776376&r2=776377&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/FormulaRenderer.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/FormulaRenderer.java Tue May 19 16:29:51 2009
@@ -1,131 +1,131 @@
-/* ====================================================================
- 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.ss.formula;
-
-import java.util.Stack;
-
-import org.apache.poi.hssf.record.formula.AttrPtg;
-import org.apache.poi.hssf.record.formula.MemAreaPtg;
-import org.apache.poi.hssf.record.formula.MemErrPtg;
-import org.apache.poi.hssf.record.formula.MemFuncPtg;
-import org.apache.poi.hssf.record.formula.OperationPtg;
-import org.apache.poi.hssf.record.formula.ParenthesisPtg;
-import org.apache.poi.hssf.record.formula.Ptg;
-
-/**
- * Common logic for rendering formulas.<br/>
- *
- * For POI internal use only
- *
- * @author Josh Micich
- */
-public class FormulaRenderer {
-
- /**
- * Static method to convert an array of {@link Ptg}s in RPN order
- * to a human readable string format in infix mode.
- * @param book used for defined names and 3D references
- * @param ptgs must not be <code>null</code>
- * @return a human readable String
- */
- public static String toFormulaString(FormulaRenderingWorkbook book, Ptg[] ptgs) {
- if (ptgs == null || ptgs.length == 0) {
- throw new IllegalArgumentException("ptgs must not be null");
- }
- Stack<String> stack = new Stack<String>();
-
- for (int i=0 ; i < ptgs.length; i++) {
- Ptg ptg = ptgs[i];
- // TODO - what about MemNoMemPtg?
- if(ptg instanceof MemAreaPtg || ptg instanceof MemFuncPtg || ptg instanceof MemErrPtg) {
- // marks the start of a list of area expressions which will be naturally combined
- // by their trailing operators (e.g. UnionPtg)
- // TODO - put comment and throw exception in toFormulaString() of these classes
- continue;
- }
- if (ptg instanceof ParenthesisPtg) {
- String contents = stack.pop();
- stack.push ("(" + contents + ")");
- continue;
- }
- if (ptg instanceof AttrPtg) {
- AttrPtg attrPtg = ((AttrPtg) ptg);
- if (attrPtg.isOptimizedIf() || attrPtg.isOptimizedChoose() || attrPtg.isGoto()) {
- continue;
- }
- if (attrPtg.isSpace()) {
- // POI currently doesn't render spaces in formulas
- continue;
- // but if it ever did, care must be taken:
- // tAttrSpace comes *before* the operand it applies to, which may be consistent
- // with how the formula text appears but is against the RPN ordering assumed here
- }
- if (attrPtg.isSemiVolatile()) {
- // similar to tAttrSpace - RPN is violated
- continue;
- }
- if (attrPtg.isSum()) {
- String[] operands = getOperands(stack, attrPtg.getNumberOfOperands());
- stack.push(attrPtg.toFormulaString(operands));
- continue;
- }
- throw new RuntimeException("Unexpected tAttr: " + attrPtg.toString());
- }
-
- if (ptg instanceof WorkbookDependentFormula) {
- WorkbookDependentFormula optg = (WorkbookDependentFormula) ptg;
- stack.push(optg.toFormulaString(book));
- continue;
- }
- if (! (ptg instanceof OperationPtg)) {
- stack.push(ptg.toFormulaString());
- continue;
- }
-
- OperationPtg o = (OperationPtg) ptg;
- String[] operands = getOperands(stack, o.getNumberOfOperands());
- stack.push(o.toFormulaString(operands));
- }
- if(stack.isEmpty()) {
- // inspection of the code above reveals that every stack.pop() is followed by a
- // stack.push(). So this is either an internal error or impossible.
- throw new IllegalStateException("Stack underflow");
- }
- String result = stack.pop();
- if(!stack.isEmpty()) {
- // Might be caused by some tokens like AttrPtg and Mem*Ptg, which really shouldn't
- // put anything on the stack
- throw new IllegalStateException("too much stuff left on the stack");
- }
- return result;
- }
-
- private static String[] getOperands(Stack<String> stack, int nOperands) {
- String[] operands = new String[nOperands];
-
- for (int j = nOperands-1; j >= 0; j--) { // reverse iteration because args were pushed in-order
- if(stack.isEmpty()) {
- String msg = "Too few arguments supplied to operation. Expected (" + nOperands
- + ") operands but got (" + (nOperands - j - 1) + ")";
- throw new IllegalStateException(msg);
- }
- operands[j] = stack.pop();
- }
- return operands;
- }
-}
+/* ====================================================================
+ 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.ss.formula;
+
+import java.util.Stack;
+
+import org.apache.poi.hssf.record.formula.AttrPtg;
+import org.apache.poi.hssf.record.formula.MemAreaPtg;
+import org.apache.poi.hssf.record.formula.MemErrPtg;
+import org.apache.poi.hssf.record.formula.MemFuncPtg;
+import org.apache.poi.hssf.record.formula.OperationPtg;
+import org.apache.poi.hssf.record.formula.ParenthesisPtg;
+import org.apache.poi.hssf.record.formula.Ptg;
+
+/**
+ * Common logic for rendering formulas.<br/>
+ *
+ * For POI internal use only
+ *
+ * @author Josh Micich
+ */
+public class FormulaRenderer {
+
+ /**
+ * Static method to convert an array of {@link Ptg}s in RPN order
+ * to a human readable string format in infix mode.
+ * @param book used for defined names and 3D references
+ * @param ptgs must not be <code>null</code>
+ * @return a human readable String
+ */
+ public static String toFormulaString(FormulaRenderingWorkbook book, Ptg[] ptgs) {
+ if (ptgs == null || ptgs.length == 0) {
+ throw new IllegalArgumentException("ptgs must not be null");
+ }
+ Stack<String> stack = new Stack<String>();
+
+ for (int i=0 ; i < ptgs.length; i++) {
+ Ptg ptg = ptgs[i];
+ // TODO - what about MemNoMemPtg?
+ if(ptg instanceof MemAreaPtg || ptg instanceof MemFuncPtg || ptg instanceof MemErrPtg) {
+ // marks the start of a list of area expressions which will be naturally combined
+ // by their trailing operators (e.g. UnionPtg)
+ // TODO - put comment and throw exception in toFormulaString() of these classes
+ continue;
+ }
+ if (ptg instanceof ParenthesisPtg) {
+ String contents = stack.pop();
+ stack.push ("(" + contents + ")");
+ continue;
+ }
+ if (ptg instanceof AttrPtg) {
+ AttrPtg attrPtg = ((AttrPtg) ptg);
+ if (attrPtg.isOptimizedIf() || attrPtg.isOptimizedChoose() || attrPtg.isGoto()) {
+ continue;
+ }
+ if (attrPtg.isSpace()) {
+ // POI currently doesn't render spaces in formulas
+ continue;
+ // but if it ever did, care must be taken:
+ // tAttrSpace comes *before* the operand it applies to, which may be consistent
+ // with how the formula text appears but is against the RPN ordering assumed here
+ }
+ if (attrPtg.isSemiVolatile()) {
+ // similar to tAttrSpace - RPN is violated
+ continue;
+ }
+ if (attrPtg.isSum()) {
+ String[] operands = getOperands(stack, attrPtg.getNumberOfOperands());
+ stack.push(attrPtg.toFormulaString(operands));
+ continue;
+ }
+ throw new RuntimeException("Unexpected tAttr: " + attrPtg.toString());
+ }
+
+ if (ptg instanceof WorkbookDependentFormula) {
+ WorkbookDependentFormula optg = (WorkbookDependentFormula) ptg;
+ stack.push(optg.toFormulaString(book));
+ continue;
+ }
+ if (! (ptg instanceof OperationPtg)) {
+ stack.push(ptg.toFormulaString());
+ continue;
+ }
+
+ OperationPtg o = (OperationPtg) ptg;
+ String[] operands = getOperands(stack, o.getNumberOfOperands());
+ stack.push(o.toFormulaString(operands));
+ }
+ if(stack.isEmpty()) {
+ // inspection of the code above reveals that every stack.pop() is followed by a
+ // stack.push(). So this is either an internal error or impossible.
+ throw new IllegalStateException("Stack underflow");
+ }
+ String result = stack.pop();
+ if(!stack.isEmpty()) {
+ // Might be caused by some tokens like AttrPtg and Mem*Ptg, which really shouldn't
+ // put anything on the stack
+ throw new IllegalStateException("too much stuff left on the stack");
+ }
+ return result;
+ }
+
+ private static String[] getOperands(Stack<String> stack, int nOperands) {
+ String[] operands = new String[nOperands];
+
+ for (int j = nOperands-1; j >= 0; j--) { // reverse iteration because args were pushed in-order
+ if(stack.isEmpty()) {
+ String msg = "Too few arguments supplied to operation. Expected (" + nOperands
+ + ") operands but got (" + (nOperands - j - 1) + ")";
+ throw new IllegalStateException(msg);
+ }
+ operands[j] = stack.pop();
+ }
+ return operands;
+ }
+}
Modified: poi/trunk/src/java/org/apache/poi/ss/formula/FormulaRenderingWorkbook.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/FormulaRenderingWorkbook.java?rev=776377&r1=776376&r2=776377&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/FormulaRenderingWorkbook.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/FormulaRenderingWorkbook.java Tue May 19 16:29:51 2009
@@ -1,40 +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.ss.formula;
-
-import org.apache.poi.hssf.record.formula.NamePtg;
-import org.apache.poi.hssf.record.formula.NameXPtg;
-import org.apache.poi.ss.formula.EvaluationWorkbook.ExternalSheet;
-
-/**
- * Abstracts a workbook for the purpose of converting formula to text.<br/>
- *
- * For POI internal use only
- *
- * @author Josh Micich
- */
-public interface FormulaRenderingWorkbook {
-
- /**
- * @return <code>null</code> if externSheetIndex refers to a sheet inside the current workbook
- */
- ExternalSheet getExternalSheet(int externSheetIndex);
- String getSheetNameByExternSheet(int externSheetIndex);
- String resolveNameXText(NameXPtg nameXPtg);
- String getNameText(NamePtg namePtg);
-}
+/* ====================================================================
+ 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.ss.formula;
+
+import org.apache.poi.hssf.record.formula.NamePtg;
+import org.apache.poi.hssf.record.formula.NameXPtg;
+import org.apache.poi.ss.formula.EvaluationWorkbook.ExternalSheet;
+
+/**
+ * Abstracts a workbook for the purpose of converting formula to text.<br/>
+ *
+ * For POI internal use only
+ *
+ * @author Josh Micich
+ */
+public interface FormulaRenderingWorkbook {
+
+ /**
+ * @return <code>null</code> if externSheetIndex refers to a sheet inside the current workbook
+ */
+ ExternalSheet getExternalSheet(int externSheetIndex);
+ String getSheetNameByExternSheet(int externSheetIndex);
+ String resolveNameXText(NameXPtg nameXPtg);
+ String getNameText(NamePtg namePtg);
+}
Modified: poi/trunk/src/java/org/apache/poi/ss/formula/FormulaType.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/FormulaType.java?rev=776377&r1=776376&r2=776377&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/FormulaType.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/FormulaType.java Tue May 19 16:29:51 2009
@@ -1,40 +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.ss.formula;
-
-/**
- * Enumeration of various formula types.<br/>
- *
- * For POI internal use only
- *
- * @author Josh Micich
- */
-public final class FormulaType {
- private FormulaType() {
- // no instances of this class
- }
- public static final int CELL = 0;
- public static final int SHARED = 1;
- public static final int ARRAY =2;
- public static final int CONDFORMAT = 3;
- public static final int NAMEDRANGE = 4;
- // this constant is currently very specific. The exact differences from general data
- // validation formulas or conditional format formulas is not known yet
- public static final int DATAVALIDATION_LIST = 5;
-
-}
+/* ====================================================================
+ 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.ss.formula;
+
+/**
+ * Enumeration of various formula types.<br/>
+ *
+ * For POI internal use only
+ *
+ * @author Josh Micich
+ */
+public final class FormulaType {
+ private FormulaType() {
+ // no instances of this class
+ }
+ public static final int CELL = 0;
+ public static final int SHARED = 1;
+ public static final int ARRAY =2;
+ public static final int CONDFORMAT = 3;
+ public static final int NAMEDRANGE = 4;
+ // this constant is currently very specific. The exact differences from general data
+ // validation formulas or conditional format formulas is not known yet
+ public static final int DATAVALIDATION_LIST = 5;
+
+}
Modified: poi/trunk/src/java/org/apache/poi/ss/formula/LazyRefEval.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/LazyRefEval.java?rev=776377&r1=776376&r2=776377&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/LazyRefEval.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/LazyRefEval.java Tue May 19 16:29:51 2009
@@ -1,68 +1,68 @@
-/* ====================================================================
- 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.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;
-import org.apache.poi.hssf.record.formula.eval.ValueEval;
-import org.apache.poi.hssf.util.CellReference;
-
-/**
-*
-* @author Josh Micich
-*/
-final class LazyRefEval extends RefEvalBase {
-
- private final SheetRefEvaluator _evaluator;
-
- public LazyRefEval(RefPtg ptg, SheetRefEvaluator sre) {
- super(ptg.getRow(), ptg.getColumn());
- _evaluator = sre;
- }
- public LazyRefEval(Ref3DPtg ptg, SheetRefEvaluator sre) {
- super(ptg.getRow(), ptg.getColumn());
- _evaluator = sre;
- }
-
- public ValueEval getInnerValueEval() {
- return _evaluator.getEvalForCell(getRow(), getColumn());
- }
-
- public AreaEval offset(int relFirstRowIx, int relLastRowIx, int relFirstColIx, int relLastColIx) {
-
- AreaI area = new OffsetArea(getRow(), getColumn(),
- relFirstRowIx, relLastRowIx, relFirstColIx, relLastColIx);
-
- return new LazyAreaEval(area, _evaluator);
- }
-
- public String toString() {
- CellReference cr = new CellReference(getRow(), getColumn());
- StringBuffer sb = new StringBuffer();
- sb.append(getClass().getName()).append("[");
- sb.append(_evaluator.getSheetName());
- sb.append('!');
- sb.append(cr.formatAsString());
- sb.append("]");
- return sb.toString();
- }
-}
+/* ====================================================================
+ 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.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;
+import org.apache.poi.hssf.record.formula.eval.ValueEval;
+import org.apache.poi.hssf.util.CellReference;
+
+/**
+*
+* @author Josh Micich
+*/
+final class LazyRefEval extends RefEvalBase {
+
+ private final SheetRefEvaluator _evaluator;
+
+ public LazyRefEval(RefPtg ptg, SheetRefEvaluator sre) {
+ super(ptg.getRow(), ptg.getColumn());
+ _evaluator = sre;
+ }
+ public LazyRefEval(Ref3DPtg ptg, SheetRefEvaluator sre) {
+ super(ptg.getRow(), ptg.getColumn());
+ _evaluator = sre;
+ }
+
+ public ValueEval getInnerValueEval() {
+ return _evaluator.getEvalForCell(getRow(), getColumn());
+ }
+
+ public AreaEval offset(int relFirstRowIx, int relLastRowIx, int relFirstColIx, int relLastColIx) {
+
+ AreaI area = new OffsetArea(getRow(), getColumn(),
+ relFirstRowIx, relLastRowIx, relFirstColIx, relLastColIx);
+
+ return new LazyAreaEval(area, _evaluator);
+ }
+
+ public String toString() {
+ CellReference cr = new CellReference(getRow(), getColumn());
+ StringBuffer sb = new StringBuffer();
+ sb.append(getClass().getName()).append("[");
+ sb.append(_evaluator.getSheetName());
+ sb.append('!');
+ sb.append(cr.formatAsString());
+ sb.append("]");
+ return sb.toString();
+ }
+}
Modified: poi/trunk/src/java/org/apache/poi/ss/formula/SheetRefEvaluator.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/SheetRefEvaluator.java?rev=776377&r1=776376&r2=776377&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/SheetRefEvaluator.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/SheetRefEvaluator.java Tue May 19 16:29:51 2009
@@ -1,48 +1,48 @@
-/* ====================================================================
- 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.ss.formula;
-
-import org.apache.poi.hssf.record.formula.eval.ValueEval;
-/**
- *
- *
- * @author Josh Micich
- */
-final class SheetRefEvaluator {
-
- private final WorkbookEvaluator _bookEvaluator;
- private final EvaluationTracker _tracker;
- private final EvaluationSheet _sheet;
- private final int _sheetIndex;
-
- public SheetRefEvaluator(WorkbookEvaluator bookEvaluator, EvaluationTracker tracker,
- EvaluationWorkbook _workbook, int sheetIndex) {
- _bookEvaluator = bookEvaluator;
- _tracker = tracker;
- _sheet = _workbook.getSheet(sheetIndex);
- _sheetIndex = sheetIndex;
- }
-
- public String getSheetName() {
- return _bookEvaluator.getSheetName(_sheetIndex);
- }
-
- public ValueEval getEvalForCell(int rowIndex, int columnIndex) {
- return _bookEvaluator.evaluateReference(_sheet, _sheetIndex, rowIndex, columnIndex, _tracker);
- }
-}
+/* ====================================================================
+ 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.ss.formula;
+
+import org.apache.poi.hssf.record.formula.eval.ValueEval;
+/**
+ *
+ *
+ * @author Josh Micich
+ */
+final class SheetRefEvaluator {
+
+ private final WorkbookEvaluator _bookEvaluator;
+ private final EvaluationTracker _tracker;
+ private final EvaluationSheet _sheet;
+ private final int _sheetIndex;
+
+ public SheetRefEvaluator(WorkbookEvaluator bookEvaluator, EvaluationTracker tracker,
+ EvaluationWorkbook _workbook, int sheetIndex) {
+ _bookEvaluator = bookEvaluator;
+ _tracker = tracker;
+ _sheet = _workbook.getSheet(sheetIndex);
+ _sheetIndex = sheetIndex;
+ }
+
+ public String getSheetName() {
+ return _bookEvaluator.getSheetName(_sheetIndex);
+ }
+
+ public ValueEval getEvalForCell(int rowIndex, int columnIndex) {
+ return _bookEvaluator.evaluateReference(_sheet, _sheetIndex, rowIndex, columnIndex, _tracker);
+ }
+}
Modified: poi/trunk/src/java/org/apache/poi/ss/formula/WorkbookDependentFormula.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/WorkbookDependentFormula.java?rev=776377&r1=776376&r2=776377&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/WorkbookDependentFormula.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/WorkbookDependentFormula.java Tue May 19 16:29:51 2009
@@ -1,30 +1,30 @@
-/* ====================================================================
- 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.ss.formula;
-
-/**
- * Should be implemented by any {@link org.apache.poi.hssf.record.formula.Ptg} subclass that needs a workbook to render its formula.
- * <br/>
- *
- * For POI internal use only
- *
- * @author Josh Micich
- */
-public interface WorkbookDependentFormula {
- String toFormulaString(FormulaRenderingWorkbook book);
-}
+/* ====================================================================
+ 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.ss.formula;
+
+/**
+ * Should be implemented by any {@link org.apache.poi.hssf.record.formula.Ptg} subclass that needs a workbook to render its formula.
+ * <br/>
+ *
+ * For POI internal use only
+ *
+ * @author Josh Micich
+ */
+public interface WorkbookDependentFormula {
+ String toFormulaString(FormulaRenderingWorkbook book);
+}
Modified: poi/trunk/src/java/org/apache/poi/ss/formula/eval/NotImplementedException.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/eval/NotImplementedException.java?rev=776377&r1=776376&r2=776377&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/eval/NotImplementedException.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/eval/NotImplementedException.java Tue May 19 16:29:51 2009
@@ -1,36 +1,36 @@
-/* ====================================================================
- 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.ss.formula.eval;
-
-import org.apache.poi.ss.usermodel.FormulaEvaluator;
-
-/**
- * An exception thrown by implementors of {@link FormulaEvaluator} when attempting to evaluate
- * a formula which requires features that POI does not (yet) support.
- *
- * @author Josh Micich
- */
-public final class NotImplementedException extends RuntimeException {
-
- public NotImplementedException(String message) {
- super(message);
- }
- public NotImplementedException(String message, NotImplementedException cause) {
- super(message, cause);
- }
-}
+/* ====================================================================
+ 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.ss.formula.eval;
+
+import org.apache.poi.ss.usermodel.FormulaEvaluator;
+
+/**
+ * An exception thrown by implementors of {@link FormulaEvaluator} when attempting to evaluate
+ * a formula which requires features that POI does not (yet) support.
+ *
+ * @author Josh Micich
+ */
+public final class NotImplementedException extends RuntimeException {
+
+ public NotImplementedException(String message) {
+ super(message);
+ }
+ public NotImplementedException(String message, NotImplementedException cause) {
+ super(message, cause);
+ }
+}
Modified: poi/trunk/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluationCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluationCell.java?rev=776377&r1=776376&r2=776377&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluationCell.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluationCell.java Tue May 19 16:29:51 2009
@@ -1,132 +1,132 @@
-/* ====================================================================
- 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.ss.formula.eval.forked;
-
-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.NumberEval;
-import org.apache.poi.hssf.record.formula.eval.StringEval;
-import org.apache.poi.hssf.record.formula.eval.ValueEval;
-import org.apache.poi.hssf.usermodel.HSSFCell;
-import org.apache.poi.ss.formula.EvaluationCell;
-import org.apache.poi.ss.formula.EvaluationSheet;
-import org.apache.poi.ss.usermodel.Cell;
-
-/**
- * Represents a cell being used for forked evaluation that has had a value set different from the
- * corresponding cell in the shared master workbook.
- *
- * @author Josh Micich
- */
-final class ForkedEvaluationCell implements EvaluationCell {
-
- private final EvaluationSheet _sheet;
- /** corresponding cell from master workbook */
- private final EvaluationCell _masterCell;
- private boolean _booleanValue;
- private int _cellType;
- private int _errorValue;
- private double _numberValue;
- private String _stringValue;
-
- public ForkedEvaluationCell(ForkedEvaluationSheet sheet, EvaluationCell masterCell) {
- _sheet = sheet;
- _masterCell = masterCell;
- // start with value blank, but expect construction to be immediately
- setValue(BlankEval.INSTANCE); // followed by a proper call to setValue()
- }
-
- public Object getIdentityKey() {
- return _masterCell.getIdentityKey();
- }
-
- public void setValue(ValueEval value) {
- Class<? extends ValueEval> cls = value.getClass();
-
- if (cls == NumberEval.class) {
- _cellType = HSSFCell.CELL_TYPE_NUMERIC;
- _numberValue = ((NumberEval)value).getNumberValue();
- return;
- }
- if (cls == StringEval.class) {
- _cellType = HSSFCell.CELL_TYPE_STRING;
- _stringValue = ((StringEval)value).getStringValue();
- return;
- }
- if (cls == BoolEval.class) {
- _cellType = HSSFCell.CELL_TYPE_BOOLEAN;
- _booleanValue = ((BoolEval)value).getBooleanValue();
- return;
- }
- if (cls == ErrorEval.class) {
- _cellType = HSSFCell.CELL_TYPE_ERROR;
- _errorValue = ((ErrorEval)value).getErrorCode();
- return;
- }
- if (cls == BlankEval.class) {
- _cellType = HSSFCell.CELL_TYPE_BLANK;
- return;
- }
- throw new IllegalArgumentException("Unexpected value class (" + cls.getName() + ")");
- }
- public void copyValue(Cell destCell) {
- switch (_cellType) {
- case Cell.CELL_TYPE_BLANK: destCell.setCellType(Cell.CELL_TYPE_BLANK); return;
- case Cell.CELL_TYPE_NUMERIC: destCell.setCellValue(_numberValue); return;
- case Cell.CELL_TYPE_BOOLEAN: destCell.setCellValue(_booleanValue); return;
- case Cell.CELL_TYPE_STRING: destCell.setCellValue(_stringValue); return;
- case Cell.CELL_TYPE_ERROR: destCell.setCellErrorValue((byte)_errorValue); return;
- }
- throw new IllegalStateException("Unexpected data type (" + _cellType + ")");
- }
-
- private void checkCellType(int expectedCellType) {
- if (_cellType != expectedCellType) {
- throw new RuntimeException("Wrong data type (" + _cellType + ")");
- }
- }
- public int getCellType() {
- return _cellType;
- }
- public boolean getBooleanCellValue() {
- checkCellType(HSSFCell.CELL_TYPE_BOOLEAN);
- return _booleanValue;
- }
- public int getErrorCellValue() {
- checkCellType(HSSFCell.CELL_TYPE_ERROR);
- return _errorValue;
- }
- public double getNumericCellValue() {
- checkCellType(HSSFCell.CELL_TYPE_NUMERIC);
- return _numberValue;
- }
- public String getStringCellValue() {
- checkCellType(HSSFCell.CELL_TYPE_STRING);
- return _stringValue;
- }
- public EvaluationSheet getSheet() {
- return _sheet;
- }
- public int getRowIndex() {
- return _masterCell.getRowIndex();
- }
- public int getColumnIndex() {
- return _masterCell.getColumnIndex();
- }
-}
+/* ====================================================================
+ 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.ss.formula.eval.forked;
+
+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.NumberEval;
+import org.apache.poi.hssf.record.formula.eval.StringEval;
+import org.apache.poi.hssf.record.formula.eval.ValueEval;
+import org.apache.poi.hssf.usermodel.HSSFCell;
+import org.apache.poi.ss.formula.EvaluationCell;
+import org.apache.poi.ss.formula.EvaluationSheet;
+import org.apache.poi.ss.usermodel.Cell;
+
+/**
+ * Represents a cell being used for forked evaluation that has had a value set different from the
+ * corresponding cell in the shared master workbook.
+ *
+ * @author Josh Micich
+ */
+final class ForkedEvaluationCell implements EvaluationCell {
+
+ private final EvaluationSheet _sheet;
+ /** corresponding cell from master workbook */
+ private final EvaluationCell _masterCell;
+ private boolean _booleanValue;
+ private int _cellType;
+ private int _errorValue;
+ private double _numberValue;
+ private String _stringValue;
+
+ public ForkedEvaluationCell(ForkedEvaluationSheet sheet, EvaluationCell masterCell) {
+ _sheet = sheet;
+ _masterCell = masterCell;
+ // start with value blank, but expect construction to be immediately
+ setValue(BlankEval.INSTANCE); // followed by a proper call to setValue()
+ }
+
+ public Object getIdentityKey() {
+ return _masterCell.getIdentityKey();
+ }
+
+ public void setValue(ValueEval value) {
+ Class<? extends ValueEval> cls = value.getClass();
+
+ if (cls == NumberEval.class) {
+ _cellType = HSSFCell.CELL_TYPE_NUMERIC;
+ _numberValue = ((NumberEval)value).getNumberValue();
+ return;
+ }
+ if (cls == StringEval.class) {
+ _cellType = HSSFCell.CELL_TYPE_STRING;
+ _stringValue = ((StringEval)value).getStringValue();
+ return;
+ }
+ if (cls == BoolEval.class) {
+ _cellType = HSSFCell.CELL_TYPE_BOOLEAN;
+ _booleanValue = ((BoolEval)value).getBooleanValue();
+ return;
+ }
+ if (cls == ErrorEval.class) {
+ _cellType = HSSFCell.CELL_TYPE_ERROR;
+ _errorValue = ((ErrorEval)value).getErrorCode();
+ return;
+ }
+ if (cls == BlankEval.class) {
+ _cellType = HSSFCell.CELL_TYPE_BLANK;
+ return;
+ }
+ throw new IllegalArgumentException("Unexpected value class (" + cls.getName() + ")");
+ }
+ public void copyValue(Cell destCell) {
+ switch (_cellType) {
+ case Cell.CELL_TYPE_BLANK: destCell.setCellType(Cell.CELL_TYPE_BLANK); return;
+ case Cell.CELL_TYPE_NUMERIC: destCell.setCellValue(_numberValue); return;
+ case Cell.CELL_TYPE_BOOLEAN: destCell.setCellValue(_booleanValue); return;
+ case Cell.CELL_TYPE_STRING: destCell.setCellValue(_stringValue); return;
+ case Cell.CELL_TYPE_ERROR: destCell.setCellErrorValue((byte)_errorValue); return;
+ }
+ throw new IllegalStateException("Unexpected data type (" + _cellType + ")");
+ }
+
+ private void checkCellType(int expectedCellType) {
+ if (_cellType != expectedCellType) {
+ throw new RuntimeException("Wrong data type (" + _cellType + ")");
+ }
+ }
+ public int getCellType() {
+ return _cellType;
+ }
+ public boolean getBooleanCellValue() {
+ checkCellType(HSSFCell.CELL_TYPE_BOOLEAN);
+ return _booleanValue;
+ }
+ public int getErrorCellValue() {
+ checkCellType(HSSFCell.CELL_TYPE_ERROR);
+ return _errorValue;
+ }
+ public double getNumericCellValue() {
+ checkCellType(HSSFCell.CELL_TYPE_NUMERIC);
+ return _numberValue;
+ }
+ public String getStringCellValue() {
+ checkCellType(HSSFCell.CELL_TYPE_STRING);
+ return _stringValue;
+ }
+ public EvaluationSheet getSheet() {
+ return _sheet;
+ }
+ public int getRowIndex() {
+ return _masterCell.getRowIndex();
+ }
+ public int getColumnIndex() {
+ return _masterCell.getColumnIndex();
+ }
+}
Modified: poi/trunk/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluationSheet.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluationSheet.java?rev=776377&r1=776376&r2=776377&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluationSheet.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluationSheet.java Tue May 19 16:29:51 2009
@@ -1,129 +1,129 @@
-/* ====================================================================
- 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.ss.formula.eval.forked;
-
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.Map;
-
-import org.apache.poi.ss.formula.EvaluationCell;
-import org.apache.poi.ss.formula.EvaluationSheet;
-import org.apache.poi.ss.formula.EvaluationWorkbook;
-import org.apache.poi.ss.usermodel.Cell;
-import org.apache.poi.ss.usermodel.Row;
-import org.apache.poi.ss.usermodel.Sheet;
-
-/**
- * Represents a sheet being used for forked evaluation. Initially, objects of this class contain
- * only the cells from the master workbook. By calling {@link #getOrCreateUpdatableCell(int, int)},
- * the master cell object is logically replaced with a {@link ForkedEvaluationCell} instance, which
- * will be used in all subsequent evaluations.
- *
- * @author Josh Micich
- */
-final class ForkedEvaluationSheet implements EvaluationSheet {
-
- private final EvaluationSheet _masterSheet;
- /**
- * Only cells which have been split are put in this map. (This has been done to conserve memory).
- */
- private final Map<RowColKey, ForkedEvaluationCell> _sharedCellsByRowCol;
-
- public ForkedEvaluationSheet(EvaluationSheet masterSheet) {
- _masterSheet = masterSheet;
- _sharedCellsByRowCol = new HashMap<RowColKey, ForkedEvaluationCell>();
- }
-
- public EvaluationCell getCell(int rowIndex, int columnIndex) {
- RowColKey key = new RowColKey(rowIndex, columnIndex);
-
- ForkedEvaluationCell result = _sharedCellsByRowCol.get(key);
- if (result == null) {
- return _masterSheet.getCell(rowIndex, columnIndex);
- }
- return result;
- }
-
- public ForkedEvaluationCell getOrCreateUpdatableCell(int rowIndex, int columnIndex) {
- RowColKey key = new RowColKey(rowIndex, columnIndex);
-
- ForkedEvaluationCell result = _sharedCellsByRowCol.get(key);
- if (result == null) {
- EvaluationCell mcell = _masterSheet.getCell(rowIndex, columnIndex);
- result = new ForkedEvaluationCell(this, mcell);
- _sharedCellsByRowCol.put(key, result);
- }
- return result;
- }
-
- public void copyUpdatedCells(Sheet sheet) {
- RowColKey[] keys = new RowColKey[_sharedCellsByRowCol.size()];
- _sharedCellsByRowCol.keySet().toArray(keys);
- Arrays.sort(keys);
- for (int i = 0; i < keys.length; i++) {
- RowColKey key = keys[i];
- Row row = sheet.getRow(key.getRowIndex());
- if (row == null) {
- row = sheet.createRow(key.getRowIndex());
- }
- Cell destCell = row.getCell(key.getColumnIndex());
- if (destCell == null) {
- destCell = row.createCell(key.getColumnIndex());
- }
-
- ForkedEvaluationCell srcCell = _sharedCellsByRowCol.get(key);
- srcCell.copyValue(destCell);
- }
- }
-
- public int getSheetIndex(EvaluationWorkbook mewb) {
- return mewb.getSheetIndex(_masterSheet);
- }
-
- private static final class RowColKey implements Comparable<RowColKey>{
- private final int _rowIndex;
- private final int _columnIndex;
-
- public RowColKey(int rowIndex, int columnIndex) {
- _rowIndex = rowIndex;
- _columnIndex = columnIndex;
- }
- @Override
- public boolean equals(Object obj) {
- RowColKey other = (RowColKey) obj;
- return _rowIndex == other._rowIndex && _columnIndex == other._columnIndex;
- }
- @Override
- public int hashCode() {
- return _rowIndex ^ _columnIndex;
- }
- public int compareTo(RowColKey o) {
- int cmp = _rowIndex - o._rowIndex;
- if (cmp != 0) {
- return cmp;
- }
- return _columnIndex - o._columnIndex;
- }
- public int getRowIndex() {
- return _rowIndex;
- }
- public int getColumnIndex() {
- return _columnIndex;
- }
- }
-}
+/* ====================================================================
+ 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.ss.formula.eval.forked;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.poi.ss.formula.EvaluationCell;
+import org.apache.poi.ss.formula.EvaluationSheet;
+import org.apache.poi.ss.formula.EvaluationWorkbook;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+
+/**
+ * Represents a sheet being used for forked evaluation. Initially, objects of this class contain
+ * only the cells from the master workbook. By calling {@link #getOrCreateUpdatableCell(int, int)},
+ * the master cell object is logically replaced with a {@link ForkedEvaluationCell} instance, which
+ * will be used in all subsequent evaluations.
+ *
+ * @author Josh Micich
+ */
+final class ForkedEvaluationSheet implements EvaluationSheet {
+
+ private final EvaluationSheet _masterSheet;
+ /**
+ * Only cells which have been split are put in this map. (This has been done to conserve memory).
+ */
+ private final Map<RowColKey, ForkedEvaluationCell> _sharedCellsByRowCol;
+
+ public ForkedEvaluationSheet(EvaluationSheet masterSheet) {
+ _masterSheet = masterSheet;
+ _sharedCellsByRowCol = new HashMap<RowColKey, ForkedEvaluationCell>();
+ }
+
+ public EvaluationCell getCell(int rowIndex, int columnIndex) {
+ RowColKey key = new RowColKey(rowIndex, columnIndex);
+
+ ForkedEvaluationCell result = _sharedCellsByRowCol.get(key);
+ if (result == null) {
+ return _masterSheet.getCell(rowIndex, columnIndex);
+ }
+ return result;
+ }
+
+ public ForkedEvaluationCell getOrCreateUpdatableCell(int rowIndex, int columnIndex) {
+ RowColKey key = new RowColKey(rowIndex, columnIndex);
+
+ ForkedEvaluationCell result = _sharedCellsByRowCol.get(key);
+ if (result == null) {
+ EvaluationCell mcell = _masterSheet.getCell(rowIndex, columnIndex);
+ result = new ForkedEvaluationCell(this, mcell);
+ _sharedCellsByRowCol.put(key, result);
+ }
+ return result;
+ }
+
+ public void copyUpdatedCells(Sheet sheet) {
+ RowColKey[] keys = new RowColKey[_sharedCellsByRowCol.size()];
+ _sharedCellsByRowCol.keySet().toArray(keys);
+ Arrays.sort(keys);
+ for (int i = 0; i < keys.length; i++) {
+ RowColKey key = keys[i];
+ Row row = sheet.getRow(key.getRowIndex());
+ if (row == null) {
+ row = sheet.createRow(key.getRowIndex());
+ }
+ Cell destCell = row.getCell(key.getColumnIndex());
+ if (destCell == null) {
+ destCell = row.createCell(key.getColumnIndex());
+ }
+
+ ForkedEvaluationCell srcCell = _sharedCellsByRowCol.get(key);
+ srcCell.copyValue(destCell);
+ }
+ }
+
+ public int getSheetIndex(EvaluationWorkbook mewb) {
+ return mewb.getSheetIndex(_masterSheet);
+ }
+
+ private static final class RowColKey implements Comparable<RowColKey>{
+ private final int _rowIndex;
+ private final int _columnIndex;
+
+ public RowColKey(int rowIndex, int columnIndex) {
+ _rowIndex = rowIndex;
+ _columnIndex = columnIndex;
+ }
+ @Override
+ public boolean equals(Object obj) {
+ RowColKey other = (RowColKey) obj;
+ return _rowIndex == other._rowIndex && _columnIndex == other._columnIndex;
+ }
+ @Override
+ public int hashCode() {
+ return _rowIndex ^ _columnIndex;
+ }
+ public int compareTo(RowColKey o) {
+ int cmp = _rowIndex - o._rowIndex;
+ if (cmp != 0) {
+ return cmp;
+ }
+ return _columnIndex - o._columnIndex;
+ }
+ public int getRowIndex() {
+ return _rowIndex;
+ }
+ public int getColumnIndex() {
+ return _columnIndex;
+ }
+ }
+}
Modified: poi/trunk/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluationWorkbook.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluationWorkbook.java?rev=776377&r1=776376&r2=776377&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluationWorkbook.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluationWorkbook.java Tue May 19 16:29:51 2009
@@ -1,144 +1,144 @@
-/* ====================================================================
- 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.ss.formula.eval.forked;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import org.apache.poi.hssf.record.formula.NamePtg;
-import org.apache.poi.hssf.record.formula.NameXPtg;
-import org.apache.poi.hssf.record.formula.Ptg;
-import org.apache.poi.ss.formula.EvaluationCell;
-import org.apache.poi.ss.formula.EvaluationName;
-import org.apache.poi.ss.formula.EvaluationSheet;
-import org.apache.poi.ss.formula.EvaluationWorkbook;
-import org.apache.poi.ss.usermodel.Workbook;
-
-/**
- * Represents a workbook being used for forked evaluation. Most operations are delegated to the
- * shared master workbook, except those that potentially involve cell values that may have been
- * updated after a call to {@link #getOrCreateUpdatableCell(String, int, int)}.
- *
- * @author Josh Micich
- */
-final class ForkedEvaluationWorkbook implements EvaluationWorkbook {
-
- private final EvaluationWorkbook _masterBook;
- private final Map<String, ForkedEvaluationSheet> _sharedSheetsByName;
-
- public ForkedEvaluationWorkbook(EvaluationWorkbook master) {
- _masterBook = master;
- _sharedSheetsByName = new HashMap<String, ForkedEvaluationSheet>();
- }
-
- public ForkedEvaluationCell getOrCreateUpdatableCell(String sheetName, int rowIndex,
- int columnIndex) {
- ForkedEvaluationSheet sheet = getSharedSheet(sheetName);
- return sheet.getOrCreateUpdatableCell(rowIndex, columnIndex);
- }
-
- public EvaluationCell getEvaluationCell(String sheetName, int rowIndex, int columnIndex) {
- ForkedEvaluationSheet sheet = getSharedSheet(sheetName);
- return sheet.getCell(rowIndex, columnIndex);
- }
-
- private ForkedEvaluationSheet getSharedSheet(String sheetName) {
- ForkedEvaluationSheet result = _sharedSheetsByName.get(sheetName);
- if (result == null) {
- result = new ForkedEvaluationSheet(_masterBook.getSheet(_masterBook
- .getSheetIndex(sheetName)));
- _sharedSheetsByName.put(sheetName, result);
- }
- return result;
- }
-
- public void copyUpdatedCells(Workbook workbook) {
- String[] sheetNames = new String[_sharedSheetsByName.size()];
- _sharedSheetsByName.keySet().toArray(sheetNames);
- OrderedSheet[] oss = new OrderedSheet[sheetNames.length];
- for (int i = 0; i < sheetNames.length; i++) {
- String sheetName = sheetNames[i];
- oss[i] = new OrderedSheet(sheetName, _masterBook.getSheetIndex(sheetName));
- }
- for (int i = 0; i < oss.length; i++) {
- String sheetName = oss[i].getSheetName();
- ForkedEvaluationSheet sheet = _sharedSheetsByName.get(sheetName);
- sheet.copyUpdatedCells(workbook.getSheet(sheetName));
- }
- }
-
- public int convertFromExternSheetIndex(int externSheetIndex) {
- return _masterBook.convertFromExternSheetIndex(externSheetIndex);
- }
-
- public ExternalSheet getExternalSheet(int externSheetIndex) {
- return _masterBook.getExternalSheet(externSheetIndex);
- }
-
- public Ptg[] getFormulaTokens(EvaluationCell cell) {
- if (cell instanceof ForkedEvaluationCell) {
- // doesn't happen yet because formulas cannot be modified from the master workbook
- throw new RuntimeException("Updated formulas not supported yet");
- }
- return _masterBook.getFormulaTokens(cell);
- }
-
- public EvaluationName getName(NamePtg namePtg) {
- return _masterBook.getName(namePtg);
- }
-
- public EvaluationSheet getSheet(int sheetIndex) {
- return getSharedSheet(getSheetName(sheetIndex));
- }
-
- public int getSheetIndex(EvaluationSheet sheet) {
- if (sheet instanceof ForkedEvaluationSheet) {
- ForkedEvaluationSheet mes = (ForkedEvaluationSheet) sheet;
- return mes.getSheetIndex(_masterBook);
- }
- return _masterBook.getSheetIndex(sheet);
- }
-
- public int getSheetIndex(String sheetName) {
- return _masterBook.getSheetIndex(sheetName);
- }
-
- public String getSheetName(int sheetIndex) {
- return _masterBook.getSheetName(sheetIndex);
- }
-
- public String resolveNameXText(NameXPtg ptg) {
- return _masterBook.resolveNameXText(ptg);
- }
-
- private static final class OrderedSheet implements Comparable<OrderedSheet> {
- private final String _sheetName;
- private final int _index;
-
- public OrderedSheet(String sheetName, int index) {
- _sheetName = sheetName;
- _index = index;
- }
- public String getSheetName() {
- return _sheetName;
- }
- public int compareTo(OrderedSheet o) {
- return _index - o._index;
- }
- }
-}
+/* ====================================================================
+ 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.ss.formula.eval.forked;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.poi.hssf.record.formula.NamePtg;
+import org.apache.poi.hssf.record.formula.NameXPtg;
+import org.apache.poi.hssf.record.formula.Ptg;
+import org.apache.poi.ss.formula.EvaluationCell;
+import org.apache.poi.ss.formula.EvaluationName;
+import org.apache.poi.ss.formula.EvaluationSheet;
+import org.apache.poi.ss.formula.EvaluationWorkbook;
+import org.apache.poi.ss.usermodel.Workbook;
+
+/**
+ * Represents a workbook being used for forked evaluation. Most operations are delegated to the
+ * shared master workbook, except those that potentially involve cell values that may have been
+ * updated after a call to {@link #getOrCreateUpdatableCell(String, int, int)}.
+ *
+ * @author Josh Micich
+ */
+final class ForkedEvaluationWorkbook implements EvaluationWorkbook {
+
+ private final EvaluationWorkbook _masterBook;
+ private final Map<String, ForkedEvaluationSheet> _sharedSheetsByName;
+
+ public ForkedEvaluationWorkbook(EvaluationWorkbook master) {
+ _masterBook = master;
+ _sharedSheetsByName = new HashMap<String, ForkedEvaluationSheet>();
+ }
+
+ public ForkedEvaluationCell getOrCreateUpdatableCell(String sheetName, int rowIndex,
+ int columnIndex) {
+ ForkedEvaluationSheet sheet = getSharedSheet(sheetName);
+ return sheet.getOrCreateUpdatableCell(rowIndex, columnIndex);
+ }
+
+ public EvaluationCell getEvaluationCell(String sheetName, int rowIndex, int columnIndex) {
+ ForkedEvaluationSheet sheet = getSharedSheet(sheetName);
+ return sheet.getCell(rowIndex, columnIndex);
+ }
+
+ private ForkedEvaluationSheet getSharedSheet(String sheetName) {
+ ForkedEvaluationSheet result = _sharedSheetsByName.get(sheetName);
+ if (result == null) {
+ result = new ForkedEvaluationSheet(_masterBook.getSheet(_masterBook
+ .getSheetIndex(sheetName)));
+ _sharedSheetsByName.put(sheetName, result);
+ }
+ return result;
+ }
+
+ public void copyUpdatedCells(Workbook workbook) {
+ String[] sheetNames = new String[_sharedSheetsByName.size()];
+ _sharedSheetsByName.keySet().toArray(sheetNames);
+ OrderedSheet[] oss = new OrderedSheet[sheetNames.length];
+ for (int i = 0; i < sheetNames.length; i++) {
+ String sheetName = sheetNames[i];
+ oss[i] = new OrderedSheet(sheetName, _masterBook.getSheetIndex(sheetName));
+ }
+ for (int i = 0; i < oss.length; i++) {
+ String sheetName = oss[i].getSheetName();
+ ForkedEvaluationSheet sheet = _sharedSheetsByName.get(sheetName);
+ sheet.copyUpdatedCells(workbook.getSheet(sheetName));
+ }
+ }
+
+ public int convertFromExternSheetIndex(int externSheetIndex) {
+ return _masterBook.convertFromExternSheetIndex(externSheetIndex);
+ }
+
+ public ExternalSheet getExternalSheet(int externSheetIndex) {
+ return _masterBook.getExternalSheet(externSheetIndex);
+ }
+
+ public Ptg[] getFormulaTokens(EvaluationCell cell) {
+ if (cell instanceof ForkedEvaluationCell) {
+ // doesn't happen yet because formulas cannot be modified from the master workbook
+ throw new RuntimeException("Updated formulas not supported yet");
+ }
+ return _masterBook.getFormulaTokens(cell);
+ }
+
+ public EvaluationName getName(NamePtg namePtg) {
+ return _masterBook.getName(namePtg);
+ }
+
+ public EvaluationSheet getSheet(int sheetIndex) {
+ return getSharedSheet(getSheetName(sheetIndex));
+ }
+
+ public int getSheetIndex(EvaluationSheet sheet) {
+ if (sheet instanceof ForkedEvaluationSheet) {
+ ForkedEvaluationSheet mes = (ForkedEvaluationSheet) sheet;
+ return mes.getSheetIndex(_masterBook);
+ }
+ return _masterBook.getSheetIndex(sheet);
+ }
+
+ public int getSheetIndex(String sheetName) {
+ return _masterBook.getSheetIndex(sheetName);
+ }
+
+ public String getSheetName(int sheetIndex) {
+ return _masterBook.getSheetName(sheetIndex);
+ }
+
+ public String resolveNameXText(NameXPtg ptg) {
+ return _masterBook.resolveNameXText(ptg);
+ }
+
+ private static final class OrderedSheet implements Comparable<OrderedSheet> {
+ private final String _sheetName;
+ private final int _index;
+
+ public OrderedSheet(String sheetName, int index) {
+ _sheetName = sheetName;
+ _index = index;
+ }
+ public String getSheetName() {
+ return _sheetName;
+ }
+ public int compareTo(OrderedSheet o) {
+ return _index - o._index;
+ }
+ }
+}
Modified: poi/trunk/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluator.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluator.java?rev=776377&r1=776376&r2=776377&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluator.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluator.java Tue May 19 16:29:51 2009
@@ -1,133 +1,133 @@
-/* ====================================================================
- 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.ss.formula.eval.forked;
-
-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.NumberEval;
-import org.apache.poi.hssf.record.formula.eval.StringEval;
-import org.apache.poi.hssf.record.formula.eval.ValueEval;
-import org.apache.poi.hssf.usermodel.HSSFCell;
-import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook;
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment;
-import org.apache.poi.ss.formula.EvaluationCell;
-import org.apache.poi.ss.formula.EvaluationWorkbook;
-import org.apache.poi.ss.formula.IStabilityClassifier;
-import org.apache.poi.ss.formula.WorkbookEvaluator;
-import org.apache.poi.ss.usermodel.Workbook;
-
-/**
- * An alternative workbook evaluator that saves memory in situations where a single workbook is
- * concurrently and independently evaluated many times. With standard formula evaluation, around
- * 90% of memory consumption is due to loading of the {@link HSSFWorkbook} or {@link org.apache.poi.xssf.usermodel.XSSFWorkbook}.
- * This class enables a 'master workbook' to be loaded just once and shared between many evaluation
- * clients. Each evaluation client creates its own {@link ForkedEvaluator} and can set cell values
- * that will be used for local evaluations (and don't disturb evaluations on other evaluators).
- *
- * @author Josh Micich
- */
-public final class ForkedEvaluator {
-
- private WorkbookEvaluator _evaluator;
- private ForkedEvaluationWorkbook _sewb;
-
- private ForkedEvaluator(EvaluationWorkbook masterWorkbook, IStabilityClassifier stabilityClassifier) {
- _sewb = new ForkedEvaluationWorkbook(masterWorkbook);
- _evaluator = new WorkbookEvaluator(_sewb, stabilityClassifier);
- }
- private static EvaluationWorkbook createEvaluationWorkbook(Workbook wb) {
- if (wb instanceof HSSFWorkbook) {
- return HSSFEvaluationWorkbook.create((HSSFWorkbook) wb);
- }
-// TODO rearrange POI build to allow this
-// if (wb instanceof XSSFWorkbook) {
-// return XSSFEvaluationWorkbook.create((XSSFWorkbook) wb);
-// }
- throw new IllegalArgumentException("Unexpected workbook type (" + wb.getClass().getName() + ")");
- }
- public static ForkedEvaluator create(Workbook wb, IStabilityClassifier stabilityClassifier) {
- return new ForkedEvaluator(createEvaluationWorkbook(wb), stabilityClassifier);
- }
-
- /**
- * Sets the specified cell to the supplied <tt>value</tt>
- * @param sheetName the name of the sheet containing the cell
- * @param rowIndex zero based
- * @param columnIndex zero based
- */
- public void updateCell(String sheetName, int rowIndex, int columnIndex, ValueEval value) {
-
- ForkedEvaluationCell cell = _sewb.getOrCreateUpdatableCell(sheetName, rowIndex, columnIndex);
- cell.setValue(value);
- _evaluator.notifyUpdateCell(cell);
- }
- /**
- * Copies the values of all updated cells (modified by calls to {@link
- * #updateCell(String, int, int, ValueEval)}) to the supplied <tt>workbook</tt>.<br/>
- * Typically, the supplied <tt>workbook</tt> is a writable copy of the 'master workbook',
- * but at the very least it must contain sheets with the same names.
- */
- public void copyUpdatedCells(Workbook workbook) {
- _sewb.copyUpdatedCells(workbook);
- }
-
- /**
- * If cell contains a formula, the formula is evaluated and returned,
- * else the CellValue simply copies the appropriate cell value from
- * the cell and also its cell type. This method should be preferred over
- * evaluateInCell() when the call should not modify the contents of the
- * original cell.
- *
- * @param cell may be <code>null</code> signifying that the cell is not present (or blank)
- * @return <code>null</code> if the supplied cell is <code>null</code> or blank
- */
- public ValueEval evaluate(String sheetName, int rowIndex, int columnIndex) {
- EvaluationCell cell = _sewb.getEvaluationCell(sheetName, rowIndex, columnIndex);
-
- switch (cell.getCellType()) {
- case HSSFCell.CELL_TYPE_BOOLEAN:
- return BoolEval.valueOf(cell.getBooleanCellValue());
- case HSSFCell.CELL_TYPE_ERROR:
- return ErrorEval.valueOf(cell.getErrorCellValue());
- case HSSFCell.CELL_TYPE_FORMULA:
- return _evaluator.evaluate(cell);
- case HSSFCell.CELL_TYPE_NUMERIC:
- return new NumberEval(cell.getNumericCellValue());
- case HSSFCell.CELL_TYPE_STRING:
- return new StringEval(cell.getStringCellValue());
- case HSSFCell.CELL_TYPE_BLANK:
- return null;
- }
- throw new IllegalStateException("Bad cell type (" + cell.getCellType() + ")");
- }
- /**
- * Coordinates several formula evaluators together so that formulas that involve external
- * references can be evaluated.
- * @param workbookNames the simple file names used to identify the workbooks in formulas
- * with external links (for example "MyData.xls" as used in a formula "[MyData.xls]Sheet1!A1")
- * @param evaluators all evaluators for the full set of workbooks required by the formulas.
- */
- public static void setupEnvironment(String[] workbookNames, ForkedEvaluator[] evaluators) {
- WorkbookEvaluator[] wbEvals = new WorkbookEvaluator[evaluators.length];
- for (int i = 0; i < wbEvals.length; i++) {
- wbEvals[i] = evaluators[i]._evaluator;
- }
- CollaboratingWorkbooksEnvironment.setup(workbookNames, wbEvals);
- }
-}
+/* ====================================================================
+ 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.ss.formula.eval.forked;
+
+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.NumberEval;
+import org.apache.poi.hssf.record.formula.eval.StringEval;
+import org.apache.poi.hssf.record.formula.eval.ValueEval;
+import org.apache.poi.hssf.usermodel.HSSFCell;
+import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment;
+import org.apache.poi.ss.formula.EvaluationCell;
+import org.apache.poi.ss.formula.EvaluationWorkbook;
+import org.apache.poi.ss.formula.IStabilityClassifier;
+import org.apache.poi.ss.formula.WorkbookEvaluator;
+import org.apache.poi.ss.usermodel.Workbook;
+
+/**
+ * An alternative workbook evaluator that saves memory in situations where a single workbook is
+ * concurrently and independently evaluated many times. With standard formula evaluation, around
+ * 90% of memory consumption is due to loading of the {@link HSSFWorkbook} or {@link org.apache.poi.xssf.usermodel.XSSFWorkbook}.
+ * This class enables a 'master workbook' to be loaded just once and shared between many evaluation
+ * clients. Each evaluation client creates its own {@link ForkedEvaluator} and can set cell values
+ * that will be used for local evaluations (and don't disturb evaluations on other evaluators).
+ *
+ * @author Josh Micich
+ */
+public final class ForkedEvaluator {
+
+ private WorkbookEvaluator _evaluator;
+ private ForkedEvaluationWorkbook _sewb;
+
+ private ForkedEvaluator(EvaluationWorkbook masterWorkbook, IStabilityClassifier stabilityClassifier) {
+ _sewb = new ForkedEvaluationWorkbook(masterWorkbook);
+ _evaluator = new WorkbookEvaluator(_sewb, stabilityClassifier);
+ }
+ private static EvaluationWorkbook createEvaluationWorkbook(Workbook wb) {
+ if (wb instanceof HSSFWorkbook) {
+ return HSSFEvaluationWorkbook.create((HSSFWorkbook) wb);
+ }
+// TODO rearrange POI build to allow this
+// if (wb instanceof XSSFWorkbook) {
+// return XSSFEvaluationWorkbook.create((XSSFWorkbook) wb);
+// }
+ throw new IllegalArgumentException("Unexpected workbook type (" + wb.getClass().getName() + ")");
+ }
+ public static ForkedEvaluator create(Workbook wb, IStabilityClassifier stabilityClassifier) {
+ return new ForkedEvaluator(createEvaluationWorkbook(wb), stabilityClassifier);
+ }
+
+ /**
+ * Sets the specified cell to the supplied <tt>value</tt>
+ * @param sheetName the name of the sheet containing the cell
+ * @param rowIndex zero based
+ * @param columnIndex zero based
+ */
+ public void updateCell(String sheetName, int rowIndex, int columnIndex, ValueEval value) {
+
+ ForkedEvaluationCell cell = _sewb.getOrCreateUpdatableCell(sheetName, rowIndex, columnIndex);
+ cell.setValue(value);
+ _evaluator.notifyUpdateCell(cell);
+ }
+ /**
+ * Copies the values of all updated cells (modified by calls to {@link
+ * #updateCell(String, int, int, ValueEval)}) to the supplied <tt>workbook</tt>.<br/>
+ * Typically, the supplied <tt>workbook</tt> is a writable copy of the 'master workbook',
+ * but at the very least it must contain sheets with the same names.
+ */
+ public void copyUpdatedCells(Workbook workbook) {
+ _sewb.copyUpdatedCells(workbook);
+ }
+
+ /**
+ * If cell contains a formula, the formula is evaluated and returned,
+ * else the CellValue simply copies the appropriate cell value from
+ * the cell and also its cell type. This method should be preferred over
+ * evaluateInCell() when the call should not modify the contents of the
+ * original cell.
+ *
+ * @param cell may be <code>null</code> signifying that the cell is not present (or blank)
+ * @return <code>null</code> if the supplied cell is <code>null</code> or blank
+ */
+ public ValueEval evaluate(String sheetName, int rowIndex, int columnIndex) {
+ EvaluationCell cell = _sewb.getEvaluationCell(sheetName, rowIndex, columnIndex);
+
+ switch (cell.getCellType()) {
+ case HSSFCell.CELL_TYPE_BOOLEAN:
+ return BoolEval.valueOf(cell.getBooleanCellValue());
+ case HSSFCell.CELL_TYPE_ERROR:
+ return ErrorEval.valueOf(cell.getErrorCellValue());
+ case HSSFCell.CELL_TYPE_FORMULA:
+ return _evaluator.evaluate(cell);
+ case HSSFCell.CELL_TYPE_NUMERIC:
+ return new NumberEval(cell.getNumericCellValue());
+ case HSSFCell.CELL_TYPE_STRING:
+ return new StringEval(cell.getStringCellValue());
+ case HSSFCell.CELL_TYPE_BLANK:
+ return null;
+ }
+ throw new IllegalStateException("Bad cell type (" + cell.getCellType() + ")");
+ }
+ /**
+ * Coordinates several formula evaluators together so that formulas that involve external
+ * references can be evaluated.
+ * @param workbookNames the simple file names used to identify the workbooks in formulas
+ * with external links (for example "MyData.xls" as used in a formula "[MyData.xls]Sheet1!A1")
+ * @param evaluators all evaluators for the full set of workbooks required by the formulas.
+ */
+ public static void setupEnvironment(String[] workbookNames, ForkedEvaluator[] evaluators) {
+ WorkbookEvaluator[] wbEvals = new WorkbookEvaluator[evaluators.length];
+ for (int i = 0; i < wbEvals.length; i++) {
+ wbEvals[i] = evaluators[i]._evaluator;
+ }
+ CollaboratingWorkbooksEnvironment.setup(workbookNames, wbEvals);
+ }
+}
Modified: poi/trunk/src/java/org/apache/poi/ss/usermodel/CellValue.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/usermodel/CellValue.java?rev=776377&r1=776376&r2=776377&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/usermodel/CellValue.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/usermodel/CellValue.java Tue May 19 16:29:51 2009
@@ -1,114 +1,114 @@
-/* ====================================================================
- 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.ss.usermodel;
-
-import org.apache.poi.hssf.record.formula.eval.ErrorEval;
-import org.apache.poi.ss.usermodel.Cell;
-
-/**
- * Mimics the 'data view' of a cell. This allows formula evaluator
- * to return a CellValue instead of precasting the value to String
- * or Number or boolean type.
- * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
- */
-public final class CellValue {
- public static final CellValue TRUE = new CellValue(Cell.CELL_TYPE_BOOLEAN, 0.0, true, null, 0);
- public static final CellValue FALSE= new CellValue(Cell.CELL_TYPE_BOOLEAN, 0.0, false, null, 0);
-
- private final int _cellType;
- private final double _numberValue;
- private final boolean _booleanValue;
- private final String _textValue;
- private final int _errorCode;
-
- private CellValue(int cellType, double numberValue, boolean booleanValue,
- String textValue, int errorCode) {
- _cellType = cellType;
- _numberValue = numberValue;
- _booleanValue = booleanValue;
- _textValue = textValue;
- _errorCode = errorCode;
- }
-
-
- public CellValue(double numberValue) {
- this(Cell.CELL_TYPE_NUMERIC, numberValue, false, null, 0);
- }
- public static CellValue valueOf(boolean booleanValue) {
- return booleanValue ? TRUE : FALSE;
- }
- public CellValue(String stringValue) {
- this(Cell.CELL_TYPE_STRING, 0.0, false, stringValue, 0);
- }
- public static CellValue getError(int errorCode) {
- return new CellValue(Cell.CELL_TYPE_ERROR, 0.0, false, null, errorCode);
- }
-
-
- /**
- * @return Returns the booleanValue.
- */
- public boolean getBooleanValue() {
- return _booleanValue;
- }
- /**
- * @return Returns the numberValue.
- */
- public double getNumberValue() {
- return _numberValue;
- }
- /**
- * @return Returns the stringValue.
- */
- public String getStringValue() {
- return _textValue;
- }
- /**
- * @return Returns the cellType.
- */
- public int getCellType() {
- return _cellType;
- }
- /**
- * @return Returns the errorValue.
- */
- public byte getErrorValue() {
- return (byte) _errorCode;
- }
- public String toString() {
- StringBuffer sb = new StringBuffer(64);
- sb.append(getClass().getName()).append(" [");
- sb.append(formatAsString());
- sb.append("]");
- return sb.toString();
- }
-
- public String formatAsString() {
- switch (_cellType) {
- case Cell.CELL_TYPE_NUMERIC:
- return String.valueOf(_numberValue);
- case Cell.CELL_TYPE_STRING:
- return '"' + _textValue + '"';
- case Cell.CELL_TYPE_BOOLEAN:
- return _booleanValue ? "TRUE" : "FALSE";
- case Cell.CELL_TYPE_ERROR:
- return ErrorEval.getText(_errorCode);
- }
- return "<error unexpected cell type " + _cellType + ">";
- }
-}
+/* ====================================================================
+ 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.ss.usermodel;
+
+import org.apache.poi.hssf.record.formula.eval.ErrorEval;
+import org.apache.poi.ss.usermodel.Cell;
+
+/**
+ * Mimics the 'data view' of a cell. This allows formula evaluator
+ * to return a CellValue instead of precasting the value to String
+ * or Number or boolean type.
+ * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
+ */
+public final class CellValue {
+ public static final CellValue TRUE = new CellValue(Cell.CELL_TYPE_BOOLEAN, 0.0, true, null, 0);
+ public static final CellValue FALSE= new CellValue(Cell.CELL_TYPE_BOOLEAN, 0.0, false, null, 0);
+
+ private final int _cellType;
+ private final double _numberValue;
+ private final boolean _booleanValue;
+ private final String _textValue;
+ private final int _errorCode;
+
+ private CellValue(int cellType, double numberValue, boolean booleanValue,
+ String textValue, int errorCode) {
+ _cellType = cellType;
+ _numberValue = numberValue;
+ _booleanValue = booleanValue;
+ _textValue = textValue;
+ _errorCode = errorCode;
+ }
+
+
+ public CellValue(double numberValue) {
+ this(Cell.CELL_TYPE_NUMERIC, numberValue, false, null, 0);
+ }
+ public static CellValue valueOf(boolean booleanValue) {
+ return booleanValue ? TRUE : FALSE;
+ }
+ public CellValue(String stringValue) {
+ this(Cell.CELL_TYPE_STRING, 0.0, false, stringValue, 0);
+ }
+ public static CellValue getError(int errorCode) {
+ return new CellValue(Cell.CELL_TYPE_ERROR, 0.0, false, null, errorCode);
+ }
+
+
+ /**
+ * @return Returns the booleanValue.
+ */
+ public boolean getBooleanValue() {
+ return _booleanValue;
+ }
+ /**
+ * @return Returns the numberValue.
+ */
+ public double getNumberValue() {
+ return _numberValue;
+ }
+ /**
+ * @return Returns the stringValue.
+ */
+ public String getStringValue() {
+ return _textValue;
+ }
+ /**
+ * @return Returns the cellType.
+ */
+ public int getCellType() {
+ return _cellType;
+ }
+ /**
+ * @return Returns the errorValue.
+ */
+ public byte getErrorValue() {
+ return (byte) _errorCode;
+ }
+ public String toString() {
+ StringBuffer sb = new StringBuffer(64);
+ sb.append(getClass().getName()).append(" [");
+ sb.append(formatAsString());
+ sb.append("]");
+ return sb.toString();
+ }
+
+ public String formatAsString() {
+ switch (_cellType) {
+ case Cell.CELL_TYPE_NUMERIC:
+ return String.valueOf(_numberValue);
+ case Cell.CELL_TYPE_STRING:
+ return '"' + _textValue + '"';
+ case Cell.CELL_TYPE_BOOLEAN:
+ return _booleanValue ? "TRUE" : "FALSE";
+ case Cell.CELL_TYPE_ERROR:
+ return ErrorEval.getText(_errorCode);
+ }
+ return "<error unexpected cell type " + _cellType + ">";
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org