Remost StatementContextBase.schemaPath() 08/94008/2
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 2 Dec 2020 13:39:02 +0000 (14:39 +0100)
committerRobert Varga <nite@hq.sk>
Wed, 2 Dec 2020 15:02:17 +0000 (15:02 +0000)
We are still suporting this mechanics, hence move it to
ReactorStmtCtx, allowing it to be reused without StatementContextBase.

JIRA: YANGTOOLS-1184
Change-Id: Ife7fd573e14415e78fd45663a40c904d702a05b1
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/ReactorStmtCtx.java
yang/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java

index 760f7c35cf7c98eebbd530e199fe6c6f9cd9e00f..a602aebce7bfdd01b5748c023d0b0db3e9960f58 100644 (file)
@@ -20,17 +20,24 @@ import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.YangVersion;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
+import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ConfigEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.DeviationStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.RefineStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
+import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
 import org.opendaylight.yangtools.yang.parser.spi.source.SupportedFeaturesNamespace;
@@ -134,6 +141,11 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
     // FIXME: move this out once we have JDK15+
     private boolean fullyDefined;
 
+    // SchemaPath cache for use with SubstatementContext and InferredStatementContext. This hurts RootStatementContext
+    // a bit in terms of size -- but those are only a few and SchemaPath is on its way out anyway.
+    @Deprecated
+    private volatile SchemaPath schemaPath;
+
     ReactorStmtCtx() {
         // Empty on purpose
     }
@@ -430,6 +442,65 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
         fullyDefined = true;
     }
 
+    //
+    //
+    // Common SchemaPath cache. All of this is bound to be removed once YANGTOOLS-1066 is done.
+    //
+    //
+
+    abstract @NonNull Optional<SchemaPath> schemaPath();
+
+    // Exists only to support {SubstatementContext,InferredStatementContext}.schemaPath()
+    @Deprecated
+    final @NonNull Optional<SchemaPath> substatementGetSchemaPath() {
+        SchemaPath local = schemaPath;
+        if (local == null) {
+            synchronized (this) {
+                local = schemaPath;
+                if (local == null) {
+                    schemaPath = local = createSchemaPath((StatementContextBase<?, ?, ?>) coerceParentContext());
+                }
+            }
+        }
+
+        return Optional.ofNullable(local);
+    }
+
+    @Deprecated
+    private SchemaPath createSchemaPath(final StatementContextBase<?, ?, ?> parent) {
+        final Optional<SchemaPath> maybeParentPath = parent.schemaPath();
+        verify(maybeParentPath.isPresent(), "Parent %s does not have a SchemaPath", parent);
+        final SchemaPath parentPath = maybeParentPath.get();
+
+        if (StmtContextUtils.isUnknownStatement(this)) {
+            return parentPath.createChild(publicDefinition().getStatementName());
+        }
+        final Object argument = argument();
+        if (argument instanceof QName) {
+            final QName qname = (QName) argument;
+            if (producesDeclared(UsesStatement.class)) {
+                return maybeParentPath.orElse(null);
+            }
+
+            return parentPath.createChild(qname);
+        }
+        if (argument instanceof String) {
+            // FIXME: This may yield illegal argument exceptions
+            final Optional<StmtContext<A, D, E>> originalCtx = getOriginalCtx();
+            final QName qname = StmtContextUtils.qnameFromArgument(originalCtx.orElse(this), (String) argument);
+            return parentPath.createChild(qname);
+        }
+        if (argument instanceof SchemaNodeIdentifier
+                && (producesDeclared(AugmentStatement.class) || producesDeclared(RefineStatement.class)
+                        || producesDeclared(DeviationStatement.class))) {
+
+            return parentPath.createChild(((SchemaNodeIdentifier) argument).getNodeIdentifiers());
+        }
+
+        // FIXME: this does not look right
+        return maybeParentPath.orElse(null);
+    }
+
     //
     //
     // Reference counting mechanics start. Please keep these methods in one block for clarity. Note this does not
index 1fe9c071de09077ba55285ad06a21aa1a0bf8fd3..aa0b2702142dbd0f6efc381208c53fb61f5c0025 100644 (file)
@@ -31,18 +31,11 @@ import java.util.Optional;
 import java.util.stream.Stream;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
-import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
-import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.DeviationStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.RefineStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
-import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyHistory;
 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ImplicitParentAwareStatementSupport;
@@ -54,7 +47,6 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.StatementNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport.CopyPolicy;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
 import org.opendaylight.yangtools.yang.parser.spi.source.ImplicitSubstatement;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 import org.opendaylight.yangtools.yang.parser.stmt.reactor.NamespaceBehaviourWithListeners.KeyedValueAddedListener;
@@ -113,10 +105,6 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
 
     private @Nullable ModelProcessingPhase completedPhase;
 
-    // SchemaPath cache for use with SubstatementContext and InferredStatementContext. This hurts RootStatementContext
-    // a bit in terms of size -- but those are only a few and SchemaPath is on its way out anyway.
-    private volatile SchemaPath schemaPath;
-
     // Copy constructor used by subclasses to implement reparent()
     StatementContextBase(final StatementContextBase<A, D, E> original) {
         super(original);
@@ -737,57 +725,4 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
      * @return True if {@link #allSubstatements()} and {@link #allSubstatementsStream()} would return an empty stream.
      */
     abstract boolean hasEmptySubstatements();
-
-    abstract @NonNull Optional<SchemaPath> schemaPath();
-
-    // Exists only to support {SubstatementContext,InferredStatementContext}.schemaPath()
-    @Deprecated
-    final @NonNull Optional<SchemaPath> substatementGetSchemaPath() {
-        SchemaPath local = schemaPath;
-        if (local == null) {
-            synchronized (this) {
-                local = schemaPath;
-                if (local == null) {
-                    schemaPath = local = createSchemaPath((StatementContextBase<?, ?, ?>) coerceParentContext());
-                }
-            }
-        }
-
-        return Optional.ofNullable(local);
-    }
-
-    @Deprecated
-    private SchemaPath createSchemaPath(final StatementContextBase<?, ?, ?> parent) {
-        final Optional<SchemaPath> maybeParentPath = parent.schemaPath();
-        verify(maybeParentPath.isPresent(), "Parent %s does not have a SchemaPath", parent);
-        final SchemaPath parentPath = maybeParentPath.get();
-
-        if (StmtContextUtils.isUnknownStatement(this)) {
-            return parentPath.createChild(publicDefinition().getStatementName());
-        }
-        final Object argument = argument();
-        if (argument instanceof QName) {
-            final QName qname = (QName) argument;
-            if (producesDeclared(UsesStatement.class)) {
-                return maybeParentPath.orElse(null);
-            }
-
-            return parentPath.createChild(qname);
-        }
-        if (argument instanceof String) {
-            // FIXME: This may yield illegal argument exceptions
-            final Optional<StmtContext<A, D, E>> originalCtx = getOriginalCtx();
-            final QName qname = StmtContextUtils.qnameFromArgument(originalCtx.orElse(this), (String) argument);
-            return parentPath.createChild(qname);
-        }
-        if (argument instanceof SchemaNodeIdentifier
-                && (producesDeclared(AugmentStatement.class) || producesDeclared(RefineStatement.class)
-                        || producesDeclared(DeviationStatement.class))) {
-
-            return parentPath.createChild(((SchemaNodeIdentifier) argument).getNodeIdentifiers());
-        }
-
-        // FIXME: this does not look right
-        return maybeParentPath.orElse(null);
-    }
 }