diff --git a/src/java/org/apache/poi/ss/util/CellRangeAddressBase.java b/src/java/org/apache/poi/ss/util/CellRangeAddressBase.java index 4b5b10424..9561cb9f5 100644 --- a/src/java/org/apache/poi/ss/util/CellRangeAddressBase.java +++ b/src/java/org/apache/poi/ss/util/CellRangeAddressBase.java @@ -164,4 +164,36 @@ public abstract class CellRangeAddressBase { 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(); + } } diff --git a/src/testcases/org/apache/poi/ss/util/TestCellRangeAddress.java b/src/testcases/org/apache/poi/ss/util/TestCellRangeAddress.java index c5a163394..5616f79f3 100644 --- a/src/testcases/org/apache/poi/ss/util/TestCellRangeAddress.java +++ b/src/testcases/org/apache/poi/ss/util/TestCellRangeAddress.java @@ -20,7 +20,9 @@ package org.apache.poi.ss.util; 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 @@ public final class TestCellRangeAddress extends TestCase { 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()); + } }