BUG-994: optimize resolveRelativeXPath() 36/8336/4
authorRobert Varga <rovarga@cisco.com>
Wed, 25 Jun 2014 13:41:12 +0000 (15:41 +0200)
committerRobert Varga <rovarga@cisco.com>
Thu, 26 Jun 2014 09:46:40 +0000 (11:46 +0200)
This patch is a slight optimization of the method which takes advantage
of the fact that we do not need to return a List anymore.

Change-Id: I05ed7b96feca26247a6d73dfe2e758846155302f
Signed-off-by: Robert Varga <rovarga@cisco.com>
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaContextUtil.java

index f48bfde63e7e420b54004cce9b1ce19fb7f7e273..2af7159ffed81dd8d70cabaf205648acbefc2b0d 100644 (file)
@@ -7,8 +7,10 @@
  */
 package org.opendaylight.yangtools.yang.model.util;
 
+import com.google.common.base.Function;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Splitter;
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Iterables;
 
 import java.net.URI;
@@ -498,7 +500,7 @@ public final class SchemaContextUtil {
         Preconditions.checkArgument(m != null, "Failed to find module for node {} in context {}", node, ctx);
 
         for (final UsesNode u : m.getUses()) {
-            final SchemaNode targetGrouping = findNodeInSchemaContext(ctx, u.getGroupingPath().getPath());
+            final SchemaNode targetGrouping = findNodeInSchemaContext(ctx, u.getGroupingPath().getPathFromRoot());
             Preconditions.checkArgument(targetGrouping instanceof GroupingDefinition,
                     "Failed to generate code for augment in %s", u);
 
@@ -625,7 +627,7 @@ public final class SchemaContextUtil {
     }
 
     private static DataSchemaNode getResultFromUses(final UsesNode u, final String currentName, final SchemaContext ctx) {
-        SchemaNode targetGrouping = findNodeInSchemaContext(ctx, u.getGroupingPath().getPath());
+        SchemaNode targetGrouping = findNodeInSchemaContext(ctx, u.getGroupingPath().getPathFromRoot());
 
         Preconditions.checkArgument(targetGrouping instanceof GroupingDefinition,
                 "Failed to generate code for augment in %s", u);
@@ -867,16 +869,14 @@ public final class SchemaContextUtil {
             ++colCount;
         }
 
-        final List<QName> absolutePath = new LinkedList<QName>();
-        final List<QName> path = leafrefParentNode.getPath().getPath();
-        if (path != null) {
-            int lenght = path.size() - colCount;
-            absolutePath.addAll(path.subList(0, lenght));
-            for (String s : Iterables.skip(xpaths, colCount)) {
-                absolutePath.add(stringPathPartToQName(context, module, s));
-            }
-        }
-
-        return absolutePath;
+        final ImmutableList<QName> relative = ImmutableList.copyOf(
+                Iterables.transform(Iterables.skip(xpaths, colCount), new Function<String, QName>() {
+                    @Override
+                    public QName apply(final String input) {
+                        return stringPathPartToQName(context, module, input);
+                    }
+                }));
+        final Iterable<QName> parent = leafrefParentNode.getPath().getPathFromRoot();
+        return Iterables.concat(Iterables.limit(parent, Iterables.size(parent) - colCount), relative);
     }
 }