You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by on...@apache.org on 2015/11/29 13:14:46 UTC

svn commit: r1717054 - in /poi/trunk/src: java/org/apache/poi/hssf/usermodel/HSSFRow.java ooxml/java/org/apache/poi/xssf/streaming/SXSSFRow.java ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java

Author: onealj
Date: Sun Nov 29 12:14:45 2015
New Revision: 1717054

URL: http://svn.apache.org/viewvc?rev=1717054&view=rev
Log:
bug 58667: make SX/X/HSSFRow implement Comparable interface

Modified:
    poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFRow.java
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java

Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java?rev=1717054&r1=1717053&r2=1717054&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java Sun Nov 29 12:14:45 2015
@@ -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 Ro
       }
 
     }
-
-    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.getRowNum() == loc.getRowNum())
-        {
-            return 0;
+        if (this.getSheet() != other.getSheet()) {
+            throw new IllegalArgumentException("The compared rows must belong to the same sheet");
         }
-        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 Ro
         {
             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();
     }
 }

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFRow.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFRow.java?rev=1717054&r1=1717053&r2=1717054&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFRow.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFRow.java Sun Nov 29 12:14:45 2015
@@ -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);
+    }
 }
 

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java?rev=1717054&r1=1717053&r2=1717054&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java Sun Nov 29 12:14:45 2015
@@ -122,20 +122,31 @@ public class XSSFRow implements Row, Com
      * 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);
     }
 
     /**



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org