Move common code
[yangtools.git] / yang / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / StatementContextBase.java
index 47f2a3e2e082193aa9aeeef6f76d1fe8f29d31d3..8cc6b37712003e0d993b735986bf3676031fe319 100644 (file)
@@ -101,6 +101,7 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
     private final @NonNull StatementDefinitionContext<A, D, E> definition;
     private final @NonNull StatementSourceReference statementDeclSource;
     private final StmtContext<?, ?, ?> originalCtx;
+    private final StmtContext<?, ?, ?> prevCopyCtx;
     private final CopyHistory copyHistory;
     private final String rawArgument;
 
@@ -127,6 +128,7 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
         this.rawArgument = def.internArgument(rawArgument);
         this.copyHistory = CopyHistory.original();
         this.originalCtx = null;
+        this.prevCopyCtx = null;
     }
 
     StatementContextBase(final StatementDefinitionContext<A, D, E> def, final StatementSourceReference ref,
@@ -136,6 +138,7 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
         this.rawArgument = rawArgument;
         this.copyHistory = CopyHistory.of(copyType, CopyHistory.original());
         this.originalCtx = null;
+        this.prevCopyCtx = null;
     }
 
     StatementContextBase(final StatementContextBase<A, D, E> original, final CopyType copyType) {
@@ -144,6 +147,7 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
         this.rawArgument = original.rawArgument;
         this.copyHistory = CopyHistory.of(copyType, original.getCopyHistory());
         this.originalCtx = original.getOriginalCtx().orElse(original);
+        this.prevCopyCtx = original;
     }
 
     StatementContextBase(final StatementContextBase<A, D, E> original) {
@@ -152,6 +156,7 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
         this.rawArgument = original.rawArgument;
         this.copyHistory = original.getCopyHistory();
         this.originalCtx = original.getOriginalCtx().orElse(original);
+        this.prevCopyCtx = original;
         this.substatements = original.substatements;
         this.effective = original.effective;
     }
@@ -210,9 +215,7 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
         // If the set of supported features has not been provided, all features are supported by default.
         final Set<QName> supportedFeatures = getFromNamespace(SupportedFeaturesNamespace.class,
                 SupportedFeatures.SUPPORTED_FEATURES);
-        final boolean ret = supportedFeatures == null ? true
-                : StmtContextUtils.checkFeatureSupport(this, supportedFeatures);
-
+        final boolean ret = supportedFeatures == null || StmtContextUtils.checkFeatureSupport(this, supportedFeatures);
         supportedByFeatures = OptionalBoolean.of(ret);
         return ret;
     }
@@ -243,6 +246,11 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
         return Optional.ofNullable(originalCtx);
     }
 
+    @Override
+    public Optional<? extends StmtContext<?, ?, ?>> getPreviousCopyCtx() {
+        return Optional.ofNullable(prevCopyCtx);
+    }
+
     @Override
     public ModelProcessingPhase getCompletedPhase() {
         return completedPhase;
@@ -313,6 +321,15 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
         return Collections.unmodifiableCollection(effective);
     }
 
+    /**
+     * Remove a set of statements from effective statements.
+     *
+     * @param statements statements to be removed
+     * @deprecated This method was used by EffectiveStatementBase to restore proper order of effects of uses statements.
+     *             It is no longer used in that capacity and slated for removal.
+     */
+    // FIXME: 5.0.0: remove this method
+    @Deprecated
     public void removeStatementsFromEffectiveSubstatements(
             final Collection<? extends StmtContext<?, ?, ?>> statements) {
         if (!effective.isEmpty()) {
@@ -792,12 +809,11 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
 
             copy = new SubstatementContext<>(original, result, childCopyType, targetModule);
             result.addEffectiveSubstatement(copy);
-            original.definition().onStatementAdded(copy);
         } else {
             result = copy = new SubstatementContext<>(original, this, type, targetModule);
-            original.definition().onStatementAdded(copy);
         }
 
+        original.definition().onStatementAdded(copy);
         original.copyTo(copy, type, targetModule);
         return result;
     }
@@ -873,11 +889,11 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
     }
 
     // FIXME: revise this, as it seems to be wrong
-    private static final Set<YangStmtMapping> NOCOPY_FROM_GROUPING_SET = ImmutableSet.of(
+    private static final ImmutableSet<YangStmtMapping> NOCOPY_FROM_GROUPING_SET = ImmutableSet.of(
         YangStmtMapping.DESCRIPTION,
         YangStmtMapping.REFERENCE,
         YangStmtMapping.STATUS);
-    private static final Set<YangStmtMapping> REUSED_DEF_SET = ImmutableSet.of(
+    private static final ImmutableSet<YangStmtMapping> REUSED_DEF_SET = ImmutableSet.of(
         YangStmtMapping.TYPE,
         YangStmtMapping.TYPEDEF,
         YangStmtMapping.USES);