+
+ /*
+ * When instantiating binding objects we need to specify constructor arguments
+ * in alphabetic order. We play a couple of tricks here to optimize CPU/memory
+ * trade-offs.
+ *
+ * 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.
+ *
+ * BUG-2755: remove this if order is made declaration-order-dependent
+ */
+ final List<QName> unsortedKeys = schema.getKeyDefinition();
+ final List<QName> sortedKeys;
+ if (unsortedKeys.size() > 1) {
+ final List<QName> tmp = new ArrayList<>(unsortedKeys);
+ Collections.sort(tmp, KEYARG_COMPARATOR);
+ sortedKeys = tmp;
+ } else {
+ sortedKeys = unsortedKeys;
+ }
+
+ this.keysInBindingOrder = ImmutableList.copyOf(sortedKeys);