Author: josh
Date: Tue Dec 22 03:00:21 2009
New Revision: 893063

URL: http://svn.apache.org/viewvc?rev=893063&view=rev
Log:
Added getRow() and getColumn() functions to TwoDEval to simplify logic in INDEX 
implementation.

Modified:
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/Index.java
    poi/trunk/src/java/org/apache/poi/ss/formula/LazyAreaEval.java
    poi/trunk/src/java/org/apache/poi/ss/formula/TwoDEval.java
    
poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/TestRangeEval.java
    
poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/functions/EvalFactory.java

Modified: 
poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/Index.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/Index.java?rev=893063&r1=893062&r2=893063&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/Index.java 
(original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/Index.java 
Tue Dec 22 03:00:21 2009
@@ -17,7 +17,6 @@
 
 package org.apache.poi.hssf.record.formula.functions;
 
-import org.apache.poi.hssf.record.formula.eval.AreaEval;
 import org.apache.poi.hssf.record.formula.eval.BlankEval;
 import org.apache.poi.hssf.record.formula.eval.ErrorEval;
 import org.apache.poi.hssf.record.formula.eval.EvaluationException;
@@ -53,7 +52,7 @@
                int columnIx = 0;
                try {
                        int rowIx = resolveIndexArg(arg1, srcRowIndex, 
srcColumnIndex);
-                       
+
                        if (!reference.isColumn()) {
                                if (!reference.isRow()) {
                                        // always an error with 2-D area refs
@@ -65,7 +64,7 @@
                                columnIx = rowIx;
                                rowIx = 0;
                        }
- 
+
                        return getValueFromArea(reference, rowIx, columnIx);
                } catch (EvaluationException e) {
                        return e.getErrorEval();
@@ -125,45 +124,27 @@
                        throws EvaluationException {
                assert pRowIx >= 0;
                assert pColumnIx >= 0;
-       
-               int width = ae.getWidth();
-               int height = ae.getHeight();
-               
-               int relFirstRowIx;
-               int relLastRowIx;
-
-               if ((pRowIx == 0)) {
-                       relFirstRowIx = 0;
-                       relLastRowIx = height-1;
-               } else {
+
+               TwoDEval result = ae;
+
+               if (pRowIx != 0) {
                        // Slightly irregular logic for bounds checking errors
-                       if (pRowIx > height) {
+                       if (pRowIx > ae.getHeight()) {
                                // high bounds check fail gives #REF! if arg 
was explicitly passed
                                throw new 
EvaluationException(ErrorEval.REF_INVALID);
                        }
-                       int rowIx = pRowIx-1;
-                       relFirstRowIx = rowIx;
-                       relLastRowIx = rowIx;
+                       result = result.getRow(pRowIx-1);
                }
 
-               int relFirstColIx;
-               int relLastColIx;
-               if ((pColumnIx == 0)) {
-                       relFirstColIx = 0;
-                       relLastColIx = width-1;
-               } else {
+               if (pColumnIx != 0) {
                        // Slightly irregular logic for bounds checking errors
-                       if (pColumnIx > width) {
+                       if (pColumnIx > ae.getWidth()) {
                                // high bounds check fail gives #REF! if arg 
was explicitly passed
                                throw new 
EvaluationException(ErrorEval.REF_INVALID);
                        }
-                       int columnIx = pColumnIx-1;
-                       relFirstColIx = columnIx;
-                       relLastColIx = columnIx;
+                       result = result.getColumn(pColumnIx-1);
                }
-
-               AreaEval x = ((AreaEval) ae);
-               return x.offset(relFirstRowIx, relLastRowIx, relFirstColIx, 
relLastColIx);
+               return result;
        }
 
 

Modified: poi/trunk/src/java/org/apache/poi/ss/formula/LazyAreaEval.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/LazyAreaEval.java?rev=893063&r1=893062&r2=893063&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/LazyAreaEval.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/LazyAreaEval.java Tue Dec 22 
03:00:21 2009
@@ -57,6 +57,23 @@
 
                return new LazyAreaEval(area, _evaluator);
        }
+       public LazyAreaEval getRow(int rowIndex) {
+               if (rowIndex >= getHeight()) {
+                       throw new IllegalArgumentException("Invalid rowIndex " 
+ rowIndex
+                                       + ".  Allowable range is (0.." + 
getHeight() + ").");
+               }
+               int absRowIx = getFirstRow() + rowIndex;
+               return new LazyAreaEval(absRowIx, getFirstColumn(), absRowIx, 
getLastColumn(), _evaluator);
+       }
+       public LazyAreaEval getColumn(int columnIndex) {
+               if (columnIndex >= getWidth()) {
+                       throw new IllegalArgumentException("Invalid columnIndex 
" + columnIndex
+                                       + ".  Allowable range is (0.." + 
getWidth() + ").");
+               }
+               int absColIx = getFirstColumn() + columnIndex;
+               return new LazyAreaEval(getFirstRow(), absColIx, getLastRow(), 
absColIx, _evaluator);
+       }
+
        public String toString() {
                CellReference crA = new CellReference(getFirstRow(), 
getFirstColumn());
                CellReference crB = new CellReference(getLastRow(), 
getLastColumn());

Modified: poi/trunk/src/java/org/apache/poi/ss/formula/TwoDEval.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/TwoDEval.java?rev=893063&r1=893062&r2=893063&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/TwoDEval.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/TwoDEval.java Tue Dec 22 
03:00:21 2009
@@ -28,11 +28,11 @@
 public interface TwoDEval extends ValueEval {
 
        /**
-        * @param row relative row index (zero based)
-        * @param col relative column index (zero based)
-        * @return element at the specified row and col position
+        * @param rowIndex relative row index (zero based)
+        * @param columnIndex relative column index (zero based)
+        * @return element at the specified row and column position
         */
-       public ValueEval getValue(int row, int col);
+       ValueEval getValue(int rowIndex, int columnIndex);
 
        int getWidth();
        int getHeight();
@@ -48,4 +48,15 @@
         * the trivial case when the area has just a single cell.
         */
        boolean isColumn();
+
+       /**
+        * @param rowIndex relative row index (zero based)
+        * @return a single row {...@link TwoDEval}
+        */
+       TwoDEval getRow(int rowIndex);
+       /**
+        * @param columnIndex relative column index (zero based)
+        * @return a single column {...@link TwoDEval}
+        */
+       TwoDEval getColumn(int columnIndex);
 }

Modified: 
poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/TestRangeEval.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/TestRangeEval.java?rev=893063&r1=893062&r2=893063&view=diff
==============================================================================
--- 
poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/TestRangeEval.java
 (original)
+++ 
poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/eval/TestRangeEval.java
 Tue Dec 22 03:00:21 2009
@@ -28,6 +28,7 @@
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.hssf.util.AreaReference;
 import org.apache.poi.hssf.util.CellReference;
+import org.apache.poi.ss.formula.TwoDEval;
 import org.apache.poi.ss.usermodel.CellValue;
 
 /**
@@ -90,6 +91,9 @@
                public MockAreaEval(AreaI ptg) {
                        super(ptg);
                }
+               private MockAreaEval(int firstRow, int firstColumn, int 
lastRow, int lastColumn) {
+                       super(firstRow, firstColumn, lastRow, lastColumn);
+               }
                public ValueEval getRelativeValue(int relativeRowIndex, int 
relativeColumnIndex) {
                        throw new RuntimeException("not expected to be called 
during this test");
                }
@@ -100,6 +104,20 @@
 
                        return new MockAreaEval(area);
                }
+               public TwoDEval getRow(int rowIndex) {
+                       if (rowIndex >= getHeight()) {
+                               throw new IllegalArgumentException("Invalid 
rowIndex " + rowIndex
+                                               + ".  Allowable range is (0.." 
+ getHeight() + ").");
+                       }
+                       return new MockAreaEval(rowIndex, getFirstColumn(), 
rowIndex, getLastColumn());
+               }
+               public TwoDEval getColumn(int columnIndex) {
+                       if (columnIndex >= getWidth()) {
+                               throw new IllegalArgumentException("Invalid 
columnIndex " + columnIndex
+                                               + ".  Allowable range is (0.." 
+ getWidth() + ").");
+                       }
+                       return new MockAreaEval(getFirstRow(), columnIndex, 
getLastRow(), columnIndex);
+               }
        }
 
        public void testRangeUsingOffsetFunc_bug46948() {

Modified: 
poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/functions/EvalFactory.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/functions/EvalFactory.java?rev=893063&r1=893062&r2=893063&view=diff
==============================================================================
--- 
poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/functions/EvalFactory.java
 (original)
+++ 
poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/functions/EvalFactory.java
 Tue Dec 22 03:00:21 2009
@@ -27,6 +27,7 @@
 import org.apache.poi.hssf.record.formula.eval.RefEval;
 import org.apache.poi.hssf.record.formula.eval.RefEvalBase;
 import org.apache.poi.hssf.record.formula.eval.ValueEval;
+import org.apache.poi.ss.formula.TwoDEval;
 
 /**
  * Test helper class for creating mock <code>Eval</code> objects
@@ -119,7 +120,7 @@
                        int width = relLastColIx - relFirstColIx + 1;
                        ValueEval[] result = new ValueEval[height * width];
                        for (int r=0; r<height; r++) {
-                               int srcRowIx = r + relFirstRowIx; 
+                               int srcRowIx = r + relFirstRowIx;
                                for (int c=0; c<width; c++) {
                                        int srcColIx = c + relFirstColIx;
                                        int destIx = r * width + c;
@@ -129,6 +130,28 @@
                        }
                        return result;
                }
+               public TwoDEval getRow(int rowIndex) {
+                       if (rowIndex >= getHeight()) {
+                               throw new IllegalArgumentException("Invalid 
rowIndex " + rowIndex
+                                               + ".  Allowable range is (0.." 
+ getHeight() + ").");
+                       }
+                       ValueEval[] values = new ValueEval[getWidth()];
+                       for (int i = 0; i < values.length; i++) {
+                               values[i] = getRelativeValue(rowIndex, i);
+                       }
+                       return new MockAreaEval(rowIndex, getFirstColumn(), 
rowIndex, getLastColumn(), values);
+               }
+               public TwoDEval getColumn(int columnIndex) {
+                       if (columnIndex >= getWidth()) {
+                               throw new IllegalArgumentException("Invalid 
columnIndex " + columnIndex
+                                               + ".  Allowable range is (0.." 
+ getWidth() + ").");
+                       }
+                       ValueEval[] values = new ValueEval[getHeight()];
+                       for (int i = 0; i < values.length; i++) {
+                               values[i] = getRelativeValue(i, columnIndex);
+                       }
+                       return new MockAreaEval(getFirstRow(), columnIndex, 
getLastRow(), columnIndex, values);
+               }
        }
 
        private static final class MockRefEval extends RefEvalBase {



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to