mirror of https://github.com/apache/poi.git
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:
parent
5be01c04f4
commit
8ee07a27f5
|
@ -118,10 +118,31 @@ public abstract class CellRangeAddressBase {
|
||||||
* @param rowInd The row, 0-based.
|
* @param rowInd The row, 0-based.
|
||||||
* @param colInd The column, 0-based.
|
* @param colInd The column, 0-based.
|
||||||
* @return True if the coordinates lie within the bounds, false otherwise.
|
* @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) {
|
public boolean isInRange(int rowInd, int colInd) {
|
||||||
return _firstRow <= rowInd && rowInd <= _lastRow &&
|
return _firstRow <= rowInd && rowInd <= _lastRow && //containsRow
|
||||||
_firstCol <= colInd && colInd <= _lastCol;
|
_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
|
* @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
|
* @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) {
|
public boolean intersects(CellRangeAddressBase other) {
|
||||||
return this._firstRow <= other._lastRow &&
|
return this._firstRow <= other._lastRow &&
|
||||||
|
|
|
@ -18,6 +18,7 @@ limitations under the License.
|
||||||
package org.apache.poi.ss.util;
|
package org.apache.poi.ss.util;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertNotEquals;
|
import static org.junit.Assert.assertNotEquals;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
@ -32,8 +33,10 @@ import org.apache.poi.util.LittleEndianOutputStream;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public final class TestCellRangeAddress {
|
public final class TestCellRangeAddress {
|
||||||
static final byte[] data = new byte[] { (byte) 0x02, (byte) 0x00, (byte) 0x04,
|
static final byte[] data = new byte[] {
|
||||||
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x00, };
|
0x02, 0x00, 0x04, 0x00,
|
||||||
|
0x00, 0x00, 0x03, 0x00,
|
||||||
|
};
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLoad() {
|
public void testLoad() {
|
||||||
|
@ -241,6 +244,28 @@ public final class TestCellRangeAddress {
|
||||||
assertNotIntersects(baseRegion, disjointRegion);
|
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) {
|
private static void assertIntersects(CellRangeAddress regionA, CellRangeAddress regionB) {
|
||||||
if (!(regionA.intersects(regionB) && regionB.intersects(regionA))) {
|
if (!(regionA.intersects(regionB) && regionB.intersects(regionA))) {
|
||||||
final String A = regionA.formatAsString();
|
final String A = regionA.formatAsString();
|
||||||
|
|
Loading…
Reference in New Issue