`CartesianSet.equals` always returns false for other `Set` types
Marcono1234 opened this issue · comments
Version
32.1.3-jre
Description
The internal class CartesianSet
returned by Sets.cartesianProduct
implements equals
in a way that it is never equal to any other Set
type because it calls super.equals
which is actually Object.equals
(and therefore checks for object identity).
That does not seem intended.
Example
Object o1 = ImmutableSet.of(ImmutableList.of(1, 2));
Object o2 = Sets.cartesianProduct(ImmutableList.of(
ImmutableSet.of(1), ImmutableSet.of(2)
));
System.out.println("o1.equals(o2): " + o1.equals(o2));
System.out.println("o2.equals(o1): " + o2.equals(o1));
That definitely looks like a violation of the contract of Set.equals
. It's been like that for 15 years, so probably it hasn't mattered in practice? Still, I think we could have fallback logic for this case. It could just check that the other set has the right size and that the CartesianSet
contains all of the other set's elements.
As far as I can see, CartesianSet.hashCode()
does follow the contract for Set.hashCode()
.
Actually, not quite 15 years, but 11, since this commit changed the parent type from AbstractSet
to ForwardingCollection
.