I am writing an app that has an "Achievements" system, similar to Stack Overflow's badges. I am also using SugarORM to store the progress of the achievements. This is the Achievement
class:
public class Achievement extends SugarRecord<Achievement>{
@StringRes
public int shortNameId;
@StringRes
public int descriptionId;
public int progressNow;
public int fullProgress;
//I am afraid that Sugar ORM will ignore private fields
//so I made them public. Please tell me if Sugar ORM does
//not ignore those.
}
Now I want to override equals
. And that is easy enough.
@Override
public boolean equals (Object other) {
if (other == null) return false;
if (!(other instanceof Achievement)) return false;
Achievement a = (Achievement)other;
return a.shortNameId == shortNameId &&
a.descriptionId == descriptionId &&
a.fullProgress == fullProgress;
}
Then I remembered that I should always override hashCode
if I override equals
. According to Effective Java by Joshua Bloch, I wrote the hashCode
method like this:
@Override
public int hashCode () {
int result = 17;
result = result * 31 + shortNameId;
result = result * 31 + descriptionId;
result = result * 31 + fullProgress;
return result;
}
Then I though that I should change the implementation of equals
to
@Override
public boolean equals (Object other) {
if (other == null) return false;
if (!(other instanceof Achievement)) return false;
Achievement a = (Achievement)other;
return a.hashCode() == hashCode ();
}
After that, I thought that if I overrode hashCode
wrongly, equals
would not work either. However, The above implementation seems "right" to me, because I think hash codes are what makes objects equal.
P.S. Don't tell me it's personal preference. I think there must be a standard for this, right? And also I am very willing to follow standards.