wenwenlin / jsr-305

Automatically exported from code.google.com/p/jsr-305
0 stars 0 forks source link

(compareTo() == 0) != equals() #18

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
BigDecimal d = new BigDecimal("4.0");
BigDecimal e = new BigDecimal("4.00");

System.out.println(d.compareTo(e) == 0); //true
System.out.println(d.equals(e)); //false

From the Comparable.compareTo documentation:
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." 

We need an annotation that clearly denotes that the result is inconsistent 
with equals.  I recommend @InconsistentWithEquals.

Original issue reported on code.google.com by karlgsch...@gmail.com on 14 Aug 2009 at 7:16

GoogleCodeExporter commented 9 years ago
To explain further since some collections do not properly handle Comparables 
that 
are inconsistent with equals, tools can issues warnings in those cases.  Such 
as:
SortedSet<BigDecimal> set = new TreeSet<BigDecimal>();
should causes a warning if the tool is configured for that check.

Original comment by karlgsch...@gmail.com on 14 Aug 2009 at 7:23