Skip to content
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

Merged
merged 5 commits into from
Nov 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions src/main/java/htsjdk/samtools/LinearIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package htsjdk.samtools;

import java.util.Arrays;
import java.util.Objects;

/**
* The linear index associated with a given reference in a BAM index.
Expand Down Expand Up @@ -103,23 +104,19 @@ public int getIndexStart() {
}

@Override
public boolean equals(final Object o) {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

final LinearIndex that = (LinearIndex) o;

if (mIndexStart != that.mIndexStart) return false;
if (mReferenceSequence != that.mReferenceSequence) return false;
if (!Arrays.equals(mIndexEntries, that.mIndexEntries)) return false;

return true;
LinearIndex that = (LinearIndex) o;
return mReferenceSequence == that.mReferenceSequence &&
mIndexStart == that.mIndexStart &&
Arrays.equals(mIndexEntries, that.mIndexEntries);
}

@Override
public int hashCode() {
int result = mReferenceSequence;
result = 31 * result + mIndexStart;

int result = Objects.hash(mReferenceSequence, mIndexStart);
result = 31 * result + Arrays.hashCode(mIndexEntries);
return result;
}
Expand Down
24 changes: 17 additions & 7 deletions src/main/java/htsjdk/samtools/cram/common/Version.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package htsjdk.samtools.cram.common;

import java.util.Objects;

/**
* A class to represent a version information, 3 number: major, minor and build number.
*/
Expand Down Expand Up @@ -44,20 +46,28 @@ public int compareTo(@SuppressWarnings("NullableProblems") final Version o) {
return build - o.build;
}

public boolean compatibleWith(final Version version) {
return compareTo(version) >= 0;
}

/**
* Check if another version is exactly the same as this one.
*
* @param obj another version object
* @param o another version object
* @return true if both versions are the same, false otherwise.
*/
@Override
public boolean equals(final Object obj) {
if (obj == null || !(obj instanceof Version)) return false;
final Version version = (Version) obj;
return major == version.major && minor == version.minor;
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Version version = (Version) o;
return major == version.major &&
minor == version.minor &&
build == version.build;
}

public boolean compatibleWith(final Version version) {
return compareTo(version) >= 0;
@Override
public int hashCode() {
return Objects.hash(major, minor, build);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

public class CanonicalHuffmanIntegerEncoding implements Encoding<Integer> {
private static final EncodingID ENCODING_ID = EncodingID.HUFFMAN;
Expand Down Expand Up @@ -89,12 +90,19 @@ public static EncodingParams toParam(final int[] bfValues, final int[] bfBitLens
}

@Override
public boolean equals(final Object obj) {
if (obj instanceof CanonicalHuffmanIntegerEncoding) {
final CanonicalHuffmanIntegerEncoding foe = (CanonicalHuffmanIntegerEncoding) obj;
return Arrays.equals(bitLengths, foe.bitLengths) && Arrays.equals(values, foe.values);
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CanonicalHuffmanIntegerEncoding that = (CanonicalHuffmanIntegerEncoding) o;
return Arrays.equals(bitLengths, that.bitLengths) &&
Arrays.equals(values, that.values);
}

@Override
public int hashCode() {

}
return false;
int result = Arrays.hashCode(bitLengths);
result = 31 * result + Arrays.hashCode(values);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package htsjdk.samtools.cram.encoding.readfeatures;

import java.io.Serializable;
import java.util.Objects;

/**
* A read feature representing a single quality score in a read.
Expand Down Expand Up @@ -57,21 +58,23 @@ public void setQualityScore(final byte qualityScore) {
this.qualityScore = qualityScore;
}

@Override
public boolean equals(final Object obj) {
if (!(obj instanceof BaseQualityScore))
return false;

final BaseQualityScore v = (BaseQualityScore) obj;

return position == v.position && qualityScore == v.qualityScore;

}

@Override
public String toString() {
return new StringBuilder().append((char) operator).append('@')
.append(position).append('#').appendCodePoint(qualityScore).toString();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BaseQualityScore that = (BaseQualityScore) o;
return position == that.position &&
qualityScore == that.qualityScore;
}

@Override
public int hashCode() {
return Objects.hash(position, qualityScore);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.io.Serializable;
import java.util.Arrays;
import java.util.Objects;

public class Bases implements Serializable, ReadFeature {

Expand Down Expand Up @@ -59,18 +60,23 @@ public void setPosition(final int position) {
}

@Override
public boolean equals(final Object obj) {
if (!(obj instanceof Bases))
return false;

final Bases bases = (Bases) obj;

return position == bases.position && !Arrays.equals(this.bases, bases.bases);
public String toString() {
return getClass().getSimpleName() + "[" + "position=" + position + "; bases=" + new String(bases) + "] ";
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Bases bases1 = (Bases) o;
return position == bases1.position &&
Arrays.equals(bases, bases1.bases);
}

@Override
public String toString() {
return getClass().getSimpleName() + "[" + "position=" + position + "; bases=" + new String(bases) + "] ";
public int hashCode() {
int result = Objects.hash(position);
result = 31 * result + Arrays.hashCode(bases);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package htsjdk.samtools.cram.encoding.readfeatures;

import java.io.Serializable;
import java.util.Objects;

/**
* A read feature representing a deletion of one or more bases similar to {@link htsjdk.samtools.CigarOperator#D}.
Expand Down Expand Up @@ -60,18 +61,21 @@ public void setLength(final int length) {
}

@Override
public boolean equals(final Object obj) {
if (!(obj instanceof Deletion))
return false;

final Deletion deleteion = (Deletion) obj;

return position == deleteion.position && length == deleteion.length;
public String toString() {
return String.valueOf((char) operator) + '@' + position + '+' + length;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Deletion deletion = (Deletion) o;
return position == deletion.position &&
length == deletion.length;
}

@Override
public String toString() {
return String.valueOf((char) operator) + '@' + position + '+' + length;
public int hashCode() {
return Objects.hash(position, length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package htsjdk.samtools.cram.encoding.readfeatures;

import java.io.Serializable;
import java.util.Objects;

/**
* A read feature representing a hard clip similar to {@link htsjdk.samtools.CigarOperator#H}.
Expand Down Expand Up @@ -60,18 +61,21 @@ public void setLength(final int length) {
}

@Override
public boolean equals(final Object obj) {
if (!(obj instanceof HardClip))
return false;

final HardClip hardClip = (HardClip) obj;

return position == hardClip.position && length == hardClip.length;
public String toString() {
return getClass().getSimpleName() + "[" + "position=" + position + "; length=" + length + "] ";
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
HardClip hardClip = (HardClip) o;
return position == hardClip.position &&
length == hardClip.length;
}

@Override
public String toString() {
return getClass().getSimpleName() + "[" + "position=" + position + "; length=" + length + "] ";
public int hashCode() {
return Objects.hash(position, length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package htsjdk.samtools.cram.encoding.readfeatures;

import java.io.Serializable;
import java.util.Objects;

/**
* A read feature representing a single insert base.
Expand Down Expand Up @@ -52,17 +53,6 @@ public void setPosition(final int position) {
this.position = position;
}

@Override
public boolean equals(final Object obj) {
if (!(obj instanceof InsertBase))
return false;

final InsertBase insertBase = (InsertBase) obj;

return position == insertBase.position && base == insertBase.base;

}

@Override
public String toString() {
return new StringBuilder().append((char) operator).append('@')
Expand All @@ -76,4 +66,18 @@ public byte getBase() {
public void setBase(final byte base) {
this.base = base;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
InsertBase that = (InsertBase) o;
return position == that.position &&
base == that.base;
}

@Override
public int hashCode() {
return Objects.hash(position, base);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.io.Serializable;
import java.util.Arrays;
import java.util.Objects;

/**
* A read feature representing a multi-base insertion.
Expand Down Expand Up @@ -61,17 +62,23 @@ public void setSequence(final byte[] sequence) {
}

@Override
public boolean equals(final Object obj) {
if (!(obj instanceof Insertion))
return false;

final Insertion insertion = (Insertion) obj;
public String toString() {
return getClass().getSimpleName() + "[" + "position=" + position + "; sequence=" + new String(sequence) + "] ";
}

return position == insertion.position && Arrays.equals(sequence, insertion.sequence);
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final Insertion insertion = (Insertion) o;
return position == insertion.position &&
Arrays.equals(sequence, insertion.sequence);
}

@Override
public String toString() {
return getClass().getSimpleName() + "[" + "position=" + position + "; sequence=" + new String(sequence) + "] ";
public int hashCode() {
int result = Objects.hash(position);
result = 31 * result + Arrays.hashCode(sequence);
return result;
}
}
Loading