Migrate rawStatementArgument()/getStatementSourceReference() callers
[yangtools.git] / yang / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / AbstractResumedStatement.java
index c3a62b8f883c34a99e80302a7b16a6677f994608..004676dfbb8e3636235c812019008a9a029c6760 100644 (file)
@@ -8,13 +8,19 @@
 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
 
 import static com.google.common.base.Preconditions.checkState;
+import static java.util.Objects.requireNonNull;
 
+import com.google.common.collect.ImmutableList;
 import java.util.Collection;
+import java.util.List;
 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.model.api.meta.DeclaredStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
+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.ModelProcessingPhase;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
@@ -33,45 +39,108 @@ import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter.Resumed
  */
 abstract class AbstractResumedStatement<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
         extends StatementContextBase<A, D, E> implements ResumedStatement {
+    private final @NonNull StatementSourceReference statementDeclSource;
+    private final String rawArgument;
+
+    private List<StatementContextBase<?, ?, ?>> effective = ImmutableList.of();
     private StatementMap substatements = StatementMap.empty();
+    private @Nullable D declaredInstance;
+
+    // Copy constructor
+    AbstractResumedStatement(final AbstractResumedStatement<A, D, E> original) {
+        super(original);
+        this.statementDeclSource = original.statementDeclSource;
+        this.rawArgument = original.rawArgument;
+        this.substatements = original.substatements;
+        this.declaredInstance = original.declaredInstance;
+    }
 
     AbstractResumedStatement(final StatementDefinitionContext<A, D, E> def, final StatementSourceReference ref,
             final String rawArgument) {
-        super(def, ref, rawArgument);
+        super(def);
+        this.statementDeclSource = requireNonNull(ref);
+        this.rawArgument = def.support().internArgument(rawArgument);
     }
 
     AbstractResumedStatement(final StatementDefinitionContext<A, D, E> def, final StatementSourceReference ref,
             final String rawArgument, final CopyType copyType) {
-        super(def, ref, rawArgument, copyType);
+        super(def, CopyHistory.of(copyType, CopyHistory.original()));
+        this.statementDeclSource = requireNonNull(ref);
+        this.rawArgument = rawArgument;
     }
 
-    AbstractResumedStatement(final StatementContextBase<A, D, E> original, final CopyType copyType) {
-        super(original, copyType);
+    @Override
+    public final Optional<StmtContext<A, D, E>> getOriginalCtx() {
+        return Optional.empty();
     }
 
-    AbstractResumedStatement(final AbstractResumedStatement<A, D, E> original) {
-        super(original);
-        this.substatements = original.substatements;
+    @Override
+    public final Optional<StmtContext<A, D, E>> getPreviousCopyCtx() {
+        return Optional.empty();
     }
 
     @Override
-    public Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements() {
-        return substatements.values();
+    public final StatementSourceReference sourceReference() {
+        return statementDeclSource;
+    }
+
+    @Override
+    public final String rawArgument() {
+        return rawArgument;
     }
 
     @Override
     public Collection<? extends StatementContextBase<?, ?, ?>> mutableDeclaredSubstatements() {
-        return substatements.values();
+        return substatements;
+    }
+
+    @Override
+    public final Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements() {
+        return mutableEffectiveSubstatements(effective);
+    }
+
+    @Override
+    public final void removeStatementFromEffectiveSubstatements(final StatementDefinition statementDef) {
+        effective = removeStatementFromEffectiveSubstatements(effective, statementDef);
+    }
+
+    @Override
+    public final void removeStatementFromEffectiveSubstatements(final StatementDefinition statementDef,
+            final String statementArg) {
+        effective = removeStatementFromEffectiveSubstatements(effective, statementDef, statementArg);
     }
 
     @Override
-    public @NonNull StatementDefinition getDefinition() {
-        return getPublicDefinition();
+    public final void addEffectiveSubstatement(final Mutable<?, ?, ?> substatement) {
+        effective = addEffectiveSubstatement(effective, substatement);
     }
 
     @Override
-    public @NonNull StatementSourceReference getSourceReference() {
-        return getStatementSourceReference();
+    final void addEffectiveSubstatementsImpl(final Collection<? extends Mutable<?, ?, ?>> statements) {
+        effective = addEffectiveSubstatementsImpl(effective, statements);
+    }
+
+    @Override
+    public final D buildDeclared() {
+        final D existing;
+        return (existing = declaredInstance) != null ? existing : loadDeclared();
+    }
+
+    private @NonNull D loadDeclared() {
+        final ModelProcessingPhase phase = getCompletedPhase();
+        checkState(phase == ModelProcessingPhase.FULL_DECLARATION || phase == ModelProcessingPhase.EFFECTIVE_MODEL,
+                "Cannot build declared instance after phase %s", phase);
+        return declaredInstance = definition().getFactory().createDeclared(this);
+    }
+
+    @Override
+    public StatementDefinition getDefinition() {
+        return publicDefinition();
+    }
+
+    @Override
+    public StatementSourceReference getSourceReference() {
+        return sourceReference();
     }
 
     @Override
@@ -98,7 +167,7 @@ abstract class AbstractResumedStatement<A, D extends DeclaredStatement<A>, E ext
                     final String argument) {
         final ModelProcessingPhase inProgressPhase = getRoot().getSourceContext().getInProgressPhase();
         checkState(inProgressPhase != ModelProcessingPhase.EFFECTIVE_MODEL,
-                "Declared statement cannot be added in effective phase at: %s", getStatementSourceReference());
+                "Declared statement cannot be added in effective phase at: %s", sourceReference());
 
         final Optional<StatementSupport<?, ?, ?>> implicitParent =
                 definition().getImplicitParentFor(def.getPublicView());
@@ -113,6 +182,26 @@ abstract class AbstractResumedStatement<A, D extends DeclaredStatement<A>, E ext
         return ret;
     }
 
+    @Override
+    final boolean hasEmptySubstatements() {
+        return substatements.size() == 0 && effective.isEmpty();
+    }
+
+    @Override
+    final Iterable<StatementContextBase<?, ?, ?>> effectiveChildrenToComplete() {
+        return effective;
+    }
+
+    @Override
+    final Stream<? extends StmtContext<?, ?, ?>> streamDeclared() {
+        return declaredSubstatements().stream();
+    }
+
+    @Override
+    final Stream<? extends StmtContext<?, ?, ?>> streamEffective() {
+        return effective.stream();
+    }
+
     /**
      * Lookup substatement by its offset in this statement.
      *
@@ -129,7 +218,7 @@ abstract class AbstractResumedStatement<A, D extends DeclaredStatement<A>, E ext
 
     final void walkChildren(final ModelProcessingPhase phase) {
         checkState(isFullyDefined());
-        substatements.values().forEach(stmt -> {
+        substatements.forEach(stmt -> {
             stmt.walkChildren(phase);
             stmt.endDeclared(phase);
         });