]> source.dussan.org Git - poi.git/commitdiff
bug 58441: define equals method for CellRangeAddressBase
authorJaven O'Neal <onealj@apache.org>
Fri, 23 Oct 2015 11:17:06 +0000 (11:17 +0000)
committerJaven O'Neal <onealj@apache.org>
Fri, 23 Oct 2015 11:17:06 +0000 (11:17 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1710172 13f79535-47bb-0310-9956-ffa450edef68

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

index 4b5b1042444ef635bf4b841b108c551d09d33fe0..9561cb9f5bc889578645f55ada16b4e94028e968 100644 (file)
@@ -164,4 +164,36 @@ public abstract class CellRangeAddressBase {
                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();
+       }
 }
index c5a163394c657a583543164c1a88e4018d932f5f..5616f79f38e0c7c319aa65a36357de9cd5363939 100644 (file)
@@ -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 extends TestCase {
         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());
+    }
 }