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 16c1afa0d0
commit 74f0442563
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. * 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 // used for collections
public final static int INITIAL_CAPACITY = Configurator.getIntValue("HSSFRow.ColInitialCapacity", 5); public final static int INITIAL_CAPACITY = Configurator.getIntValue("HSSFRow.ColInitialCapacity", 5);
@ -664,23 +664,37 @@ 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()) Integer thisRow = this.getRowNum();
{ Integer otherRow = other.getRowNum();
return 0; return thisRow.compareTo(otherRow);
}
if (this.getRowNum() < loc.getRowNum())
{
return -1;
}
if (this.getRowNum() > loc.getRowNum())
{
return 1;
}
return -1;
} }
@Override @Override
@ -690,18 +704,14 @@ public final class HSSFRow implements Row {
{ {
return false; return false;
} }
HSSFRow loc = (HSSFRow) obj; HSSFRow other = (HSSFRow) obj;
if (this.getRowNum() == loc.getRowNum()) return (this.getRowNum() == other.getRowNum()) &&
{ (this.getSheet() == other.getSheet());
return true;
}
return false;
} }
@Override @Override
public int hashCode() { public int hashCode() {
assert false : "hashCode not designed"; return row.hashCode();
return 42; // any arbitrary constant will do
} }
} }

View File

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