okaywit / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

RegularImmutableSortedSet uses unsafeCompare in equals method #1414

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Issue: If I have two ImmutableSortedSets that each contain 1 object (call them 
objects a and b). Objects a and b are both of the same Type (call it Type). 
Type has overridden equals and compareTo methods. The equals method is a deep 
equals and the compareTo method just uses one of the fields to compare. This 
could result in the following behavior:

((a.compareTo(b) == 0) != (a.equals(b)))

This is not advised but is allowed in the javadoc:

"It is strongly recommended, but not strictly required that (x.compareTo(y)==0) 
== (x.equals(y)). Generally speaking, any class that implements the Comparable 
interface and violates this condition should clearly indicate this fact. The 
recommended language is "Note: this class has a natural ordering that is 
inconsistent with equals.""

With this in place two unequal sets can be considered equal because 
RegularImmutableSortedSet uses compareTo instead of equals.

Original issue reported on code.google.com by mcarrier...@gmail.com on 15 May 2013 at 2:17

GoogleCodeExporter commented 9 years ago
I suspect that we tried to mimic the behavior of TreeSet here. Does that appear 
to be the case?

Original comment by cpov...@google.com on 15 May 2013 at 2:19

GoogleCodeExporter commented 9 years ago
Just noticed the javadoc on ImmutableSortedSet, and at least it is documented. 
I will look into whether TreeSet behaves in the same fashion.

Original comment by mcarrier...@gmail.com on 15 May 2013 at 2:24

GoogleCodeExporter commented 9 years ago
You are 100% correct.  TreeSet does indeed follow the same pattern, and I also 
missed the portion of Comparable which deals explicitly with sorted sets. 
Please feel free to close and I appreciate the time.

Original comment by mcarrier...@gmail.com on 15 May 2013 at 2:31

GoogleCodeExporter commented 9 years ago
Phew, glad to hear it :) Thanks for looking into it.

Original comment by cpov...@google.com on 15 May 2013 at 2:37

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:12

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08