]> source.dussan.org Git - poi.git/commitdiff
bug 56454: add CellRangeAddress#containsRow and containsColumn
authorJaven O'Neal <onealj@apache.org>
Mon, 20 Jun 2016 00:16:41 +0000 (00:16 +0000)
committerJaven O'Neal <onealj@apache.org>
Mon, 20 Jun 2016 00:16:41 +0000 (00:16 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1749239 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/ss/util/CellRangeAddressBase.java
src/testcases/org/apache/poi/ss/util/TestCellRangeAddress.java

index 05ebdd2c2ae0ab8f11da3c4c157c523eaad5be96..f94444d9a791b3abaf8f54db3721e7146e5ac095 100644 (file)
@@ -118,10 +118,31 @@ public abstract class CellRangeAddressBase {
         * @param rowInd The row, 0-based.
         * @param colInd The column, 0-based.
         * @return True if the coordinates lie within the bounds, false otherwise.
+        * @see #intersects(CellRangeAddressBase) for checking if two ranges overlap
         */
        public boolean isInRange(int rowInd, int colInd) {
-               return _firstRow <= rowInd && rowInd <= _lastRow &&
-                               _firstCol <= colInd && colInd <= _lastCol;
+               return _firstRow <= rowInd && rowInd <= _lastRow && //containsRow
+                               _firstCol <= colInd && colInd <= _lastCol; //containsColumn
+       }
+       
+       /**
+        * Check if the row is in the specified cell range
+        *
+        * @param rowInd the row to check
+        * @return true if the range contains the row [rowInd]
+        */
+       public boolean containsRow(int rowInd) {
+               return _firstRow <= rowInd && rowInd <= _lastRow;
+       }
+       
+       /**
+        * Check if the column is in the specified cell range
+        *
+        * @param colInd the column to check
+        * @return true if the range contains the column [colInd]
+        */
+       public boolean containsColumn(int colInd) {
+               return _firstCol <= colInd && colInd <= _lastCol;
        }
        
        /**
@@ -129,6 +150,7 @@ public abstract class CellRangeAddressBase {
         *
         * @param other a candidate cell range address to check for intersection with this range
         * @return returns true if this range and other range have at least 1 cell in common
+        * @see #isInRange(int, int) for checking if a single cell intersects
         */
        public boolean intersects(CellRangeAddressBase other) {
                return this._firstRow <= other._lastRow &&
index e11d3cf039a55f13e79231016ab978c6bcdbacda..3f0f4414511b4828a2b27fe314c9ea9a6ef79fd0 100644 (file)
@@ -18,6 +18,7 @@ limitations under the License.
 package org.apache.poi.ss.util;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
@@ -32,8 +33,10 @@ import org.apache.poi.util.LittleEndianOutputStream;
 import org.junit.Test;
 
 public final class TestCellRangeAddress {
-    static final byte[] data = new byte[] { (byte) 0x02, (byte) 0x00, (byte) 0x04,
-            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x00, };
+    static final byte[] data = new byte[] {
+            0x02, 0x00, 0x04, 0x00,
+            0x00, 0x00, 0x03, 0x00,
+    };
 
     @Test
     public void testLoad() {
@@ -241,6 +244,28 @@ public final class TestCellRangeAddress {
         assertNotIntersects(baseRegion, disjointRegion);
     }
     
+    @Test
+    public void containsRow() {
+        final CellRangeAddress region = new CellRangeAddress(10, 12, 3, 5);
+        
+        assertFalse(region.containsRow(9));
+        assertTrue(region.containsRow(10));
+        assertTrue(region.containsRow(11));
+        assertTrue(region.containsRow(12));
+        assertFalse(region.containsRow(13));
+    }
+    
+    @Test
+    public void containsColumn() {
+        final CellRangeAddress region = new CellRangeAddress(10, 12, 3, 5);
+        
+        assertFalse(region.containsColumn(2));
+        assertTrue(region.containsColumn(3));
+        assertTrue(region.containsColumn(4));
+        assertTrue(region.containsColumn(5));
+        assertFalse(region.containsColumn(6));
+    }
+    
     private static void assertIntersects(CellRangeAddress regionA, CellRangeAddress regionB) {
         if (!(regionA.intersects(regionB) && regionB.intersects(regionA))) {
             final String A = regionA.formatAsString();