Bug 7051 - Refactoring of StmtContextUtils
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / SubstatementContext.java
index fabe7692b14cafa2544650014c73e5dffad2c3bc..0a2420519b5abc68020f92f0f7d8471106fda345 100644 (file)
@@ -7,14 +7,15 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
 
-import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Verify;
 import com.google.common.collect.ImmutableSet;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Optional;
 import java.util.Set;
 import javax.annotation.Nonnull;
+import org.opendaylight.yangtools.util.OptionalBoolean;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.common.YangVersion;
@@ -46,7 +47,6 @@ import org.opendaylight.yangtools.yang.parser.spi.source.AugmentToChoiceNamespac
 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -65,8 +65,22 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
      * This field maintains a resolution cache, so once we have returned a result, we will keep on returning the same
      * result without performing any lookups.
      */
-    private boolean haveConfiguration;
-    private boolean configuration;
+    // BooleanField value
+    private byte configuration;
+
+    /**
+     * This field maintains a resolution cache for ignore config, so once we have returned a result, we will
+     * keep on returning the same result without performing any lookups.
+     */
+    // BooleanField value
+    private byte ignoreConfig;
+
+    /**
+     * This field maintains a resolution cache for ignore if-feature, so once we have returned a result, we will
+     * keep on returning the same result without performing any lookups.
+     */
+    // BooleanField value
+    private byte ignoreIfFeature;
 
     private volatile SchemaPath schemaPath;
 
@@ -78,10 +92,10 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
     }
 
     @SuppressWarnings("unchecked")
-    SubstatementContext(final SubstatementContext<A, D, E> original, final QNameModule newQNameModule,
-            final StatementContextBase<?, ?, ?> newParent, final CopyType typeOfCopy) {
-        super(original);
-        this.parent = newParent;
+    private SubstatementContext(final SubstatementContext<A, D, E> original, final QNameModule newQNameModule,
+            final StatementContextBase<?, ?, ?> newParent, final CopyType copyType) {
+        super(original, copyType);
+        this.parent = Preconditions.checkNotNull(newParent);
 
         if (newQNameModule != null) {
             final A originalArg = original.argument;
@@ -132,7 +146,7 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
     }
 
     @Override
-    public StatementContextBase<?, ?, ?> createCopy(final StatementContextBase<?, ?, ?> newParent,
+    public StatementContextBase<A, D, E> createCopy(final StatementContextBase<?, ?, ?> newParent,
             final CopyType typeOfCopy) {
         return createCopy(null, newParent, typeOfCopy);
     }
@@ -145,14 +159,6 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
 
         final SubstatementContext<A, D, E> copy = new SubstatementContext<>(this, newQNameModule, newParent, typeOfCopy);
 
-        copy.appendCopyHistory(typeOfCopy, this.getCopyHistory());
-
-        if (this.getOriginalCtx() != null) {
-            copy.setOriginalCtx(this.getOriginalCtx());
-        } else {
-            copy.setOriginalCtx(this);
-        }
-
         definition().onStatementAdded(copy);
 
         copy.copyStatements(this, newQNameModule, typeOfCopy);
@@ -161,28 +167,27 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
 
     private void copyStatements(final SubstatementContext<A, D, E> original, final QNameModule newQNameModule,
             final CopyType typeOfCopy) {
-        final Collection<StatementContextBase<?, ?, ?>> declared = original.declaredSubstatements();
-        final Collection<StatementContextBase<?, ?, ?>> effective = original.effectiveSubstatements();
-        final Collection<StatementContextBase<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
+        final Collection<? extends Mutable<?, ?, ?>> declared = original.mutableDeclaredSubstatements();
+        final Collection<? extends Mutable<?, ?, ?>> effective = original.mutableEffectiveSubstatements();
+        final Collection<Mutable<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
 
-        for (final StatementContextBase<?, ?, ?> stmtContext : declared) {
-            if (StmtContextUtils.areFeaturesSupported(stmtContext)) {
+        for (final Mutable<?, ?, ?> stmtContext : declared) {
+            if (stmtContext.isSupportedByFeatures()) {
                 copySubstatement(stmtContext, newQNameModule, typeOfCopy, buffer);
             }
         }
 
-        for (final StatementContextBase<?, ?, ?> stmtContext : effective) {
+        for (final Mutable<?, ?, ?> stmtContext : effective) {
             copySubstatement(stmtContext, newQNameModule, typeOfCopy, buffer);
         }
 
         addEffectiveSubstatements(buffer);
     }
 
-    private void copySubstatement(final StatementContextBase<?, ?, ?> stmtContext,
-            final QNameModule newQNameModule, final CopyType typeOfCopy,
-            final Collection<StatementContextBase<?, ?, ?>> buffer) {
+    private void copySubstatement(final Mutable<?, ?, ?> stmtContext, final QNameModule newQNameModule,
+            final CopyType typeOfCopy, final Collection<Mutable<?, ?, ?>> buffer) {
         if (needToCopyByUses(stmtContext)) {
-            final StatementContextBase<?, ?, ?> copy = stmtContext.createCopy(newQNameModule, this, typeOfCopy);
+            final Mutable<?, ?, ?> copy = stmtContext.createCopy(newQNameModule, this, typeOfCopy);
             LOG.debug("Copying substatement {} for {} as", stmtContext, this, copy);
             buffer.add(copy);
         } else if (isReusedByUses(stmtContext)) {
@@ -232,13 +237,13 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
         Verify.verify(maybeParentPath.isPresent(), "Parent %s does not have a SchemaPath", parent);
         final SchemaPath parentPath = maybeParentPath.get();
 
-        if (Utils.isUnknownNode(this)) {
+        if (StmtContextUtils.isUnknownStatement(this)) {
             return parentPath.createChild(getPublicDefinition().getStatementName());
         }
         if (argument instanceof QName) {
             final QName qname = (QName) argument;
             if (StmtContextUtils.producesDeclared(this, UsesStatement.class)) {
-                return maybeParentPath.orNull();
+                return maybeParentPath.orElse(null);
             }
 
             final SchemaPath path;
@@ -253,21 +258,20 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
         }
         if (argument instanceof String) {
             // FIXME: This may yield illegal argument exceptions
-            final StatementContextBase<?, ?, ?> originalCtx = getOriginalCtx();
-            final QName qname = originalCtx != null ? Utils.qNameFromArgument(originalCtx, (String) argument) : Utils
-                    .qNameFromArgument(this, (String) argument);
+            final Optional<StmtContext<?, ?, ?>> originalCtx = getOriginalCtx();
+            final QName qname = StmtContextUtils.qnameFromArgument(originalCtx.orElse(this), (String) argument);
             return parentPath.createChild(qname);
         }
         if (argument instanceof SchemaNodeIdentifier
-                && (StmtContextUtils.producesDeclared(this, AugmentStatement.class) || StmtContextUtils
-                        .producesDeclared(this, RefineStatement.class) || StmtContextUtils
-                .producesDeclared(this, DeviationStatement.class))) {
+                && (StmtContextUtils.producesDeclared(this, AugmentStatement.class)
+                        || StmtContextUtils.producesDeclared(this, RefineStatement.class)
+                        || StmtContextUtils.producesDeclared(this, DeviationStatement.class))) {
 
             return parentPath.createChild(((SchemaNodeIdentifier) argument).getPathFromRoot());
         }
 
         // FIXME: this does not look right
-        return maybeParentPath.orNull();
+        return maybeParentPath.orElse(null);
     }
 
     @Nonnull
@@ -285,18 +289,17 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
 
         }
 
-        return Optional.fromNullable(local);
-    }
-
-    @Override
-    public boolean isRootContext() {
-        return false;
+        return Optional.ofNullable(local);
     }
 
     @Override
     public boolean isConfiguration() {
-        if (haveConfiguration) {
-            return configuration;
+        if (isIgnoringConfig()) {
+            return true;
+        }
+
+        if (OptionalBoolean.isPresent(configuration)) {
+            return OptionalBoolean.get(configuration);
         }
 
         final StmtContext<Boolean, ?, ?> configStatement = StmtContextUtils.findFirstSubstatement(this,
@@ -316,14 +319,13 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
         }
 
         // Resolved, make sure we cache this return
-        configuration = isConfig;
-        haveConfiguration = true;
+        configuration = OptionalBoolean.of(isConfig);
         return isConfig;
     }
 
     @Override
-    public boolean isEnabledOpenconfigVersioning() {
-        return parent.isEnabledOpenconfigVersioning();
+    public boolean isEnabledSemanticVersioning() {
+        return parent.isEnabledSemanticVersioning();
     }
 
     @Override
@@ -350,4 +352,28 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
     public void setRootIdentifier(final ModuleIdentifier identifier) {
         getRoot().setRootIdentifier(identifier);
     }
+
+    @Override
+    protected boolean isIgnoringIfFeatures() {
+        if (OptionalBoolean.isPresent(ignoreIfFeature)) {
+            return OptionalBoolean.get(ignoreIfFeature);
+        }
+
+        final boolean ret = definition().isIgnoringIfFeatures() || parent.isIgnoringIfFeatures();
+        ignoreIfFeature = OptionalBoolean.of(ret);
+
+        return ret;
+    }
+
+    @Override
+    protected boolean isIgnoringConfig() {
+        if (OptionalBoolean.isPresent(ignoreConfig)) {
+            return OptionalBoolean.get(ignoreConfig);
+        }
+
+        final boolean ret = definition().isIgnoringConfig() || parent.isIgnoringConfig();
+        ignoreConfig = OptionalBoolean.of(ret);
+
+        return ret;
+    }
 }