From what i know, Every equals object must have same hash code. However what if in equals method have multiple if that need to be followed ?
Location is an object, Junction is an object, length is an integer, offset is an integer, section is an object.
I already solved the one when atAJunction method, the hashcode i only used junction as the additional hashcode. And when Section is equal, its section's length is equal to the both location's offset. The hashcode that i used for this is only using section as the hashcode.
The main problem is when they have different section, but same offset and endPoint. its equal but the hashcode is different.
Is there anyone can help with my problem ? Thanks Before. :)
This is my Equals method :
@Override
public boolean equals(Object object) {
if(object == null){
return false;
}
if(!(object instanceof Location)){
return false;
}else{
Location otherLocation = (Location) object;
if(atAJunction() && otherLocation.atAJunction()){
return this.endPoint.getJunction().equals(otherLocation.getEndPoint().getJunction());
}else{
// The Problem Here
if(this.endPoint.equals(otherLocation.endPoint)){
return this.offset == otherLocation.getOffset();
}else{
return this.section.equals(otherLocation.getSection()) &&
this.section.getLength() == (this.offset + otherLocation.getOffset());
}
}
}
}
And this is my Hash Code :
@Override
public int hashCode() {
// creates a polynomial hash-code based on the fields of the class.
final int prime = 13; // an odd base prime
int result = 1; // the hash code under construction
if(atAJunction()){
result = prime * result + this.endPoint.getJunction().hashCode();
}else{
result = prime * result + this.section.hashCode();
}
return result;
}