Add SchemaPath/SchemaNodeIdentifier.createChild(QName) 23/65423/6
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 11 Nov 2017 00:51:01 +0000 (01:51 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 13 Nov 2017 10:39:43 +0000 (11:39 +0100)
We can use a much shorter dispatch when we do not have to deal with
arrays/collections and have a single QName only. Introduce this
variant, which is widely used in YANG parser.

Introduce these methods and adjust some users of alternative methods
to take advantage of the new method.

Change-Id: If1d6288a6607b31bb45e9fd78c0319fb403e1528
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/SchemaPath.java
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/stmt/SchemaNodeIdentifier.java
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/AbstractBaseType.java
yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/BitsTypeTest.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/key/KeyStatementSupport.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/Utils.java

index b8b280bdfe6a9d4dfe29b57847140cc728013093..fb335dbd3f18575ddb9e3baf7aef512f2bffd0f6 100644 (file)
@@ -44,8 +44,8 @@ public abstract class SchemaPath implements Immutable {
         }
 
         @Override
-        protected SchemaPath createInstance(final SchemaPath parent, final QName qname) {
-            return new AbsoluteSchemaPath(parent, requireNonNull(qname));
+        public AbsoluteSchemaPath createChild(final QName qname) {
+            return new AbsoluteSchemaPath(this, requireNonNull(qname));
         }
     }
 
@@ -63,8 +63,8 @@ public abstract class SchemaPath implements Immutable {
         }
 
         @Override
-        protected SchemaPath createInstance(final SchemaPath parent, final QName qname) {
-            return new RelativeSchemaPath(parent, requireNonNull(qname));
+        public RelativeSchemaPath createChild(final QName qname) {
+            return new RelativeSchemaPath(this, requireNonNull(qname));
         }
     }
 
@@ -175,15 +175,6 @@ public abstract class SchemaPath implements Immutable {
         return create(Arrays.asList(path), absolute);
     }
 
-    /**
-     * Create a new instance.
-     *
-     * @param parent Parent SchemaPath
-     * @param qname next path element
-     * @return A new SchemaPath instance
-     */
-    protected abstract SchemaPath createInstance(SchemaPath parent, QName qname);
-
     /**
      * Create a child path based on concatenation of this path and a relative path.
      *
@@ -197,7 +188,7 @@ public abstract class SchemaPath implements Immutable {
 
         SchemaPath parentPath = this;
         for (QName qname : relative) {
-            parentPath = parentPath.createInstance(parentPath, qname);
+            parentPath = parentPath.createChild(qname);
         }
 
         return parentPath;
@@ -211,15 +202,17 @@ public abstract class SchemaPath implements Immutable {
      */
     public SchemaPath createChild(final SchemaPath relative) {
         checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
-
-        SchemaPath parentPath = this;
-        for (QName qname : relative.getPathFromRoot()) {
-            parentPath = parentPath.createInstance(parentPath, qname);
-        }
-
-        return parentPath;
+        return createChild(relative.getPathFromRoot());
     }
 
+    /**
+     * Create a child path based on concatenation of this path and an additional path element.
+     *
+     * @param element Relative SchemaPath elements
+     * @return A new child path
+     */
+    public abstract SchemaPath createChild(QName element);
+
     /**
      * Create a child path based on concatenation of this path and additional
      * path elements.
index 8c01fd69a1ed9dc215b1b6fa81d234f8aa3068f1..764fbee19cfe2bda9939e750529cb39b10f3ff0a 100644 (file)
@@ -42,8 +42,8 @@ public abstract class SchemaNodeIdentifier implements Immutable {
         }
 
         @Override
-        protected SchemaNodeIdentifier createInstance(final SchemaNodeIdentifier parent, final QName qname) {
-            return new Absolute(parent, requireNonNull(qname));
+        public Absolute createChild(final QName element) {
+            return new Absolute(this, requireNonNull(element));
         }
     }
 
@@ -61,8 +61,8 @@ public abstract class SchemaNodeIdentifier implements Immutable {
         }
 
         @Override
-        protected SchemaNodeIdentifier createInstance(final SchemaNodeIdentifier parent, final QName qname) {
-            return new Relative(parent, requireNonNull(qname));
+        public Relative createChild(final QName element) {
+            return new Relative(this, requireNonNull(element));
         }
     }
 
@@ -74,12 +74,12 @@ public abstract class SchemaNodeIdentifier implements Immutable {
     /**
      * Shared instance of the conceptual root schema node.
      */
-    public static final SchemaNodeIdentifier ROOT = new Absolute(null, null);
+    public static final Absolute ROOT = new Absolute(null, null);
 
     /**
      * Shared instance of the "same" relative schema node.
      */
-    public static final SchemaNodeIdentifier SAME = new Relative(null, null);
+    public static final Relative SAME = new Relative(null, null);
 
     /**
      * Parent path.
@@ -107,7 +107,7 @@ public abstract class SchemaNodeIdentifier implements Immutable {
      */
     private volatile SchemaPath schemaPath;
 
-    protected SchemaNodeIdentifier(final SchemaNodeIdentifier parent, final QName qname) {
+    SchemaNodeIdentifier(final SchemaNodeIdentifier parent, final QName qname) {
         this.parent = parent;
         this.qname = qname;
 
@@ -162,15 +162,6 @@ public abstract class SchemaNodeIdentifier implements Immutable {
         return create(Arrays.asList(path), absolute);
     }
 
-    /**
-     * Create a new instance.
-     *
-     * @param parent Parent schema node identifier
-     * @param qname next path element
-     * @return A new SchemaPath instance
-     */
-    protected abstract SchemaNodeIdentifier createInstance(SchemaNodeIdentifier parent, QName qname);
-
     /**
      * Create a child path based on concatenation of this path and a relative path.
      *
@@ -184,7 +175,7 @@ public abstract class SchemaNodeIdentifier implements Immutable {
 
         SchemaNodeIdentifier parentNode = this;
         for (QName qname : relative) {
-            parentNode = parentNode.createInstance(parentNode, qname);
+            parentNode = parentNode.createChild(qname);
         }
 
         return parentNode;
@@ -198,15 +189,17 @@ public abstract class SchemaNodeIdentifier implements Immutable {
      */
     public SchemaNodeIdentifier createChild(final SchemaNodeIdentifier relative) {
         checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
-
-        SchemaNodeIdentifier parentNode = this;
-        for (QName qname : relative.getPathFromRoot()) {
-            parentNode = parentNode.createInstance(parentNode, qname);
-        }
-
-        return parentNode;
+        return createChild(relative.getPathFromRoot());
     }
 
+    /**
+     * Create a child path based on concatenation of this path and an additional path element.
+     *
+     * @param element Next SchemaPath element
+     * @return A new child path
+     */
+    public abstract SchemaNodeIdentifier createChild(QName element);
+
     /**
      * Create a child path based on concatenation of this path and additional
      * path elements.
@@ -313,7 +306,7 @@ public abstract class SchemaNodeIdentifier implements Immutable {
     }
 
     @Override
-    public boolean equals(final Object obj) {
+    public final boolean equals(final Object obj) {
         if (this == obj) {
             return true;
         }
index 8080b325746a43b040be8b9cc7953d8784b40c1f..7b205a877ec6b62c4a1acb3bd00a5476b6c12d0d 100644 (file)
@@ -19,7 +19,7 @@ import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
 
 abstract class AbstractBaseType<T extends TypeDefinition<T>> extends AbstractTypeDefinition<T> {
     AbstractBaseType(final QName qname) {
-        this(SchemaPath.create(true, qname), ImmutableList.of());
+        this(SchemaPath.ROOT.createChild(qname), ImmutableList.of());
     }
 
     AbstractBaseType(final SchemaPath path, final List<UnknownSchemaNode> unknownSchemaNodes) {
index ab965b29d13036dd8ffa5e6322927bd02c588abc..4714fb2b8c513f69e0fb42418333a4fe022e80d2 100644 (file)
@@ -35,7 +35,7 @@ public class BitsTypeTest {
         doReturn("test").when(bit).getName();
 
         QName qname = QName.create("namespace", "localname");
-        SchemaPath schemaPath = SchemaPath.create(Collections.singletonList(qname), true);
+        SchemaPath schemaPath = SchemaPath.create(true, qname);
 
         BitsTypeDefinition bitsType = BaseTypes.bitsTypeBuilder(schemaPath).addBit(bit).build();
 
index e35f8a9e07c33f27303badd267e49ac00e911a4f..ff5f2cb71c1970601fbd4ff6f38edfcd543faea4 100644 (file)
@@ -39,7 +39,7 @@ public final class KeyStatementSupport
         final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
         int tokens = 0;
         for (String keyToken : StmtContextUtils.LIST_KEY_SPLITTER.split(value)) {
-            builder.add(SchemaNodeIdentifier.create(false, StmtContextUtils.qnameFromArgument(ctx, keyToken)));
+            builder.add(SchemaNodeIdentifier.SAME.createChild(StmtContextUtils.qnameFromArgument(ctx, keyToken)));
             tokens++;
         }
 
@@ -62,7 +62,7 @@ public final class KeyStatementSupport
             if (!targetModule.equals(qname.getModule())) {
                 final QName newQname = ctx.getFromNamespace(QNameCacheNamespace.class,
                         QName.create(targetModule, qname.getLocalName()));
-                builder.add(SchemaNodeIdentifier.create(false, newQname));
+                builder.add(SchemaNodeIdentifier.SAME.createChild(newQname));
                 replaced = true;
             } else {
                 builder.add(arg);
index a729520b569a8f8dd5c1bde3df940c3354631e12..de75763e0c7af8dfd4ce6216e03cebf16f6859ce 100644 (file)
@@ -33,7 +33,6 @@ import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.YangVersion;
 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
-import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Relative;
 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
@@ -93,12 +92,8 @@ public final class Utils {
         }
 
         final Set<SchemaNodeIdentifier.Relative> keyNodes = new HashSet<>();
-
         for (final String keyToken : keyTokens) {
-
-            final SchemaNodeIdentifier.Relative keyNode = (Relative) SchemaNodeIdentifier.Relative.create(false,
-                    StmtContextUtils.qnameFromArgument(ctx, keyToken));
-            keyNodes.add(keyNode);
+            keyNodes.add(SchemaNodeIdentifier.SAME.createChild(StmtContextUtils.qnameFromArgument(ctx, keyToken)));
         }
 
         return keyNodes;