I have a set of immutable objects that implement equals() that strictly compare the member variables they hold. Two objects are identical if the member variables are equal. hashCodes() are consistent with this.
I want to be able to aggregate these objects according to looser definitions of similarity. I was thinking of using comparator for this so that I could define a bunch of ad hoc similarity rules, but the javadoc states that comparator should be consistent with equals(). Is is OK to break the comparator contract with equals() to achieve this, or is there some better method/pattern/strategy for aggregating objects according to some similarity rules?
Examples may include:
- Locations:
equals()returns true if LatLng and place name exactly equal, but comparator returns 0 if LatLng within say 25m/50m/100m regardless of place name etc, or if only place names are equal regardless of LatLng. - Dates:
equals()returns true if long millis are equal, but comparator returns 0 if on same day/month/year etc.. - Strings:
equals()return true ifequalsIgnoreCase()is true, but comparator may removes spaces/special characters to reduce to some canonical form then runequalsetc.