bug 58667: make SX/X/HSSFRow implement Comparable interface

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1717054 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2015-11-29 12:14:45 +00:00
parent 765fd556f1
commit 1ef3f4e2e8
3 changed files with 90 additions and 37 deletions

View File

@ -34,7 +34,7 @@ import org.apache.poi.util.Configurator;
*
* Only rows that have cells should be added to a Sheet.
*/
public final class HSSFRow implements Row {
public final class HSSFRow implements Row, Comparable<HSSFRow> {
// used for collections
public final static int INITIAL_CAPACITY = Configurator.getIntValue("HSSFRow.ColInitialCapacity", 5);
@ -663,24 +663,38 @@ public final class HSSFRow implements Row {
}
}
public int compareTo(Object obj)
/**
* Compares two <code>HSSFRow</code> objects. Two rows are equal if they belong to the same worksheet and
* their row indexes are equal.
*
* @param row the <code>HSSFRow</code> to be compared.
* @return <ul>
* <li>
* the value <code>0</code> if the row number of this <code>HSSFRow</code> is
* equal to the row number of the argument <code>HSSFRow</code>
* </li>
* <li>
* a value less than <code>0</code> if the row number of this this <code>HSSFRow</code> is
* numerically less than the row number of the argument <code>HSSFRow</code>
* </li>
* <li>
* a value greater than <code>0</code> if the row number of this this <code>HSSFRow</code> is
* numerically greater than the row number of the argument <code>HSSFRow</code>
* </li>
* </ul>
* @throws IllegalArgumentException if the argument row belongs to a different worksheet
*/
@Override
public int compareTo(HSSFRow other)
{
HSSFRow loc = (HSSFRow) obj;
if (this.getSheet() != other.getSheet()) {
throw new IllegalArgumentException("The compared rows must belong to the same sheet");
}
if (this.getRowNum() == loc.getRowNum())
{
return 0;
}
if (this.getRowNum() < loc.getRowNum())
{
return -1;
}
if (this.getRowNum() > loc.getRowNum())
{
return 1;
}
return -1;
Integer thisRow = this.getRowNum();
Integer otherRow = other.getRowNum();
return thisRow.compareTo(otherRow);
}
@Override
@ -690,18 +704,14 @@ public final class HSSFRow implements Row {
{
return false;
}
HSSFRow loc = (HSSFRow) obj;
HSSFRow other = (HSSFRow) obj;
if (this.getRowNum() == loc.getRowNum())
{
return true;
}
return false;
return (this.getRowNum() == other.getRowNum()) &&
(this.getSheet() == other.getSheet());
}
@Override
public int hashCode() {
assert false : "hashCode not designed";
return 42; // any arbitrary constant will do
return row.hashCode();
}
}

View File

@ -32,7 +32,7 @@ import org.apache.poi.util.Internal;
*
* @author Alex Geller, Four J's Development Tools
*/
public class SXSSFRow implements Row
public class SXSSFRow implements Row, Comparable<SXSSFRow>
{
private final SXSSFSheet _sheet;
private SXSSFCell[] _cells;
@ -505,5 +505,37 @@ public class SXSSFRow implements Row
throw new UnsupportedOperationException();
}
}
/**
* Compares two <code>SXSSFRow</code> objects. Two rows are equal if they belong to the same worksheet and
* their row indexes are equal.
*
* @param other the <code>SXSSFRow</code> to be compared.
* @return <ul>
* <li>
* the value <code>0</code> if the row number of this <code>SXSSFRow</code> is
* equal to the row number of the argument <code>SXSSFRow</code>
* </li>
* <li>
* a value less than <code>0</code> if the row number of this this <code>SXSSFRow</code> is
* numerically less than the row number of the argument <code>SXSSFRow</code>
* </li>
* <li>
* a value greater than <code>0</code> if the row number of this this <code>SXSSFRow</code> is
* numerically greater than the row number of the argument <code>SXSSFRow</code>
* </li>
* </ul>
* @throws IllegalArgumentException if the argument row belongs to a different worksheet
*/
@Override
public int compareTo(SXSSFRow other) {
if (this.getSheet() != other.getSheet()) {
throw new IllegalArgumentException("The compared rows must belong to the same sheet");
}
Integer thisRow = this.getRowNum();
Integer otherRow = other.getRowNum();
return thisRow.compareTo(otherRow);
}
}

View File

@ -122,20 +122,31 @@ public class XSSFRow implements Row, Comparable<XSSFRow> {
* their row indexes are equal.
*
* @param row the <code>XSSFRow</code> to be compared.
* @return the value <code>0</code> if the row number of this <code>XSSFRow</code> is
* equal to the row number of the argument <code>XSSFRow</code>; a value less than
* <code>0</code> if the row number of this this <code>XSSFRow</code> is numerically less
* than the row number of the argument <code>XSSFRow</code>; and a value greater
* than <code>0</code> if the row number of this this <code>XSSFRow</code> is numerically
* greater than the row number of the argument <code>XSSFRow</code>.
* @return <ul>
* <li>
* the value <code>0</code> if the row number of this <code>XSSFRow</code> is
* equal to the row number of the argument <code>XSSFRow</code>
* </li>
* <li>
* a value less than <code>0</code> if the row number of this this <code>XSSFRow</code> is
* numerically less than the row number of the argument <code>XSSFRow</code>
* </li>
* <li>
* a value greater than <code>0</code> if the row number of this this <code>XSSFRow</code> is
* numerically greater than the row number of the argument <code>XSSFRow</code>
* </li>
* </ul>
* @throws IllegalArgumentException if the argument row belongs to a different worksheet
*/
public int compareTo(XSSFRow row) {
int thisVal = this.getRowNum();
if(row.getSheet() != getSheet()) throw new IllegalArgumentException("The compared rows must belong to the same XSSFSheet");
@Override
public int compareTo(XSSFRow other) {
if (this.getSheet() != other.getSheet()) {
throw new IllegalArgumentException("The compared rows must belong to the same sheet");
}
int anotherVal = row.getRowNum();
return (thisVal < anotherVal ? -1 : (thisVal == anotherVal ? 0 : 1));
Integer thisRow = this.getRowNum();
Integer otherRow = other.getRowNum();
return thisRow.compareTo(otherRow);
}
/**