Hi
I have a persistent object "Model", which contains a set of persistent objects "Feature". "Feature" has a single parameter, "name". In "Feature" I have overridden equals and hashcode to check the "name" value instead. e.g.
Code:
public int hashCode() {
return this.getName().toUpperCase().hashCode();
}
public boolean equals( Object _obj ) {
if ( _obj instanceof Feature ) {
return ( (Feature) _obj ).getName().toUpperCase().equals( this.getName().toUpperCase() );
} else if ( _obj instanceof String ) {
return ( (String) _obj ).toUpperCase().equals( this.getName().toUpperCase() );
}
return false;
}
I want to be able to do a comparison based on a String. e.g.
Code:
Model model;
...
boolean contains = model.getFeatures().contains( "FEATURE_NAME" );
however, this always returns false. Everything I have read about overriding equals and hashcode in hibernate talks about comparing Objects across sessions, whereas I want to compare Objects based on their contents. Is this possible?
I have a solution by putting the Set into an ArrayList, but I had to override ArrayList to reverse the equals check. The default is that contains(String myString) does myString.equals(thisElement) which always failed, instead of thisElement.equals( myString ) which uses my overridden equals and hashCode. This is what I had to reverse by overriding indexOf in the ArrayList, which is EXTREMELY messy.
I'm sure that there must be a better way to do this, but I can't figure it out.
Please help