Correct QName.compareTo() 53/96553/2
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 16 Jun 2021 15:39:28 +0000 (17:39 +0200)
committerRobert Varga <nite@hq.sk>
Tue, 31 Aug 2021 22:45:33 +0000 (22:45 +0000)
Sorting on localName first is counter-intuitive and tends to spread
related names (from the same module/revision) across the domain. Sort by
module/revision first, which keeps related information closely together.

JIRA: YANGTOOLS-1298
Change-Id: Ia57418f915498a0713549195b86413988b5208ce
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/QName.java

index 2c19fa9f0295263ad9a6f85f7eed1a7c7ce47e80..60072a1b5955967a49c2106e38eea812802ca7e1 100644 (file)
@@ -371,17 +371,11 @@ public final class QName extends AbstractQName implements Comparable<QName> {
         return getLocalName().equals(other.getLocalName()) && Objects.equals(getNamespace(), other.getNamespace());
     }
 
-    // FIXME: this comparison function looks odd. We are sorting first by local name and then by module? What is
-    //        the impact on iteration order of SortedMap<QName, ?>?
     @Override
     @SuppressWarnings("checkstyle:parameterName")
     public int compareTo(final QName o) {
-        // compare mandatory localName parameter
-        int result = getLocalName().compareTo(o.getLocalName());
-        if (result != 0) {
-            return result;
-        }
-        return module.compareTo(o.module);
+        final int result = module.compareTo(o.module);
+        return result != 0 ? result : getLocalName().compareTo(o.getLocalName());
     }
 
     @Override