Instantiate a QName cache
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / StmtContextUtils.java
index 59863bdd7cd93c24d319f83283be3aa2c0fe07e6..d7e15ddc70ef114ef1574c442722dadd162616db 100644 (file)
@@ -10,9 +10,9 @@ package org.opendaylight.yangtools.yang.parser.spi.meta;
 import com.google.common.base.Function;
 import com.google.common.base.Predicate;
 import com.google.common.base.Splitter;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.ImmutableSet.Builder;
 import java.util.Collection;
-import java.util.LinkedHashSet;
-import java.util.Set;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
@@ -22,9 +22,7 @@ import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.UnknownStatementImpl;
 
 public final class StmtContextUtils {
-
-    public static final char LIST_KEY_SEPARATOR = ' ';
-    private static final Splitter KEY_SPLITTER = Splitter.on(LIST_KEY_SEPARATOR).omitEmptyStrings().trimResults();
+    public static final Splitter LIST_KEY_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults();
 
     private static final Function<StmtContext<?, ?,?>, DeclaredStatement<?>> BUILD_DECLARED =
             new Function<StmtContext<?,?,?>, DeclaredStatement<?>>() {
@@ -167,12 +165,21 @@ public final class StmtContextUtils {
             final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> keyStmtCtx,
             final QNameModule newQNameModule) {
 
-        Set<SchemaNodeIdentifier> newKeys = new LinkedHashSet<>();
-        for (String keyToken : KEY_SPLITTER.split(keyStmtCtx.rawStatementArgument())) {
-            final QName keyQName = QName.create(newQNameModule, keyToken);
-            newKeys.add(SchemaNodeIdentifier.create(false, keyQName));
+        final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
+        boolean replaced = false;
+        for (SchemaNodeIdentifier arg : keyStmtCtx.getStatementArgument()) {
+            final QName qname = arg.getLastComponent();
+            if (!newQNameModule.equals(qname)) {
+                final QName newQname = keyStmtCtx.getFromNamespace(QNameCacheNamespace.class,
+                    QName.create(newQNameModule, qname.getLocalName()));
+                builder.add(SchemaNodeIdentifier.create(false, newQname));
+                replaced = true;
+            } else {
+                builder.add(arg);
+            }
         }
 
-        return newKeys;
+        // This makes sure we reuse the collection when a grouping is instantiated in the same module
+        return replaced ? builder.build() : keyStmtCtx.getStatementArgument();
     }
 }