--- src/java/org/apache/poi/ss/util/CellRangeAddressBase.java (revision 1704432) +++ src/java/org/apache/poi/ss/util/CellRangeAddressBase.java (working copy) @@ -164,4 +164,36 @@ CellReference crB = new CellReference(_lastRow, _lastCol); return getClass().getName() + " [" + crA.formatAsString() + ":" + crB.formatAsString() +"]"; } + + // In case _firstRow > _lastRow or _firstCol > _lastCol + protected int getMinRow() { + return Math.min(_firstRow, _lastRow); + } + protected int getMaxRow() { + return Math.max(_firstRow, _lastRow); + } + protected int getMinColumn() { + return Math.min(_firstCol, _lastCol); + } + protected int getMaxColumn() { + return Math.max(_firstCol, _lastCol); + } + + @Override + public boolean equals(Object other) { + if (other instanceof CellRangeAddressBase) { + CellRangeAddressBase o = (CellRangeAddressBase) other; + return ((getMinRow() == o.getMinRow()) && + (getMaxRow() == o.getMaxRow()) && + (getMinColumn() == o.getMinColumn()) && + (getMaxColumn() == o.getMaxColumn())); + } + return false; + } + + @Override + public int hashCode() { + final int[] values = new int[]{getMinRow(), getMaxRow(), getMinColumn(), getMaxColumn()}; + return values.hashCode(); + } } --- src/testcases/org/apache/poi/ss/util/TestCellRangeAddress.java (revision 1704432) +++ src/testcases/org/apache/poi/ss/util/TestCellRangeAddress.java (working copy) @@ -20,7 +20,9 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; -import junit.framework.TestCase; +//TODO: replace junit3 with junit4 code +import junit.framework.TestCase; //junit3 +import static org.junit.Assert.assertNotEquals; //junit4 import org.apache.poi.hssf.record.TestcaseRecordInputStream; import org.apache.poi.util.LittleEndianOutputStream; @@ -190,4 +192,45 @@ ref = new CellRangeAddress(-1, -1, -1, -1); assertEquals(":", ref.formatAsString()); } + + public void testEquals() { + final CellRangeAddress ref1 = new CellRangeAddress(1, 2, 3, 4); + final CellRangeAddress ref2 = new CellRangeAddress(1, 2, 3, 4); + assertEquals(ref1, ref2); + + // Invert first/last row, but refer to same area + ref2.setFirstRow(2); + ref2.setLastRow(1); + assertEquals(ref1, ref2); + + // Invert first/last column, but refer to same area + ref2.setFirstColumn(4); + ref2.setLastColumn(3); + assertEquals(ref1, ref2); + + // Refer to a different area + assertNotEquals(ref1, new CellRangeAddress(3, 4, 1, 2)); + } + + public void testGetMinMaxRow() { + final CellRangeAddress ref = new CellRangeAddress(1, 2, 3, 4); + assertEquals(1, ref.getMinRow()); + assertEquals(2, ref.getMaxRow()); + + ref.setFirstRow(10); + //now ref is CellRangeAddress(10, 2, 3, 4) + assertEquals(2, ref.getMinRow()); + assertEquals(10, ref.getMaxRow()); + } + + public void testGetMinMaxColumn() { + final CellRangeAddress ref = new CellRangeAddress(1, 2, 3, 4); + assertEquals(3, ref.getMinColumn()); + assertEquals(4, ref.getMaxColumn()); + + ref.setFirstColumn(10); + //now ref is CellRangeAddress(1, 2, 10, 4) + assertEquals(4, ref.getMinColumn()); + assertEquals(10, ref.getMaxColumn()); + } }