bug 56454: add CellRangeAddress#containsRow and containsColumn

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1749239 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2016-06-20 00:16:41 +00:00
parent 5be01c04f4
commit 8ee07a27f5
2 changed files with 51 additions and 4 deletions

View 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 &&

View 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();