Make sure we compare key members via their property name
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / IdentifiableItemCodec.java
index 63d60bb3770acf85cdd7160bb425a8804c10d43b..8e06d5ae548cb5d98d513a7900cc3734a9390889 100644 (file)
@@ -14,11 +14,12 @@ import java.lang.invoke.MethodHandle;
 import java.lang.invoke.MethodHandles;
 import java.lang.reflect.Constructor;
 import java.util.ArrayList;
-import java.util.Collections;
+import java.util.Comparator;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
 import org.opendaylight.yangtools.concepts.Codec;
 import org.opendaylight.yangtools.yang.binding.Identifier;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem;
@@ -42,7 +43,7 @@ final class IdentifiableItemCodec implements Codec<NodeIdentifierWithPredicates,
         try {
             ctor = MethodHandles.publicLookup().unreflectConstructor(getConstructor(keyClass));
         } catch (IllegalAccessException e) {
-            throw new IllegalArgumentException("Missing construct in class " + keyClass);
+            throw new IllegalArgumentException("Missing constructor in class " + keyClass, e);
         }
         final MethodHandle inv = MethodHandles.spreadInvoker(ctor.type(), 0);
         this.ctorInvoker = inv.asType(inv.type().changeReturnType(Identifier.class));
@@ -64,7 +65,7 @@ final class IdentifiableItemCodec implements Codec<NodeIdentifierWithPredicates,
          *
          * We do not have to perform a sort if the source collection has less than two
          * elements.
-
+         *
          * We always perform an ImmutableList.copyOf(), as that will turn into a no-op
          * if the source is already immutable. It will also produce optimized implementations
          * for empty and singleton collections.
@@ -75,7 +76,8 @@ final class IdentifiableItemCodec implements Codec<NodeIdentifierWithPredicates,
         final List<QName> sortedKeys;
         if (unsortedKeys.size() > 1) {
             final List<QName> tmp = new ArrayList<>(unsortedKeys);
-            Collections.sort(tmp, (q1, q2) -> q1.getLocalName().compareToIgnoreCase(q2.getLocalName()));
+            // This is not terribly efficient but gets the job done
+            tmp.sort(Comparator.comparing(qname -> BindingMapping.getPropertyName(qname.getLocalName())));
             sortedKeys = tmp;
         } else {
             sortedKeys = unsortedKeys;
@@ -104,7 +106,7 @@ final class IdentifiableItemCodec implements Codec<NodeIdentifierWithPredicates,
         }
 
         @SuppressWarnings({ "rawtypes", "unchecked" })
-        final IdentifiableItem identifiableItem = new IdentifiableItem(identifiable, identifier);
+        final IdentifiableItem identifiableItem = IdentifiableItem.of((Class) identifiable, (Identifier) identifier);
         return identifiableItem;
     }