Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparison Method violates its general contract in Java 7

Tags:

I am getting a "Comparison Method violates its general contract" after compiling some Java code in Java 7, and then running it.

I have read Comparison method violates its general contract! Java 7 only and realize that there is something wrong with my code that was ignored in previous versions of Java. However I cannot work out what is wrong with my code. The Collections.sort() generates the error.

My code is:

   public Comparator sortBySmoothDays() {
    Comparator c = new Comparator() {
        public int compare(Object arg0, Object arg1) {
            Date date0 = ((PosObject)arg0).getDate();
            Date date1 = ((PosObject)arg1).getDate();

            double d1 = MyUtils.calcSmoothDays(date0, new Date());
            double d2 = MyUtils.calcSmoothDays(date1, new Date());
            if (d1 >= d2) {
                return 1;
            }
            else {
                return -1;
            }   
        }
    };
    return c;
}


Comparator c = ComparatorUtils.getInstance().sortBySmoothDays();
Collections.sort(posList, c);

Can anyone help? Thank you!

like image 860
gordon613 Avatar asked Feb 28 '12 17:02

gordon613


2 Answers

A comparator must return 0 if the values are equal. In your current implementation, you return 1 if they are equal. The easiest way to compare your double values correctly is to call Double.compare:

double d1 = MyUtils.calcSmoothDays(date0, new Date());
double d2 = MyUtils.calcSmoothDays(date1, new Date());

return Double.compare(d1, d2);
like image 192
Kevin Crowell Avatar answered Oct 04 '22 21:10

Kevin Crowell


With your comparator, every object compares greater than itself: compare(x,x) always returns one.

This violates the following requirement:

The implementor must ensure that sgn(compare(x, y)) == -sgn(compare(y, x)) for all x and y.

The above requirement implies that compare(x,x) must return zero.

I would recommend reading the contract and making sure your implementation fulfils it.

In particular, if date0.equals(date1), the comparator probably ought to return zero right away, without doing any floating-point conversions and comparisons.

like image 25
NPE Avatar answered Oct 04 '22 21:10

NPE