JAL-1989 equals method strengthened to fully match selection/hidden cols
[jalview.git] / src / jalview / datamodel / ColumnSelection.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;
   }
 
 }