From 528191d2feb45cd42c5b4f48420687346705119d Mon Sep 17 00:00:00 2001 From: gmungoc Date: Mon, 30 May 2016 14:46:13 +0100 Subject: [PATCH] JAL-1989 equals method strengthened to fully match selection/hidden cols --- src/jalview/datamodel/ColumnSelection.java | 48 +++++++++++++++++++++-- test/jalview/datamodel/ColumnSelectionTest.java | 6 +++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/jalview/datamodel/ColumnSelection.java b/src/jalview/datamodel/ColumnSelection.java index 74c58b7..aaf70b8 100644 --- a/src/jalview/datamodel/ColumnSelection.java +++ b/src/jalview/datamodel/ColumnSelection.java @@ -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; } } diff --git a/test/jalview/datamodel/ColumnSelectionTest.java b/test/jalview/datamodel/ColumnSelectionTest.java index e1d04eb..1a7ae32 100644 --- a/test/jalview/datamodel/ColumnSelectionTest.java +++ b/test/jalview/datamodel/ColumnSelectionTest.java @@ -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); -- 1.7.10.2