Add CommonStmtCtx
[yangtools.git] / yang / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / StmtContextUtils.java
index 4a1ef8e113878c7019dc2d9c0d2aaeac458bface..6e59f57d1d65d3638d7619eb4a6a5c32471f2b5e 100644 (file)
@@ -29,14 +29,12 @@ import org.opendaylight.yangtools.yang.model.api.stmt.LeafStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.MinElementsStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.PresenceStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.PresenceEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.UnknownStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.UnrecognizedStatement;
-import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
-import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
+import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Parent;
 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToPrefixToModuleName;
 import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToModuleCtx;
 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
@@ -52,7 +50,7 @@ public final class StmtContextUtils {
     public static <A, D extends DeclaredStatement<A>> A firstAttributeOf(
             final Iterable<? extends StmtContext<?, ?, ?>> contexts, final Class<D> declaredType) {
         for (final StmtContext<?, ?, ?> ctx : contexts) {
-            if (producesDeclared(ctx, declaredType)) {
+            if (ctx.producesDeclared(declaredType)) {
                 return (A) ctx.getStatementArgument();
             }
         }
@@ -62,7 +60,7 @@ public final class StmtContextUtils {
     @SuppressWarnings("unchecked")
     public static <A, D extends DeclaredStatement<A>> A firstAttributeOf(final StmtContext<?, ?, ?> ctx,
             final Class<D> declaredType) {
-        return producesDeclared(ctx, declaredType) ? (A) ctx.getStatementArgument() : null;
+        return ctx.producesDeclared(declaredType) ? (A) ctx.getStatementArgument() : null;
     }
 
     public static <A, D extends DeclaredStatement<A>> A firstSubstatementAttributeOf(
@@ -74,7 +72,7 @@ public final class StmtContextUtils {
     public static <A, D extends DeclaredStatement<A>> StmtContext<A, ?, ?> findFirstDeclaredSubstatement(
             final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType) {
         for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
-            if (producesDeclared(subStmtContext, declaredType)) {
+            if (subStmtContext.producesDeclared(declaredType)) {
                 return (StmtContext<A, ?, ?>) subStmtContext;
             }
         }
@@ -82,6 +80,7 @@ public final class StmtContextUtils {
     }
 
     @SafeVarargs
+    @SuppressWarnings({ "rawtypes", "unchecked" })
     public static StmtContext<?, ?, ?> findFirstDeclaredSubstatement(final StmtContext<?, ?, ?> stmtContext,
             int startIndex, final Class<? extends DeclaredStatement<?>>... types) {
         if (startIndex >= types.length) {
@@ -89,7 +88,7 @@ public final class StmtContextUtils {
         }
 
         for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
-            if (producesDeclared(subStmtContext, types[startIndex])) {
+            if (subStmtContext.producesDeclared((Class) types[startIndex])) {
                 return startIndex + 1 == types.length ? subStmtContext : findFirstDeclaredSubstatement(subStmtContext,
                         ++startIndex, types);
             }
@@ -102,7 +101,7 @@ public final class StmtContextUtils {
             final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType) {
         final ImmutableList.Builder<StmtContext<A, D, ?>> listBuilder = ImmutableList.builder();
         for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
-            if (producesDeclared(subStmtContext, declaredType)) {
+            if (subStmtContext.producesDeclared(declaredType)) {
                 listBuilder.add((StmtContext<A, D, ?>) subStmtContext);
             }
         }
@@ -114,7 +113,7 @@ public final class StmtContextUtils {
             final StmtContext<?, ?, ?> stmtContext, final Class<D> type) {
         final ImmutableList.Builder<StmtContext<A, D, ?>> listBuilder = ImmutableList.builder();
         for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.effectiveSubstatements()) {
-            if (producesDeclared(subStmtContext, type)) {
+            if (subStmtContext.producesDeclared(type)) {
                 listBuilder.add((StmtContext<A, D, ?>) subStmtContext);
             }
         }
@@ -133,7 +132,7 @@ public final class StmtContextUtils {
     public static <A, D extends DeclaredStatement<A>> StmtContext<A, ?, ?> findFirstEffectiveSubstatement(
             final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType) {
         for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.effectiveSubstatements()) {
-            if (producesDeclared(subStmtContext, declaredType)) {
+            if (subStmtContext.producesDeclared(declaredType)) {
                 return (StmtContext<A, ?, ?>) subStmtContext;
             }
         }
@@ -151,7 +150,9 @@ public final class StmtContextUtils {
      * @param <A> statement argument type
      * @param <D> declared statement type
      * @return statement context that was searched for or null if was not found
+     * @deprecated Use {@link StmtContext#findSubstatementArgument(Class)} instead.
      */
+    @Deprecated(forRemoval = true)
     public static <A, D extends DeclaredStatement<A>> StmtContext<A, ?, ?> findFirstSubstatement(
             final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType) {
         final StmtContext<A, ?, ?> effectiveSubstatement = findFirstEffectiveSubstatement(stmtContext, declaredType);
@@ -160,9 +161,9 @@ public final class StmtContextUtils {
     }
 
     public static <D extends DeclaredStatement<?>> StmtContext<?, ?, ?> findFirstDeclaredSubstatementOnSublevel(
-            final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType, int sublevel) {
+            final StmtContext<?, ?, ?> stmtContext, final Class<? super D> declaredType, int sublevel) {
         for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
-            if (sublevel == 1 && producesDeclared(subStmtContext, declaredType)) {
+            if (sublevel == 1 && subStmtContext.producesDeclared(declaredType)) {
                 return subStmtContext;
             }
             if (sublevel > 1) {
@@ -178,9 +179,9 @@ public final class StmtContextUtils {
     }
 
     public static <D extends DeclaredStatement<?>> StmtContext<?, ?, ?> findDeepFirstDeclaredSubstatement(
-            final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType) {
+            final StmtContext<?, ?, ?> stmtContext, final Class<? super D> declaredType) {
         for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
-            if (producesDeclared(subStmtContext, declaredType)) {
+            if (subStmtContext.producesDeclared(declaredType)) {
                 return subStmtContext;
             }
 
@@ -193,21 +194,19 @@ public final class StmtContextUtils {
         return null;
     }
 
-    public static boolean producesDeclared(final StmtContext<?, ?, ?> ctx,
-            final Class<? extends DeclaredStatement<?>> type) {
-        return type.isAssignableFrom(ctx.getPublicDefinition().getDeclaredRepresentationClass());
-    }
-
     public static boolean isInExtensionBody(final StmtContext<?, ?, ?> stmtCtx) {
         StmtContext<?, ?, ?> current = stmtCtx;
-        while (current.coerceParentContext().getParentContext() != null) {
-            current = current.getParentContext();
-            if (isUnknownStatement(current)) {
+
+        while (true) {
+            final StmtContext<?, ?, ?> parent = current.coerceParentContext();
+            if (parent.getParentContext() == null) {
+                return false;
+            }
+            if (isUnknownStatement(parent)) {
                 return true;
             }
+            current = parent;
         }
-
-        return false;
     }
 
     /**
@@ -238,8 +237,7 @@ public final class StmtContextUtils {
      *             if supplied statement context is null
      */
     public static boolean isUnrecognizedStatement(final StmtContext<?, ?, ?> stmtCtx) {
-        return UnrecognizedStatement.class
-                .isAssignableFrom(stmtCtx.getPublicDefinition().getDeclaredRepresentationClass());
+        return stmtCtx.producesDeclared(UnrecognizedStatement.class);
     }
 
     public static boolean checkFeatureSupport(final StmtContext<?, ?, ?> stmtContext,
@@ -286,7 +284,7 @@ public final class StmtContextUtils {
     }
 
     private static boolean containsPresenceSubStmt(final StmtContext<?, ?, ?> stmtCtx) {
-        return findFirstSubstatement(stmtCtx, PresenceStatement.class) != null;
+        return stmtCtx.hasSubstatement(PresenceEffectiveStatement.class);
     }
 
     /**
@@ -338,22 +336,20 @@ public final class StmtContextUtils {
      * Checks whether at least one ancestor of a StatementContext matches one from a collection of statement
      * definitions.
      *
-     * @param ctx
-     *            StatementContext to be checked
-     * @param ancestorTypes
-     *            collection of statement definitions
+     * @param stmt EffectiveStmtCtx to be checked
+     * @param ancestorTypes collection of statement definitions
      * @return true if at least one ancestor of a StatementContext matches one
      *         from collection of statement definitions, otherwise false.
      */
-    public static boolean hasAncestorOfType(final StmtContext<?, ?, ?> ctx,
+    public static boolean hasAncestorOfType(final EffectiveStmtCtx stmt,
             final Collection<StatementDefinition> ancestorTypes) {
         requireNonNull(ancestorTypes);
-        StmtContext<?, ?, ?> current = ctx.getParentContext();
+        Parent current = stmt.effectiveParent();
         while (current != null) {
-            if (ancestorTypes.contains(current.getPublicDefinition())) {
+            if (ancestorTypes.contains(current.publicDefinition())) {
                 return true;
             }
-            current = current.getParentContext();
+            current = current.effectiveParent();
         }
         return false;
     }
@@ -361,27 +357,24 @@ public final class StmtContextUtils {
     /**
      * Checks whether all of StmtContext's ancestors of specified type have a child of specified type.
      *
-     * @param ctx StmtContext to be checked
+     * @param stmt EffectiveStmtCtx to be checked
      * @param ancestorType type of ancestor to search for
      * @param ancestorChildType type of child to search for in the specified ancestor type
      * @return true if all of StmtContext's ancestors of specified type have a child of specified type, otherwise false
      */
     public static <A, D extends DeclaredStatement<A>> boolean hasAncestorOfTypeWithChildOfType(
-            final StmtContext<?, ?, ?> ctx, final StatementDefinition ancestorType,
+            final EffectiveStmtCtx.Current<?, ?> stmt, final StatementDefinition ancestorType,
             final StatementDefinition ancestorChildType) {
-        requireNonNull(ctx);
+        requireNonNull(stmt);
         requireNonNull(ancestorType);
         requireNonNull(ancestorChildType);
 
-        StmtContext<?, ?, ?> current = ctx.coerceParentContext();
+        StmtContext<?, ?, ?> current = stmt.caerbannog().getParentContext();
         StmtContext<?, ?, ?> parent = current.getParentContext();
         while (parent != null) {
-            if (ancestorType.equals(current.getPublicDefinition())) {
-                @SuppressWarnings("unchecked")
-                final Class<D> ancestorChildTypeClass = (Class<D>) ancestorChildType.getDeclaredRepresentationClass();
-                if (findFirstSubstatement(current, ancestorChildTypeClass) == null) {
-                    return false;
-                }
+            if (ancestorType.equals(current.getPublicDefinition())
+                    && !current.hasSubstatement(ancestorChildType.getEffectiveRepresentationClass())) {
+                return false;
             }
 
             current = parent;
@@ -391,13 +384,23 @@ public final class StmtContextUtils {
         return true;
     }
 
+    /**
+     * Checks whether the parent of EffectiveStmtCtx is of specified type.
+     *
+     * @param stmt EffectiveStmtCtx to be checked
+     * @param parentType type of parent to check
+     * @return true if the parent of StmtContext is of specified type, otherwise false
+     */
+    public static boolean hasParentOfType(final EffectiveStmtCtx.Current<?, ?> stmt,
+            final StatementDefinition parentType) {
+        return hasParentOfType(stmt.caerbannog(), parentType);
+    }
+
     /**
      * Checks whether the parent of StmtContext is of specified type.
      *
-     * @param ctx
-     *            StmtContext to be checked
-     * @param parentType
-     *            type of parent to check
+     * @param ctx StmtContext to be checked
+     * @param parentType type of parent to check
      * @return true if the parent of StmtContext is of specified type, otherwise false
      */
     public static boolean hasParentOfType(final StmtContext<?, ?, ?> ctx, final StatementDefinition parentType) {
@@ -421,9 +424,8 @@ public final class StmtContextUtils {
             return;
         }
 
-        final StmtContext<?, ?, ?> listStmtCtx = ctx.getParentContext();
-        final StmtContext<Collection<SchemaNodeIdentifier>, ?, ?> keyStmtCtx = findFirstDeclaredSubstatement(
-            listStmtCtx, KeyStatement.class);
+        final StmtContext<?, ?, ?> listStmtCtx = ctx.coerceParentContext();
+        final StmtContext<Set<QName>, ?, ?> keyStmtCtx = findFirstDeclaredSubstatement(listStmtCtx, KeyStatement.class);
 
         if (YangStmtMapping.LEAF.equals(ctx.getPublicDefinition())) {
             if (isListKey(ctx, keyStmtCtx)) {
@@ -444,14 +446,8 @@ public final class StmtContextUtils {
     }
 
     private static boolean isListKey(final StmtContext<?, ?, ?> leafStmtCtx,
-            final StmtContext<Collection<SchemaNodeIdentifier>, ?, ?> keyStmtCtx) {
-        for (final SchemaNodeIdentifier keyIdentifier : keyStmtCtx.coerceStatementArgument()) {
-            if (leafStmtCtx.getStatementArgument().equals(keyIdentifier.getLastComponent())) {
-                return true;
-            }
-        }
-
-        return false;
+            final StmtContext<Set<QName>, ?, ?> keyStmtCtx) {
+        return keyStmtCtx.coerceStatementArgument().contains(leafStmtCtx.getStatementArgument());
     }
 
     private static void disallowIfFeatureAndWhenOnListKeys(final StmtContext<?, ?, ?> leafStmtCtx) {
@@ -586,9 +582,9 @@ public final class StmtContextUtils {
         final StmtContext<?, ?, ?> rootCtx = ctx.getRoot();
         final QNameModule qnameModule;
 
-        if (producesDeclared(rootCtx, ModuleStatement.class)) {
+        if (rootCtx.producesDeclared(ModuleStatement.class)) {
             qnameModule = rootCtx.getFromNamespace(ModuleCtxToModuleQName.class, rootCtx);
-        } else if (producesDeclared(rootCtx, SubmoduleStatement.class)) {
+        } else if (rootCtx.producesDeclared(SubmoduleStatement.class)) {
             final String belongsToModuleName = firstAttributeOf(rootCtx.declaredSubstatements(),
                 BelongsToStatement.class);
             qnameModule = rootCtx.getFromNamespace(ModuleNameToModuleQName.class, belongsToModuleName);
@@ -601,38 +597,25 @@ public final class StmtContextUtils {
     }
 
     public static QNameModule getModuleQNameByPrefix(final StmtContext<?, ?, ?> ctx, final String prefix) {
-        final StmtContext<?, ?, ?> importedModule = ctx.getRoot().getFromNamespace(ImportPrefixToModuleCtx.class,
-            prefix);
+        final StmtContext<?, ?, ?> root = ctx.getRoot();
+        final StmtContext<?, ?, ?> importedModule = root.getFromNamespace(ImportPrefixToModuleCtx.class, prefix);
         final QNameModule qnameModule = ctx.getFromNamespace(ModuleCtxToModuleQName.class, importedModule);
         if (qnameModule != null) {
             return qnameModule;
         }
 
-        if (producesDeclared(ctx.getRoot(), SubmoduleStatement.class)) {
-            final String moduleName = ctx.getRoot().getFromNamespace(BelongsToPrefixToModuleName.class, prefix);
+        if (root.producesDeclared(SubmoduleStatement.class)) {
+            final String moduleName = root.getFromNamespace(BelongsToPrefixToModuleName.class, prefix);
             return ctx.getFromNamespace(ModuleNameToModuleQName.class, moduleName);
         }
 
         return null;
     }
 
-    public static SourceIdentifier createSourceIdentifier(final StmtContext<?, ?, ?> root) {
-        final QNameModule qNameModule = root.getFromNamespace(ModuleCtxToModuleQName.class, root);
-        if (qNameModule != null) {
-            // creates SourceIdentifier for a module
-            return RevisionSourceIdentifier.create((String) root.getStatementArgument(), qNameModule.getRevision());
-        }
-
-        // creates SourceIdentifier for a submodule
-        final Optional<Revision> revision = getLatestRevision(root.declaredSubstatements());
-        return RevisionSourceIdentifier.create((String) root.getStatementArgument(), revision);
-    }
-
     public static Optional<Revision> getLatestRevision(final Iterable<? extends StmtContext<?, ?, ?>> subStmts) {
         Revision revision = null;
         for (final StmtContext<?, ?, ?> subStmt : subStmts) {
-            if (subStmt.getPublicDefinition().getDeclaredRepresentationClass().isAssignableFrom(
-                    RevisionStatement.class)) {
+            if (subStmt.producesDeclared(RevisionStatement.class)) {
                 if (revision == null && subStmt.getStatementArgument() != null) {
                     revision = (Revision) subStmt.getStatementArgument();
                 } else {