Store key arguments in an ImmutableSet 79/29179/2
authorRobert Varga <rovarga@cisco.com>
Tue, 3 Nov 2015 11:48:01 +0000 (12:48 +0100)
committerGerrit Code Review <gerrit@opendaylight.org>
Wed, 4 Nov 2015 10:19:59 +0000 (10:19 +0000)
Make sure we copy key statements from groupings efficiently by reusing
SchemaNodeIdentifiers which have not changed. Also store them in an
ImmutableSet, rather than a HashSet.

Change-Id: I979e417d722bef54e395459403394bdaccdcc339
Signed-off-by: Robert Varga <rovarga@cisco.com>
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/KeyStatementImpl.java

index f16937565aed6a584a6be3bac6857914ec0300e7..186ef6bca737b3a74db6f86fc7c07bd517c1d32c 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;
@@ -165,12 +165,20 @@ public final class StmtContextUtils {
             final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> keyStmtCtx,
             final QNameModule newQNameModule) {
 
-        Set<SchemaNodeIdentifier> newKeys = new LinkedHashSet<>();
-        for (String keyToken : LIST_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 = 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();
     }
 }
index 8a967f6209dceaf6c6c46799871e5510ebc136d7..128e3b6c76c9bd986846bbc21ccfdf21242742c4 100644 (file)
@@ -38,7 +38,7 @@ public class KeyStatementImpl extends AbstractDeclaredStatement<Collection<Schem
 
         @Override
         public Collection<SchemaNodeIdentifier> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
-            final Builder<SchemaNodeIdentifier> builder = ImmutableSet.<SchemaNodeIdentifier>builder();
+            final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
             int tokens = 0;
             for (String keyToken : StmtContextUtils.LIST_KEY_SPLITTER.split(value)) {
                 builder.add(SchemaNodeIdentifier.create(false, Utils.qNameFromArgument(ctx, keyToken)));