From 8ee07a27f56301041bfb213f5f0dbc52f34b5641 Mon Sep 17 00:00:00 2001 From: Javen O'Neal Date: Mon, 20 Jun 2016 00:16:41 +0000 Subject: [PATCH] bug 56454: add CellRangeAddress#containsRow and containsColumn git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1749239 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/ss/util/CellRangeAddressBase.java | 26 +++++++++++++++-- .../poi/ss/util/TestCellRangeAddress.java | 29 +++++++++++++++++-- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/java/org/apache/poi/ss/util/CellRangeAddressBase.java b/src/java/org/apache/poi/ss/util/CellRangeAddressBase.java index 05ebdd2c2a..f94444d9a7 100644 --- a/src/java/org/apache/poi/ss/util/CellRangeAddressBase.java +++ b/src/java/org/apache/poi/ss/util/CellRangeAddressBase.java @@ -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 && diff --git a/src/testcases/org/apache/poi/ss/util/TestCellRangeAddress.java b/src/testcases/org/apache/poi/ss/util/TestCellRangeAddress.java index e11d3cf039..3f0f441451 100644 --- a/src/testcases/org/apache/poi/ss/util/TestCellRangeAddress.java +++ b/src/testcases/org/apache/poi/ss/util/TestCellRangeAddress.java @@ -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(); -- 2.39.5