Move SourceSpecificContext.lookupDeclaredChild() 91/87691/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 13 Feb 2020 12:24:57 +0000 (13:24 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 13 Feb 2020 22:44:22 +0000 (23:44 +0100)
This method is only used from StatementContextWriter and it is
not touching any SourceSpecificContext state. Move it to its sole
caller as a private method returning a nullable.

JIRA: YANGTOOLS-652
Change-Id: Ib05581b2edcca213734748e31d17d0b7caeb9a92
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/SourceSpecificContext.java
yang/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextWriter.java

index 59d7be7495ef8b9ddbd87eca45d475b83742351e..324e4a83f6d9f083c6d8c4e05703d3ba743641ca 100644 (file)
@@ -32,7 +32,6 @@ import org.opendaylight.yangtools.yang.common.YangVersion;
 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.StatementSource;
 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;
@@ -103,21 +102,6 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh
         return inProgressPhase;
     }
 
-    Optional<AbstractResumedStatement<?, ?, ?>> lookupDeclaredChild(final AbstractResumedStatement<?, ?, ?> current,
-            final int childId) {
-        if (current == null) {
-            return Optional.empty();
-        }
-
-        // Fast path: we are entering a statement which was emitted in previous phase
-        AbstractResumedStatement<?, ?, ?> existing = current.lookupSubstatement(childId);
-        while (existing != null && StatementSource.CONTEXT == existing.getStatementSource()) {
-            existing = existing.lookupSubstatement(childId);
-        }
-
-        return Optional.ofNullable(existing);
-    }
-
     AbstractResumedStatement<?, ?, ?> createDeclaredChild(final AbstractResumedStatement<?, ?, ?> current,
             final int childId, final QName name, final String argument, final StatementSourceReference ref) {
         StatementDefinitionContext<?, ?, ?> def = currentContext.getStatementDefinition(getRootVersion(), name);
index 27bd70f2c6d37bb4cbe86a05b17833789bfc5284..c0ef1180af43447f01c608f4cf2f9cc6d1a0c682 100644 (file)
@@ -14,6 +14,7 @@ import static com.google.common.base.Verify.verifyNotNull;
 import static java.util.Objects.requireNonNull;
 
 import java.util.Optional;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
@@ -33,9 +34,12 @@ final class StatementContextWriter implements StatementWriter {
 
     @Override
     public Optional<? extends ResumedStatement> resumeStatement(final int childId) {
-        final Optional<AbstractResumedStatement<?, ?, ?>> existing = ctx.lookupDeclaredChild(current, childId);
-        existing.ifPresent(this::resumeStatement);
-        return existing;
+        final AbstractResumedStatement<?, ?, ?> existing = lookupDeclaredChild(current, childId);
+        if (existing != null) {
+            resumeStatement(existing);
+            return Optional.of(existing);
+        }
+        return Optional.empty();
     }
 
     private void resumeStatement(final AbstractResumedStatement<?, ?, ?> child) {
@@ -61,9 +65,9 @@ final class StatementContextWriter implements StatementWriter {
     @Override
     public void startStatement(final int childId, final QName name, final String argument,
             final StatementSourceReference ref) {
-        final Optional<AbstractResumedStatement<?, ?, ?>> existing = ctx.lookupDeclaredChild(current, childId);
-        current = existing.isPresent() ? existing.get()
-                :  verifyNotNull(ctx.createDeclaredChild(current, childId, name, argument, ref));
+        final AbstractResumedStatement<?, ?, ?> existing = lookupDeclaredChild(current, childId);
+        current = existing != null ? existing
+                : verifyNotNull(ctx.createDeclaredChild(current, childId, name, argument, ref));
     }
 
     @Override
@@ -95,4 +99,19 @@ final class StatementContextWriter implements StatementWriter {
             current = null;
         }
     }
+
+    private static @Nullable AbstractResumedStatement<?, ?, ?> lookupDeclaredChild(
+            final AbstractResumedStatement<?, ?, ?> current, final int childId) {
+        if (current == null) {
+            return null;
+        }
+
+        // Fast path: we are entering a statement which was emitted in previous phase
+        AbstractResumedStatement<?, ?, ?> existing = current.lookupSubstatement(childId);
+        while (existing != null && StatementSource.CONTEXT == existing.getStatementSource()) {
+            existing = existing.lookupSubstatement(childId);
+        }
+
+        return existing;
+    }
 }