-
Notifications
You must be signed in to change notification settings - Fork 245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add hashCode() to classes with equals() and clean up equals() #1222
Conversation
|
@@ -65,8 +66,12 @@ public boolean equals(final Object obj) { | |||
|
|||
final Bases bases = (Bases) obj; | |||
|
|||
return position == bases.position && !Arrays.equals(this.bases, bases.bases); | |||
return position == bases.position && Arrays.equals(this.bases, bases.bases); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yikes
@@ -64,10 +65,14 @@ public boolean equals(final Object obj) { | |||
if (!(obj instanceof Deletion)) | |||
return false; | |||
|
|||
final Deletion deleteion = (Deletion) obj; | |||
final Deletion other = (Deletion) obj; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
spelling
Codecov Report
@@ Coverage Diff @@
## master #1222 +/- ##
==============================================
- Coverage 69.016% 68.975% -0.04%
- Complexity 8066 8072 +6
==============================================
Files 539 539
Lines 32552 32587 +35
Branches 5488 5510 +22
==============================================
+ Hits 22466 22477 +11
- Misses 7890 7903 +13
- Partials 2196 2207 +11
|
@@ -125,6 +126,15 @@ public boolean equals(final Object obj) { | |||
|
|||
} | |||
|
|||
@Override | |||
public int hashCode() { | |||
// Note: flags covers these checks as well: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll probably revisit this when I refactor CramCompressionRecord
in earnest
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jmthibault79 Thanks for doing this. I think there are still a bunch of problems here due to the annoying fact that Array doesn't override Object.hashCode.
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(position, bases); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this use the memory location of bases instead of the base values?
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(bitLengths, values); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this just uses the default hashcode, not equivalent to Arrays.hashCode().
@@ -52,11 +54,16 @@ public int compareTo(@SuppressWarnings("NullableProblems") final Version o) { | |||
*/ | |||
@Override | |||
public boolean equals(final Object obj) { | |||
if (obj == null || !(obj instanceof Version)) return false; | |||
if (obj == null || (obj.getClass() != getClass())) return false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like this isn't consistent with the compareTo method since that takes build into account as well. You don't have to make it so but you should add a comment if you don't.
|
||
final Insertion insertion = (Insertion) obj; | ||
|
||
return position == insertion.position && Arrays.equals(sequence, insertion.sequence); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(position, sequence); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same problem with Object.equals vs Arrays.equals.
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(position, scores); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same issue
// Note: flags covers these checks as well: | ||
// isNegativeStrand(), isVendorFiltered(), isSegmentUnmapped(), isLastSegment(), isFirstSegment() | ||
|
||
return Objects.hash(alignmentStart, flags, readLength, recordsToNextFragment, mappingQuality, readFeatures, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same problem with arrays
@@ -103,6 +103,10 @@ public boolean equals(final Object obj) { | |||
return Arrays.equals(id, header.id) && getSamFileHeader().equals(header.getSamFileHeader()); | |||
} | |||
|
|||
@Override | |||
public int hashCode() { | |||
return Objects.hash(version, id, samFileHeader); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Be aware that if you update version.hashCode to take build into account that now this will no longer be correct since this doesn't compare with version.equals.
@@ -154,17 +155,15 @@ public int compareTo(@SuppressWarnings("NullableProblems") final ReadTag o) { | |||
|
|||
@Override | |||
public boolean equals(final Object obj) { | |||
if (!(obj instanceof ReadTag)) | |||
return false; | |||
if (obj == null || (obj.getClass() != getClass())) return false; | |||
|
|||
final ReadTag foe = (ReadTag) obj; | |||
return key.equals(foe.key) && (value == null && foe.value == null || value != null && value.equals(foe.value)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This definition and it's hashCode are correctly matching now. There's a problem with the equals method for cases of 'H' and 'B'. 'H' returns an array which should be compared using Arrays.equals() , and 'B' returns a TagValueAndUnsignedArrayFlag
which doesn't implement equals.
Fixing the array case will require a corresponding change to the hashCode.
@@ -135,6 +132,11 @@ public boolean equals(Object other) { | |||
return true; | |||
} | |||
|
|||
@Override |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this really has to hash every element of sequenceEntries.
@@ -116,6 +116,11 @@ public boolean equals(final Object obj) { | |||
return true; | |||
} | |||
|
|||
@Override | |||
public int hashCode() { | |||
return Objects.hash(position, code, referenceBase, base); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this has to be clever about the case where code = NO_CODE and ignore referenceBase and base in that case.
7aa8a4a
to
d27129e
Compare
|
||
public class CanonicalHuffmanIntegerEncoding implements Encoding<Integer> { | ||
private static final EncodingID ENCODING_ID = EncodingID.HUFFMAN; | ||
private int[] bitLengths; | ||
private int[] values; | ||
private final ByteBuffer buf = ByteBuffer.allocate(1024 * 10); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved inside method where it belongs, and it shouldn't be used for equality tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you want to localize this to the method it's used in, because that means re-allocating it every time. That seems like it could be a substantial performance hit through increased garbage collection.
You can just ignore it in equality checks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point - I'll fix that in the general Encoding refactor as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jmthibault79 Looks good except for moving that buffer out of the field.
Restored |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good
Description
equals()
withouthashCode()
is bad news. Also, someequals()
have problems too.Checklist