Skip to content

Commit

Permalink
Let IntelliJ fix equals() and hashCode() for classes with problems
Browse files Browse the repository at this point in the history
  • Loading branch information
jmthibault79 committed Nov 9, 2018
1 parent 393f2c0 commit d27129e
Show file tree
Hide file tree
Showing 20 changed files with 313 additions and 196 deletions.
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
29 changes: 17 additions & 12 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,23 @@ public int compareTo(@SuppressWarnings("NullableProblems") final Version o) {
return build - o.build;
}

/**
* Check if another version is exactly the same as this one.
*
* @param obj another version object
* @return true if both versions are the same, false otherwise.
*/
public boolean compatibleWith(final Version version) {
return compareTo(version) >= 0;
}

@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,12 +28,12 @@
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;
private int[] bitLengths;
private int[] values;
private final ByteBuffer buf = ByteBuffer.allocate(1024 * 10);

public CanonicalHuffmanIntegerEncoding() {
}
Expand All @@ -45,6 +45,7 @@ public EncodingID id() {

@Override
public byte[] toByteArray() {
final ByteBuffer buf = ByteBuffer.allocate(1024 * 10);
buf.clear();
ITF8.writeUnsignedITF8(values.length, buf);
for (final int value : values)
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,24 @@ 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,24 @@ 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,22 @@ 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,22 @@ 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,19 @@ 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,24 @@ 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;
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

0 comments on commit d27129e

Please sign in to comment.