JAL-1989 equals method strengthened to fully match selection/hidden cols
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 30 May 2016 13:46:13 +0000 (14:46 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Mon, 30 May 2016 13:46:13 +0000 (14:46 +0100)
src/jalview/datamodel/ColumnSelection.java
test/jalview/datamodel/ColumnSelectionTest.java

index 74c58b7..aaf70b8 100644 (file)
@@ -1651,14 +1651,56 @@ public class ColumnSelection
     return hashCode;
   }
 
+  /**
+   * Answers true if comparing to a ColumnSelection with the same selected
+   * columns and hidden columns, else false
+   */
   @Override
   public boolean equals(Object obj)
   {
-    if (obj instanceof ColumnSelection)
+    if (!(obj instanceof ColumnSelection))
     {
-      return hashCode() == obj.hashCode();
+      return false;
     }
-    return false;
+    ColumnSelection that = (ColumnSelection) obj;
+
+    /*
+     * check columns selected are either both null, or match
+     */
+    if (this.selection == null)
+    {
+      if (that.selection != null)
+      {
+        return false;
+      }
+    }
+    if (!this.selection.equals(that.selection))
+    {
+      return false;
+    }
+
+    /*
+     * check hidden columns are either both null, or match
+     */
+    if (this.hiddenColumns == null)
+    {
+      return (that.hiddenColumns == null);
+    }
+    if (that.hiddenColumns == null
+            || that.hiddenColumns.size() != this.hiddenColumns.size())
+    {
+      return false;
+    }
+    int i = 0;
+    for (int[] thisRange : hiddenColumns)
+    {
+      int[] thatRange = that.hiddenColumns.get(i++);
+      if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1])
+      {
+        return false;
+      }
+    }
+    return true;
   }
 
 }
index e1d04eb..1a7ae32 100644 (file)
@@ -495,6 +495,12 @@ public class ColumnSelectionTest
     cs2.addElement(1);
     cs2.addElement(513);
     cs2.addElement(0);
+
+    // with no hidden columns
+    assertFalse(cs.equals(cs2));
+    assertFalse(cs2.equals(cs));
+
+    // with hidden columns added in a different order
     cs2.hideColumns(6, 9);
     cs2.hideColumns(5, 8);
     cs2.hideColumns(3);