Introduce SchemaNodeIdentifier.asSchemaPath() 23/27523/3
authorRobert Varga <rovarga@cisco.com>
Mon, 28 Sep 2015 09:33:26 +0000 (11:33 +0200)
committerRobert Varga <rovarga@cisco.com>
Mon, 28 Sep 2015 10:08:59 +0000 (12:08 +0200)
Converting SchemaNodeIdentifier to a SchemaPath needs to be efficient,
reuse the fact the two classes are organized in the same way.

Change-Id: Ifa18f4378b66a94aa9f8e9d45fd274be9a517005
Signed-off-by: Robert Varga <rovarga@cisco.com>
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/stmt/SchemaNodeIdentifier.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/Utils.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/AugmentEffectiveStatementImpl.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/DeviationEffectiveStatementImpl.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/UsesEffectiveStatementImpl.java

index e4d9779741fdd8a43a6c19b6d47a44bf14fb0916..7613aa1da42d0d0a10cb3e17e35a9daad7819906 100644 (file)
@@ -12,19 +12,18 @@ import com.google.common.base.MoreObjects.ToStringHelper;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Iterables;
+import com.google.common.collect.UnmodifiableIterator;
 import java.util.Arrays;
 import java.util.Iterator;
-import java.util.List;
 import java.util.NoSuchElementException;
 import java.util.Objects;
 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
 import org.opendaylight.yangtools.concepts.Immutable;
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 
 /**
- * Represents unique path to the every schema node inside the schema node identifier
- * namespace.
- *
+ * Represents unique path to the every schema node inside the schema node identifier namespace.
  */
 public abstract class SchemaNodeIdentifier implements Immutable {
 
@@ -43,7 +42,7 @@ public abstract class SchemaNodeIdentifier implements Immutable {
 
         @Override
         protected SchemaNodeIdentifier createInstance(final SchemaNodeIdentifier parent, final QName qname) {
-            return new Absolute(parent, qname);
+            return new Absolute(parent, Preconditions.checkNotNull(qname));
         }
     }
 
@@ -62,14 +61,15 @@ public abstract class SchemaNodeIdentifier implements Immutable {
 
         @Override
         protected SchemaNodeIdentifier createInstance(final SchemaNodeIdentifier parent, final QName qname) {
-            return new Relative(parent, qname);
+            return new Relative(parent, Preconditions.checkNotNull(qname));
         }
     }
 
     @SuppressWarnings("rawtypes")
     private static final AtomicReferenceFieldUpdater<SchemaNodeIdentifier, ImmutableList> LEGACYPATH_UPDATER =
             AtomicReferenceFieldUpdater.newUpdater(SchemaNodeIdentifier.class, ImmutableList.class, "legacyPath");
-
+    private static final AtomicReferenceFieldUpdater<SchemaNodeIdentifier, SchemaPath> SCHEMAPATH_UPDATER =
+            AtomicReferenceFieldUpdater.newUpdater(SchemaNodeIdentifier.class, SchemaPath.class, "schemaPath");
     /**
      * Shared instance of the conceptual root schema node.
      */
@@ -101,6 +101,11 @@ public abstract class SchemaNodeIdentifier implements Immutable {
      */
     private volatile ImmutableList<QName> legacyPath;
 
+    /**
+     * Cached SchemaPath.
+     */
+    private volatile SchemaPath schemaPath;
+
     protected SchemaNodeIdentifier(final SchemaNodeIdentifier parent, final QName qname) {
         this.parent = parent;
         this.qname = qname;
@@ -123,19 +128,6 @@ public abstract class SchemaNodeIdentifier implements Immutable {
         return ret;
     }
 
-    /**
-     * Returns the complete path to schema node.
-     *
-     * @return list of <code>QName</code> instances which represents complete
-     *         path to schema node
-     *
-     * @deprecated Use {@link #getPathFromRoot()} instead.
-     */
-    @Deprecated
-    public List<QName> getPath() {
-        return getLegacyPath();
-    }
-
     /**
      * Constructs new instance of this class with the concrete path.
      *
@@ -248,7 +240,7 @@ public abstract class SchemaNodeIdentifier implements Immutable {
         return new Iterable<QName>() {
             @Override
             public Iterator<QName> iterator() {
-                return new Iterator<QName>() {
+                return new UnmodifiableIterator<QName>() {
                     private SchemaNodeIdentifier current = SchemaNodeIdentifier.this;
 
                     @Override
@@ -266,11 +258,6 @@ public abstract class SchemaNodeIdentifier implements Immutable {
                             throw new NoSuchElementException("No more elements available");
                         }
                     }
-
-                    @Override
-                    public void remove() {
-                        throw new UnsupportedOperationException("Component removal not supported");
-                    }
                 };
             }
         };
@@ -294,6 +281,28 @@ public abstract class SchemaNodeIdentifier implements Immutable {
         return qname;
     }
 
+    private SchemaPath createSchemaPath() {
+        final SchemaPath newPath;
+        if (parent == null) {
+            final SchemaPath parentPath = isAbsolute() ? SchemaPath.ROOT : SchemaPath.SAME;
+            newPath = qname == null ? parentPath : parentPath.createChild(qname);
+        } else {
+            newPath = parent.asSchemaPath().createChild(qname);
+        }
+
+        return SCHEMAPATH_UPDATER.compareAndSet(this, null, newPath) ? newPath : schemaPath;
+    }
+
+    /**
+     * Create the {@link SchemaPath} equivalent of this identifier.
+     *
+     * @return SchemaPath equivalent.
+     */
+    public final SchemaPath asSchemaPath() {
+        final SchemaPath ret = schemaPath;
+        return ret != null ? ret : createSchemaPath();
+    }
+
     /**
      * Describes whether schema node identifier is|isn't absolute.
      *
index 7c2a21e6a6a82044b02e850ef693997a38801fb5..d8ed70f09b480237f4eda92d73d3f123bedd4a42 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
 
 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
 import com.google.common.base.CharMatcher;
+import com.google.common.base.Preconditions;
 import com.google.common.base.Splitter;
 import com.google.common.collect.Iterables;
 import java.util.ArrayList;
@@ -406,9 +407,8 @@ public final class Utils {
 
     private static void addQNamesFromSchemaNodeIdentifierToList(final List<QName> qNamesFromRoot,
             final SchemaNodeIdentifier augmentTargetPath) {
-        Iterator<QName> augmentTargetPathIterator = augmentTargetPath.getPathFromRoot().iterator();
-        while (augmentTargetPathIterator.hasNext()) {
-            qNamesFromRoot.add(augmentTargetPathIterator.next());
+        for (QName qname : augmentTargetPath.getPathFromRoot()) {
+            qNamesFromRoot.add(qname);
         }
     }
 
@@ -417,9 +417,8 @@ public final class Utils {
         // Yang constants should be lowercase so we have throw if value does not
         // suit this
         String deviateUpper = deviate.toUpperCase();
-        if (Objects.equals(deviate, deviateUpper)) {
-            throw new IllegalArgumentException(String.format("String %s is not valid deviate argument", deviate));
-        }
+        Preconditions.checkArgument(!Objects.equals(deviate, deviateUpper),
+            "String %s is not valid deviate argument", deviate);
 
         // but Java enum is uppercase so we cannot use lowercase here
         try {
@@ -449,10 +448,6 @@ public final class Utils {
         return status;
     }
 
-    public static SchemaPath SchemaNodeIdentifierToSchemaPath(final SchemaNodeIdentifier identifier) {
-        return SchemaPath.create(identifier.getPathFromRoot(), identifier.isAbsolute());
-    }
-
     public static Date getLatestRevision(final RootStatementContext<?, ?, ?> root) {
         return getLatestRevision(root.declaredSubstatements());
     }
index 365c461fdb5aa843cdfb44579a25c4fecb462a8e..366c6acca1ce6662344c77acd61e34f8534d268c 100644 (file)
@@ -32,8 +32,7 @@ import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
 
 public class AugmentEffectiveStatementImpl
-        extends
-        AbstractEffectiveDocumentedDataNodeContainer<SchemaNodeIdentifier, AugmentStatement>
+        extends AbstractEffectiveDocumentedDataNodeContainer<SchemaNodeIdentifier, AugmentStatement>
         implements AugmentationSchema, NamespaceRevisionAware, Comparable<AugmentEffectiveStatementImpl> {
     private final SchemaPath targetPath;
     private final URI namespace;
@@ -47,11 +46,7 @@ public class AugmentEffectiveStatementImpl
             final StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
         super(ctx);
 
-        SchemaNodeIdentifier schemaNodeIdentifier = ctx.getStatementArgument();
-        this.targetPath = SchemaPath.create(
-                schemaNodeIdentifier.getPathFromRoot(),
-                schemaNodeIdentifier.isAbsolute());
-
+        this.targetPath = ctx.getStatementArgument().asSchemaPath();
         QNameModule rootModuleQName = Utils.getRootModuleQName(ctx);
         this.namespace = rootModuleQName.getNamespace();
         this.revision = rootModuleQName.getRevision();
@@ -161,8 +156,7 @@ public class AugmentEffectiveStatementImpl
 
     @Override
     public String toString() {
-        StringBuilder sb = new StringBuilder(
-                AugmentEffectiveStatementImpl.class.getSimpleName());
+        StringBuilder sb = new StringBuilder(AugmentEffectiveStatementImpl.class.getSimpleName());
         sb.append("[");
         sb.append("targetPath=").append(targetPath);
         sb.append(", when=").append(whenCondition);
index 2b439a13e91f9a0830c031fd24c551f1c585cbf6..3b3a9ed9486d2d6ec6167007ea8f36a5bdf1690c 100644 (file)
@@ -33,8 +33,7 @@ public class DeviationEffectiveStatementImpl extends EffectiveStatementBase<Sche
 
         List<UnknownSchemaNode> unknownSchemaNodesInit = new LinkedList<>();
 
-        targetPath = SchemaPath.create(ctx.getStatementArgument().getPathFromRoot(), ctx.getStatementArgument()
-                .isAbsolute());
+        targetPath = ctx.getStatementArgument().asSchemaPath();
 
         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
             if (effectiveStatement instanceof DeviateEffectiveStatementImpl) {
index 0a95b20312507feb955dba9eb0f4e57cd13f8db0..f506bd091ccd0f24999f3c8e747f40024c276efe 100644 (file)
@@ -73,7 +73,7 @@ public class UsesEffectiveStatementImpl extends EffectiveStatementBase<QName, Us
             if (effectiveStatement instanceof RefineEffectiveStatementImpl) {
                 RefineEffectiveStatementImpl refineStmt = (RefineEffectiveStatementImpl) effectiveStatement;
                 SchemaNodeIdentifier identifier = refineStmt.argument();
-                refinesInit.put(Utils.SchemaNodeIdentifierToSchemaPath(identifier), refineStmt.getRefineTargetNode());
+                refinesInit.put(identifier.asSchemaPath(), refineStmt.getRefineTargetNode());
             }
         }