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/10/23 13:17:06 UTC

svn commit: r1710172 - in /poi/trunk/src: java/org/apache/poi/ss/util/CellRangeAddressBase.java testcases/org/apache/poi/ss/util/TestCellRangeAddress.java

Author: onealj
Date: Fri Oct 23 11:17:06 2015
New Revision: 1710172

URL: http://svn.apache.org/viewvc?rev=1710172&view=rev
Log:
bug 58441: define equals method for CellRangeAddressBase

Modified:
    poi/trunk/src/java/org/apache/poi/ss/util/CellRangeAddressBase.java
    poi/trunk/src/testcases/org/apache/poi/ss/util/TestCellRangeAddress.java

Modified: poi/trunk/src/java/org/apache/poi/ss/util/CellRangeAddressBase.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/util/CellRangeAddressBase.java?rev=1710172&r1=1710171&r2=1710172&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/util/CellRangeAddressBase.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/util/CellRangeAddressBase.java Fri Oct 23 11:17:06 2015
@@ -164,4 +164,36 @@ public abstract class CellRangeAddressBa
 		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();
+	}
 }

Modified: poi/trunk/src/testcases/org/apache/poi/ss/util/TestCellRangeAddress.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/util/TestCellRangeAddress.java?rev=1710172&r1=1710171&r2=1710172&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/util/TestCellRangeAddress.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/util/TestCellRangeAddress.java Fri Oct 23 11:17:06 2015
@@ -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
         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());
+    }
 }



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