diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java b/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java index 2e742cecf4..8ff41cb18a 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java @@ -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 { // 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 HSSFRow objects. Two rows are equal if they belong to the same worksheet and + * their row indexes are equal. + * + * @param row the HSSFRow to be compared. + * @return + * @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(); } } diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFRow.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFRow.java index 46d2c017c0..068f5465c3 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFRow.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFRow.java @@ -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 { private final SXSSFSheet _sheet; private SXSSFCell[] _cells; @@ -505,5 +505,37 @@ public class SXSSFRow implements Row throw new UnsupportedOperationException(); } } + + /** + * Compares two SXSSFRow objects. Two rows are equal if they belong to the same worksheet and + * their row indexes are equal. + * + * @param other the SXSSFRow to be compared. + * @return + * @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); + } } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java index a3d647c9b4..c7eaf1e371 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java @@ -122,20 +122,31 @@ public class XSSFRow implements Row, Comparable { * their row indexes are equal. * * @param row the XSSFRow to be compared. - * @return the value 0 if the row number of this XSSFRow is - * equal to the row number of the argument XSSFRow; a value less than - * 0 if the row number of this this XSSFRow is numerically less - * than the row number of the argument XSSFRow; and a value greater - * than 0 if the row number of this this XSSFRow is numerically - * greater than the row number of the argument XSSFRow. + * @return
    + *
  • + * the value 0 if the row number of this XSSFRow is + * equal to the row number of the argument XSSFRow + *
  • + *
  • + * a value less than 0 if the row number of this this XSSFRow is + * numerically less than the row number of the argument XSSFRow + *
  • + *
  • + * a value greater than 0 if the row number of this this XSSFRow is + * numerically greater than the row number of the argument XSSFRow + *
  • + *
* @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); } /**