Uploaded image for project: 'Sling'
  1. Sling
  2. SLING-2975

MapEntry#compareTo method is not consistent and might throw ISE on Java7

    XMLWordPrintableJSON

Details

    Description

      On Java7 sorting the map entries might result in an IllegalStateException:
      java.lang.IllegalArgumentException: Comparison method violates its general
      contract!
      at java.util.ComparableTimSort.mergeLo(ComparableTimSort.java:714)
      at java.util.ComparableTimSort.mergeAt(ComparableTimSort.java:451)
      at java.util.ComparableTimSort.mergeCollapse(ComparableTimSort.java:376)
      at java.util.ComparableTimSort.sort(ComparableTimSort.java:182)
      at java.util.ComparableTimSort.sort(ComparableTimSort.java:146)
      at java.util.Arrays.sort(Arrays.java:472)
      at java.util.Collections.sort(Collections.java:155)

      The behaviour of sorting has changed with Java7 (see http://stackoverflow.com/questions/7849539/comparison-method-violates-its-general-contract-java-7-only) and it now throws such an exception if the compare method is not implemented in an consistent way.

      In our case: if the two strings have the same lengths, always 1 is returned, so compareTo("a", "b") returns 1 while compareTo("b", "a") returns 1 as well

      Attachments

        Activity

          People

            cziegeler Carsten Ziegeler
            cziegeler Carsten Ziegeler
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: