samedi 25 avril 2015

Unable to replicate : "Comparison method violates its general contract!"


I got the error "Comparison method violates its general contract!" when the code was using the following comparator, however I am unable to replicate the exception using jUnit. I'd like to know what caused this issue and how to replicate it. There are examples of others having the same problem but not how to replicate it.

public class DtoComparator implements Comparator<Dto> {

    @Override
    public int compare(Dto r1, Dto r2) {

        int value = 0;

        value = r1.getOrder() - r2.getOrder();

        if (value == 0 && !isValueNull(r1.getDate(), r2.getDate()))
            value = r1.getDate().compareTo(r2.getDate());

        return value;
    }

    private boolean isValueNull(Date date, Date date2) {
        return date == null || date2 == null;
    }
}

The code is called by using:

Collections.sort(dtos, new DtoComparator());

Thanks for any help.

Extra info: The error seemed to occur in the TimSort class inside Java utils and from within a method called mergeLo. Link: http://ift.tt/1z04JGp


Aucun commentaire:

Enregistrer un commentaire