Cleanup yang-parser-impl
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / RootStatementContext.java
index 695251f369a89f78a36f2a961306f368ae4e2733..03ce9c8c5face706af337b47d176e7bbfb95521e 100644 (file)
  */
 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
 
-import java.util.List;
-
+import java.util.Collection;
 import java.util.LinkedList;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
+import java.util.List;
 import org.opendaylight.yangtools.yang.common.QNameModule;
-import java.util.Collection;
 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 
-class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
-        extends StatementContextBase<A, D, E> {
+/**
+ * root statement class for a Yang source
+ */
+public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
+        StatementContextBase<A, D, E> {
 
     private final SourceSpecificContext sourceContext;
     private final A argument;
 
-    RootStatementContext(ContextBuilder<A, D, E> builder,
-            SourceSpecificContext sourceContext) throws SourceException {
+    RootStatementContext(ContextBuilder<A, D, E> builder, SourceSpecificContext sourceContext) throws SourceException {
         super(builder);
         this.sourceContext = sourceContext;
-        this.argument = builder.getDefinition().parseArgumentValue(this,
-                builder.getRawArgument());
+        this.argument = builder.getDefinition().parseArgumentValue(this, builder.getRawArgument());
     }
 
-    RootStatementContext(RootStatementContext<A, D, E> original,
-            QNameModule newQNameModule) throws SourceException {
+    RootStatementContext(RootStatementContext<A, D, E> original, QNameModule newQNameModule, TypeOfCopy typeOfCopy)
+            throws SourceException {
         super(original);
 
         sourceContext = original.sourceContext;
         this.argument = original.argument;
 
-        copyDeclaredStmts(original, newQNameModule);
+        copyDeclaredStmts(original, newQNameModule, typeOfCopy);
 
-        copyEffectiveStmts(original, newQNameModule);
+        copyEffectiveStmts(original, newQNameModule, typeOfCopy);
 
     }
 
-    private void copyDeclaredStmts(RootStatementContext<A, D, E> original,
-            QNameModule newQNameModule) throws SourceException {
-        Collection<? extends StmtContext<?, ?, ?>> originalDeclaredSubstatements = original
-                .declaredSubstatements();
+    /**
+     * copies declared statements from original to this' substatements
+     *
+     * @param typeOfCopy
+     *            determines whether copy is used by augmentation or uses
+     * @throws SourceException
+     */
+    private void copyDeclaredStmts(RootStatementContext<A, D, E> original, QNameModule newQNameModule,
+            TypeOfCopy typeOfCopy) throws SourceException {
+        Collection<? extends StmtContext<?, ?, ?>> originalDeclaredSubstatements = original.declaredSubstatements();
         for (StmtContext<?, ?, ?> stmtContext : originalDeclaredSubstatements) {
-            this.addEffectiveSubstatement(stmtContext
-                    .createCopy(newQNameModule,this));
+            this.addEffectiveSubstatement(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
         }
     }
 
-    private void copyEffectiveStmts(RootStatementContext<A, D, E> original,
-            QNameModule newQNameModule) throws SourceException {
-        Collection<? extends StmtContext<?, ?, ?>> originalEffectiveSubstatements = original
-                .effectiveSubstatements();
+    /**
+     * copies effective statements from original to this' substatements
+     *
+     * @param typeOfCopy
+     *            determines whether copy is used by augmentation or uses
+     * @throws SourceException
+     */
+    private void copyEffectiveStmts(RootStatementContext<A, D, E> original, QNameModule newQNameModule,
+            TypeOfCopy typeOfCopy) throws SourceException {
+        Collection<? extends StmtContext<?, ?, ?>> originalEffectiveSubstatements = original.effectiveSubstatements();
         for (StmtContext<?, ?, ?> stmtContext : originalEffectiveSubstatements) {
-            this.addEffectiveSubstatement(stmtContext
-                    .createCopy(newQNameModule,this));
+            this.addEffectiveSubstatement(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
         }
     }
 
+    /**
+     * @return null as root cannot have parent
+     */
     @Override
     public StatementContextBase<?, ?, ?> getParentContext() {
         return null;
     }
 
+    /**
+     * @return namespace storage of source context
+     */
     @Override
     public NamespaceStorageNode getParentNamespaceStorage() {
         return sourceContext;
     }
 
+    /**
+     * @return registry of source context
+     */
     @Override
     public Registry getBehaviourRegistry() {
         return sourceContext;
     }
 
+    /**
+     * @return this as its own root
+     */
     @Override
     public RootStatementContext<?, ?, ?> getRoot() {
         return this;
@@ -95,18 +117,65 @@ class RootStatementContext<A, D extends DeclaredStatement<A>, E extends Effectiv
         return argument;
     }
 
+    /**
+     * @return copy of this considering {@link TypeOfCopy} (augment, uses)
+     *
+     * @throws SourceException instance of SourceException
+     */
     @Override
-    public StatementContextBase<A, D, E> createCopy(QNameModule newQNameModule, StatementContextBase<?, ?, ?> newParent)
+    public StatementContextBase<?, ?, ?> createCopy(StatementContextBase<?, ?, ?> newParent, TypeOfCopy typeOfCopy)
             throws SourceException {
+        return createCopy(null, newParent, typeOfCopy);
+    }
 
-        return new RootStatementContext<>(this, newQNameModule);
+    /**
+     * @return copy of this considering {@link TypeOfCopy} (augment, uses)
+     *
+     * @throws SourceException instance of SourceException
+     */
+    @Override
+    public StatementContextBase<A, D, E> createCopy(QNameModule newQNameModule,
+            StatementContextBase<?, ?, ?> newParent, TypeOfCopy typeOfCopy) throws SourceException {
+        RootStatementContext<A, D, E> copy = new RootStatementContext<>(this, newQNameModule, typeOfCopy);
+
+        copy.addAllToCopyHistory(this.getCopyHistory());
+        copy.addToCopyHistory(typeOfCopy);
+
+        if(this.getOriginalCtx() != null) {
+            copy.setOriginalCtx(this.getOriginalCtx());
+        } else {
+            copy.setOriginalCtx(this);
+        }
+
+        return copy;
     }
 
+    /**
+     * @return this' argument as it is the only from root (this)
+     */
     @Override
     public List<Object> getArgumentsFromRoot() {
-        List<Object> argumentList = new LinkedList<Object>();
+        List<Object> argumentList = new LinkedList<>();
         argumentList.add(argument);
         return argumentList;
     }
 
+    /**
+     * @return this as it is the only\context from root (this)
+     */
+    @Override
+    public List<StmtContext<?, ?, ?>> getStmtContextsFromRoot() {
+        List<StmtContext<?, ?, ?>> stmtContextsList = new LinkedList<>();
+        stmtContextsList.add(this);
+        return stmtContextsList;
+    }
+
+    /**
+     * @return true
+     */
+    @Override
+    public boolean isRootContext() {
+        return true;
+    }
+
 }