Skip to content

Commit

Permalink
Add EqualsHashCode checkstyle rule
Browse files Browse the repository at this point in the history
This rule ensures classes that override equals() also override hashCode()
  • Loading branch information
lutovich committed May 9, 2016
1 parent 0243c24 commit 20ef116
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 731 deletions.
1 change: 1 addition & 0 deletions build/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<module name="MissingOverride"/>
<module name="EmptyStatement"/>
<module name="SuperFinalize"/>
<module name="EqualsHashCode"/>

</module>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ protected final Object clone() {
return this;
}

@Override
public int hashCode() {
return 1;
}

/**
* A Null object is equal to the null value and to itself.
* @param object An object to test for nullness.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.junit.Test;

import java.nio.ByteBuffer;
import java.util.Objects;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isOneOf;
Expand Down Expand Up @@ -50,17 +51,30 @@ class Obj
@Override
public boolean equals( Object o )
{
if ( this == o )
{
return true;
}
if ( o == null || getClass() != o.getClass() )
{
return false;
}

Obj obj = (Obj) o;

return aByte == obj.aByte && aShort == obj.aShort && Float.compare( obj.aFloat, aFloat ) == 0 &&
aChar == obj.aChar && anInt == obj.anInt && aLong == obj.aLong &&
return aBoolean == obj.aBoolean &&
aByte == obj.aByte &&
aShort == obj.aShort &&
Float.compare( obj.aFloat, aFloat ) == 0 &&
aChar == obj.aChar &&
anInt == obj.anInt &&
aLong == obj.aLong &&
Double.compare( obj.aDouble, aDouble ) == 0 &&
!(object != null ? !object.equals( obj.object ) : obj.object != null);
Objects.equals( object, obj.object );
}

@Override
public int hashCode()
{
return Objects.hash( aBoolean, aByte, aShort, aFloat, aChar, anInt, aLong, aDouble, object );
}
}

Expand Down

0 comments on commit 20ef116

Please sign in to comment.