bug 58441: define equals method for CellRangeAddressBase

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1710172 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2015-10-23 11:17:06 +00:00
parent 4780d329ef
commit 45e79a0409
2 changed files with 76 additions and 1 deletions

View File

@ -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();
}
}

View File

@ -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());
}
}