I want to be able to switch between two equals-Implementations, but I’m not sure if the Equivalence class of Google Guava maybe provides this functionality. Let’s say I have two equals methods equalsContent() and equalsKeys() or something like that I somehow want to delegate the equals method to one of the two private methods (and the same for the two hashCode methods).
Well, I’m somehow not sure what the usage of the Equivalence abstract class and the Equivalences class (static methods) is.
Besides, how would you implement the desired properties described above? I could use another method which simply sets a flag or an enum to the value and implement the two equals and hash methods inside an enum with two abstract methods (equals(), hashCode()) and simply call enum.equals() or enum.hashCode() in the equals() and hashCode() method. What do you think?
I think the enum approach would make sense from an object-oriented point of view, but it’s dangerous. It could break the
equals()andhashCode()contract (reflexivity, symmetry, and transitivity). For example, inserting an instance that uses the first equivalence strategy and an instance that uses the second equivalence strategy in the sameSetwould cause problems.If you want different equivalence relationships, you should keep those out of your class.
Equivalencelets you do just that: you extract the equivalence logic (equals() / hashCode()) by implementing Equivalence and overriding thedoHash()anddoEquivalent()methods.Then, when you want to use a
Collectionbased on one equivalence or the other, you useEquivalence.wrap(). For example, you could emulate anIdentityHashSetby doing:Of course, you could use
ForwardingSetto automate the wrapping / unwrapping of your elements.There is more info in this Guava issue.