BUG-5222: Optimize use of declared substatements
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / StmtContextUtils.java
index 90f7230e0a2acd2aed0d9f75433ad6e112932e9e..3e5589264ffc19b79c1414eb9f7e30c166196b7c 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.yangtools.yang.parser.spi.meta;
 
+import com.google.common.base.Preconditions;
 import com.google.common.base.Splitter;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
@@ -17,6 +18,7 @@ import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
+import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.MinElementsStatement;
@@ -25,6 +27,7 @@ import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.IfFeaturePredicates;
 import org.opendaylight.yangtools.yang.parser.spi.source.SupportedFeaturesNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.source.SupportedFeaturesNamespace.SupportedFeatures;
+import org.opendaylight.yangtools.yang.parser.stmt.reactor.RootStatementContext;
 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.UnknownStatementImpl;
 
@@ -237,12 +240,10 @@ public final class StmtContextUtils {
 
     private static boolean checkFeatureSupport(final StmtContext.Mutable<?, ?, ?> stmtContext,
             final Predicate<QName> isFeatureSupported) {
-        final Collection<StatementContextBase<?, ?, ?>> substatements = stmtContext.declaredSubstatements();
-
         boolean isSupported = false;
         boolean containsIfFeature = false;
-        for (final StatementContextBase<?, ?, ?> stmt : substatements) {
-            if (stmt.getPublicDefinition().equals(YangStmtMapping.IF_FEATURE)) {
+        for (final StatementContextBase<?, ?, ?> stmt : stmtContext.declaredSubstatements()) {
+            if (YangStmtMapping.IF_FEATURE.equals(stmt.getPublicDefinition())) {
                 containsIfFeature = true;
                 if (isFeatureSupported.test((QName) stmt.getStatementArgument())) {
                     isSupported = true;
@@ -321,4 +322,77 @@ public final class StmtContextUtils {
             return false;
         }
     }
+
+    /**
+     * Checks whether at least one ancestor of a StatementContext matches one
+     * from collection of statement definitions.
+     *
+     * @param ctx
+     *            StatementContext 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,
+            final Collection<StatementDefinition> ancestorTypes) {
+        Preconditions.checkNotNull(ctx);
+        Preconditions.checkNotNull(ancestorTypes);
+        StmtContext<?, ?, ?> current = ctx.getParentContext();
+        while (current != null) {
+            if (ancestorTypes.contains(current.getPublicDefinition())) {
+                return true;
+            }
+            current = current.getParentContext();
+        }
+        return false;
+    }
+
+    /**
+     * Checks whether all of StmtContext's ancestors of specified type have a child of specified type
+     *
+     * @param ctx StmtContext 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 <AT, DT extends DeclaredStatement<AT>> boolean hasAncestorOfTypeWithChildOfType(final StmtContext<?, ?, ?> ctx,
+            final StatementDefinition ancestorType, final StatementDefinition ancestorChildType) {
+        Preconditions.checkNotNull(ctx);
+        Preconditions.checkNotNull(ancestorType);
+        Preconditions.checkNotNull(ancestorChildType);
+        StmtContext<?, ?, ?> current = ctx.getParentContext();
+        while (!(current instanceof RootStatementContext)) {
+            if (ancestorType.equals(current.getPublicDefinition())) {
+                @SuppressWarnings("unchecked")
+                final Class<DT> ancestorChildTypeClass = (Class<DT>) ancestorChildType.getDeclaredRepresentationClass();
+                if (findFirstSubstatement(current, ancestorChildTypeClass) == null) {
+                    return false;
+                }
+            }
+            current = current.getParentContext();
+        }
+
+        return true;
+    }
+
+    /**
+     * Checks whether the parent of StmtContext is of specified type
+     *
+     * @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) {
+        Preconditions.checkNotNull(ctx);
+        Preconditions.checkNotNull(parentType);
+        final StmtContext<?, ?, ?> parentContext = ctx.getParentContext();
+        return parentContext != null ? parentType.equals(parentContext.getPublicDefinition()) : false;
+    }
 }