Speed up StmtContextUtils.getModuleQNameByPrefix()
[yangtools.git] / yang / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / StmtContextUtils.java
index 7cc59aca7a0f2b6f09fbfe1b9f44b6215de981fd..aadf22441be765ab15949e8d3e7dd83d6446f7f8 100644 (file)
@@ -29,13 +29,11 @@ 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.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.source.BelongsToPrefixToModuleName;
 import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToModuleCtx;
 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
@@ -81,6 +79,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) {
@@ -88,7 +87,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);
             }
@@ -150,7 +149,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);
@@ -159,9 +160,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) {
@@ -177,9 +178,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;
             }
 
@@ -192,12 +193,6 @@ public final class StmtContextUtils {
         return null;
     }
 
-    @Deprecated(forRemoval = true)
-    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;
 
@@ -288,7 +283,7 @@ public final class StmtContextUtils {
     }
 
     private static boolean containsPresenceSubStmt(final StmtContext<?, ?, ?> stmtCtx) {
-        return findFirstSubstatement(stmtCtx, PresenceStatement.class) != null;
+        return stmtCtx.hasSubstatement(PresenceEffectiveStatement.class);
     }
 
     /**
@@ -378,12 +373,9 @@ public final class StmtContextUtils {
         StmtContext<?, ?, ?> current = ctx.coerceParentContext();
         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;
@@ -596,14 +588,13 @@ 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;
         }
 
-        final StmtContext<?, ?, ?> root = ctx.getRoot();
         if (root.producesDeclared(SubmoduleStatement.class)) {
             final String moduleName = root.getFromNamespace(BelongsToPrefixToModuleName.class, prefix);
             return ctx.getFromNamespace(ModuleNameToModuleQName.class, moduleName);
@@ -612,18 +603,6 @@ public final class StmtContextUtils {
         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) {