From: gmungoc Date: Wed, 29 Apr 2015 08:13:08 +0000 (+0100) Subject: JAL-1686 ViewStyle.hashCode implemented X-Git-Tag: Jalview_2_9~42 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=be7d3d5e0d85c7c8dcfdf35a6240da9c438ef9bb;p=jalview.git JAL-1686 ViewStyle.hashCode implemented --- diff --git a/src/jalview/viewmodel/styles/ViewStyle.java b/src/jalview/viewmodel/styles/ViewStyle.java index d6e529c..3ed5634 100644 --- a/src/jalview/viewmodel/styles/ViewStyle.java +++ b/src/jalview/viewmodel/styles/ViewStyle.java @@ -270,11 +270,44 @@ public class ViewStyle implements ViewStyleI // return equivalent(this, (ViewStyle) other); } + /** + * Overridden to ensure that whenever vs1.equals(vs2) then vs1.hashCode() == + * vs2.hashCode() + */ @Override public int hashCode() { - return 0; // TODO + /* + * No need to include all properties, just a selection... + */ + int hash = 0; + int m = 1; + // Boolean.hashCode returns 1231 or 1237 + hash += m++ * Boolean.valueOf(this.abovePIDThreshold).hashCode(); + hash += m++ * Boolean.valueOf(this.centreColumnLabels).hashCode(); + hash += m++ * Boolean.valueOf(this.colourAppliesToAllGroups).hashCode(); + hash += m++ * Boolean.valueOf(this.displayReferenceSeq).hashCode(); + hash += m++ * Boolean.valueOf(this.renderGaps).hashCode(); + hash += m++ * Boolean.valueOf(this.rightAlignIds).hashCode(); + hash += m++ * Boolean.valueOf(this.scaleProteinAsCdna).hashCode(); + hash += m++ * Boolean.valueOf(this.scaleRightWrapped).hashCode(); + hash += m++ * Boolean.valueOf(this.seqNameItalics).hashCode(); + hash += m++ * Boolean.valueOf(this.showAnnotation).hashCode(); + hash += m++ * Boolean.valueOf(this.showBoxes).hashCode(); + hash += m++ * Boolean.valueOf(this.showdbrefs).hashCode(); + hash += m++ * Boolean.valueOf(this.showJVSuffix).hashCode(); + hash += m++ * Boolean.valueOf(this.showSequenceFeatures).hashCode(); + hash += m++ * Boolean.valueOf(this.showUnconserved).hashCode(); + hash += m++ * Boolean.valueOf(this.wrapAlignment).hashCode(); + hash += m++ * this.charHeight; + hash += m++ * this.charWidth; + hash += m++ * fontSize; + hash += m++ * fontStyle; + hash += m++ * idWidth; + hash += String.valueOf(this.fontName).hashCode(); + return hash; } + /** * @return the upperCasebold */ diff --git a/test/jalview/viewmodel/styles/ViewStyleTest.java b/test/jalview/viewmodel/styles/ViewStyleTest.java index 9a0820f..32d048f 100644 --- a/test/jalview/viewmodel/styles/ViewStyleTest.java +++ b/test/jalview/viewmodel/styles/ViewStyleTest.java @@ -51,6 +51,7 @@ public class ViewStyleTest + value2 + ") - not set in copy constructor?"; assertEquals(msg, value1, value2); } + assertEquals("Hashcode not equals", vs1.hashCode(), vs2.hashCode()); } /** @@ -64,7 +65,6 @@ public class ViewStyleTest throws IllegalAccessException { Class type = field.getType(); - final int numValue = 1 + r.nextInt(100); if (type.equals(boolean.class) || type.equals(Boolean.class)) { @@ -167,6 +167,12 @@ public class ViewStyleTest changeValue(vs2, field); assertFalse("equals method ignores " + field.getName(), vs1.equals(vs2)); + + if (vs1.hashCode() == vs2.hashCode()) + { + // uncomment next line to see which fields hashCode ignores + // System.out.println("hashCode ignores " + field.getName()); + } // restore original value before testing the next field field.set(vs2, oldValue); }