Reputation: 2121
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?
Upvotes: 5
Views: 4438
Reputation: 12692
I think the enum approach would make sense from an object-oriented point of view, but it's dangerous. It could break the equals()
and hashCode()
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 same Set
would cause problems.
If you want different equivalence relationships, you should keep those out of your class. Equivalence
lets you do just that: you extract the equivalence logic (equals() / hashCode()) by implementing Equivalence and overriding the doHash()
and doEquivalent()
methods.
Then, when you want to use a Collection
based on one equivalence or the other, you use Equivalence.wrap()
. For example, you could emulate an IdentityHashSet
by doing:
Set<Equivalence.Wrapper<String>> identityHashSet = Sets.newHashSet();
String a1 = "a";
String a2 = new String("a");
String b = "b";
identityHashSet.add(Equivalences.identity().wrap(a1));
identityHashSet.add(Equivalences.identity().wrap(a2));
identityHashSet.add(Equivalences.identity().wrap(a3));
// identityHashSet contains "a", "a", and "b"
// a standard HashSet would only contain "a" and "b"
// while a1 and a2 are equal according to String.equals(), they are different instances.
Of course, you could use ForwardingSet
to automate the wrapping / unwrapping of your elements.
There is more info in this Guava issue.
Upvotes: 8