Merge "BUG-944: optimize SchemaContextUtil"
authorTony Tkacik <ttkacik@cisco.com>
Thu, 26 Jun 2014 13:36:28 +0000 (13:36 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 26 Jun 2014 13:36:28 +0000 (13:36 +0000)
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/RevisionAwareXPath.java
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaContextUtil.java

index f2d911a7dadfa42931e6bb78317eabbe57509f9c..62a349b99b5a2ed2b6d42bd09ce37fc7e11cfa2d 100644 (file)
@@ -8,20 +8,17 @@
 package org.opendaylight.yangtools.yang.model.api;
 
 /**
- * 
  * Contains methods for getting data (concrete XPath) and metadata (is XPath
  * absolute) from XPath instance.
- * 
- * 
  */
 public interface RevisionAwareXPath {
 
     /**
      * Returns <code>true</code> if the XPapth starts in root of Yang model,
-     * otherwise returns <code>false</cdoe>.
-     * 
+     * otherwise returns <code>false</code>.
+     *
      * @return <code>true</code> if the XPapth starts in root of Yang model,
-     *         otherwise returns <code>false</cdoe>
+     *         otherwise returns <code>false</code>
      */
     boolean isAbsolute();
 
@@ -29,8 +26,9 @@ public interface RevisionAwareXPath {
      * Returns the XPath formatted string as is defined in model. <br>
      * For example:
      * /prefix:container/prefix:container::cond[when()=foo]/prefix:leaf
-     * 
+     *
      * @return the XPath formatted string as is defined in model.
      */
+    @Override
     String toString();
 }
index 9bca912cb6d0f2842ac70d0521ccfe98ed069a77..53c1604b7c42d5d9c4efcfcc36a4034281d7bff6 100644 (file)
@@ -8,14 +8,15 @@
 package org.opendaylight.yangtools.yang.model.util;
 
 import com.google.common.base.Preconditions;
+import com.google.common.base.Splitter;
 import com.google.common.collect.Iterables;
 
 import java.net.URI;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
+import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
@@ -51,6 +52,8 @@ import org.slf4j.LoggerFactory;
  */
 public final class SchemaContextUtil {
     private static final Logger LOG = LoggerFactory.getLogger(SchemaContextUtil.class);
+    private static final Splitter COLON_SPLITTER = Splitter.on(':');
+    private static final Splitter SLASH_SPLITTER = Splitter.on('/');
 
     private SchemaContextUtil() {
     }
@@ -740,8 +743,7 @@ public final class SchemaContextUtil {
         Preconditions.checkArgument(xpath != null, "XPath string reference cannot be NULL");
 
         List<QName> path = new LinkedList<QName>();
-        String[] prefixedPath = xpath.split("/");
-        for (String pathComponent : prefixedPath) {
+        for (String pathComponent : SLASH_SPLITTER.split(xpath)) {
             if (!pathComponent.isEmpty()) {
                 path.add(stringPathPartToQName(context, parentModule, pathComponent));
             }
@@ -775,14 +777,18 @@ public final class SchemaContextUtil {
         Preconditions.checkArgument(parentModule != null, "Parent Module reference cannot be NULL");
         Preconditions.checkArgument(prefixedPathPart != null, "Prefixed Path Part cannot be NULL!");
 
-        if (prefixedPathPart.contains(":")) {
-            String[] prefixedName = prefixedPathPart.split(":");
-            Module module = resolveModuleForPrefix(context, parentModule, prefixedName[0]);
+        if (prefixedPathPart.indexOf(':') != -1) {
+            final Iterator<String> prefixedName = COLON_SPLITTER.split(prefixedPathPart).iterator();
+            final String modulePrefix = prefixedName.next();
+
+            Module module = resolveModuleForPrefix(context, parentModule, modulePrefix);
             Preconditions.checkArgument(module != null, "Failed to resolve xpath: no module found for prefix %s in module %s",
-                    prefixedName[0], parentModule.getName());
-            return new QName(module.getNamespace(), module.getRevision(), prefixedName[1]);
+                    modulePrefix, parentModule.getName());
+
+            // FIXME: Module should have a QNameModule handle
+            return QName.create(module.getNamespace(), module.getRevision(), prefixedName.next());
         } else {
-            return new QName(parentModule.getNamespace(), parentModule.getRevision(), prefixedPathPart);
+            return QName.create(parentModule.getNamespace(), parentModule.getRevision(), prefixedPathPart);
         }
     }
 
@@ -852,25 +858,23 @@ public final class SchemaContextUtil {
         Preconditions.checkState(leafrefParentNode.getPath() != null,
                 "Schema Path reference for Leafref cannot be NULL");
 
-        List<QName> absolutePath = new LinkedList<QName>();
-        String strXPath = relativeXPath.toString();
-        String[] xpaths = strXPath.split("/");
+        final Iterable<String> xpaths = SLASH_SPLITTER.split(relativeXPath.toString());
 
+        // Find out how many "parent" components there are
+        // FIXME: is .contains() the right check here?
         int colCount = 0;
-        while (xpaths[colCount].contains("..")) {
-            colCount = colCount + 1;
+        for (Iterator<String> it = xpaths.iterator(); it.hasNext() && it.next().contains(".."); ) {
+            ++colCount;
         }
+
+        final List<QName> absolutePath = new LinkedList<QName>();
         List<QName> path = leafrefParentNode.getPath().getPath();
         if (path != null) {
             int lenght = path.size() - colCount;
             absolutePath.addAll(path.subList(0, lenght));
-            List<String> xpathsList = Arrays.asList(xpaths);
-            List<String> sublistedXPath = xpathsList.subList(colCount, xpaths.length);
-            List<QName> sublist = new ArrayList<>();
-            for (String pathPart : sublistedXPath) {
-                sublist.add(stringPathPartToQName(context, module, pathPart));
+            for (String s : Iterables.skip(xpaths, colCount)) {
+                absolutePath.add(stringPathPartToQName(context, module, s));
             }
-            absolutePath.addAll(sublist);
         }
 
         return absolutePath;