BUG-6522: create a specialized CopyHistory object
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / StatementContextBase.java
index 5352a62ae1a790f4b3dd528288083e29d74d7b6a..bb45a4a78ca968922f65b5602e85080de9a19fb4 100644 (file)
@@ -9,15 +9,15 @@ package org.opendaylight.yangtools.yang.parser.stmt.reactor;
 
 import com.google.common.base.Preconditions;
 import com.google.common.base.Throwables;
-import com.google.common.collect.HashMultimap;
 import com.google.common.collect.Multimap;
+import com.google.common.collect.Multimaps;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.EnumMap;
 import java.util.EventListener;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
-import java.util.List;
 import java.util.Map;
 import javax.annotation.Nonnull;
 import org.opendaylight.yangtools.concepts.Identifiable;
@@ -27,6 +27,8 @@ 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.StatementDefinition;
 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
+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.ModelActionBuilder;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
@@ -72,13 +74,43 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
     private final StatementDefinitionContext<A, D, E> definition;
     private final StatementIdentifier identifier;
     private final StatementSourceReference statementDeclSource;
+
+    private final Multimap<ModelProcessingPhase, OnPhaseFinished> phaseListeners =
+            Multimaps.newListMultimap(new EnumMap<>(ModelProcessingPhase.class), () -> new ArrayList<>(1));
+
+    private final Multimap<ModelProcessingPhase, ContextMutation> phaseMutation =
+            Multimaps.newListMultimap(new EnumMap<>(ModelProcessingPhase.class), () -> new ArrayList<>(1));
+
+    private final Map<StatementIdentifier, StatementContextBase<?, ?, ?>> substatements = new LinkedHashMap<>(1);
+
+    private final Collection<StatementContextBase<?, ?, ?>> declared = new ArrayList<>(1);
+    private final Collection<StatementContextBase<?, ?, ?>> effective = new ArrayList<>(1);
+    private final Collection<StatementContextBase<?, ?, ?>> effectOfStatement = new ArrayList<>(1);
+
+    private SupportedByFeatures supportedByFeatures = SupportedByFeatures.UNDEFINED;
+    private CopyHistory copyHistory = CopyHistory.original();
+    private boolean isSupportedToBuildEffective = true;
+    private ModelProcessingPhase completedPhase = null;
+    private StatementContextBase<?, ?, ?> originalCtx;
+    private D declaredInstance;
+    private E effectiveInstance;
     private int order = 0;
 
-    private final Map<StatementIdentifier, StatementContextBase<?, ?, ?>> substatements = new LinkedHashMap<>();
+    StatementContextBase(@Nonnull final ContextBuilder<A, D, E> builder) {
+        this.definition = builder.getDefinition();
+        this.identifier = builder.createIdentifier();
+        this.statementDeclSource = builder.getStamementSource();
+    }
 
-    private final Collection<StatementContextBase<?, ?, ?>> declared = new ArrayList<>();
-    private final Collection<StatementContextBase<?, ?, ?>> effective = new ArrayList<>();
-    private final Collection<StatementContextBase<?, ?, ?>> effectOfStatement = new ArrayList<>();
+    StatementContextBase(final StatementContextBase<A, D, E> original) {
+        this.definition = Preconditions.checkNotNull(original.definition,
+                "Statement context definition cannot be null copying from: %s", original.getStatementSourceReference());
+        this.identifier = Preconditions.checkNotNull(original.identifier,
+                "Statement context identifier cannot be null copying from: %s", original.getStatementSourceReference());
+        this.statementDeclSource = Preconditions.checkNotNull(original.statementDeclSource,
+                "Statement context statementDeclSource cannot be null copying from: %s",
+                original.getStatementSourceReference());
+    }
 
     @Override
     public Collection<StatementContextBase<?, ?, ?>> getEffectOfStatement() {
@@ -89,19 +121,15 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
     public void addAsEffectOfStatement(final StatementContextBase<?, ?, ?> ctx) {
         effectOfStatement.add(ctx);
     }
+    @Override
+    public SupportedByFeatures getSupportedByFeatures() {
+        return supportedByFeatures;
+    }
 
-    private ModelProcessingPhase completedPhase;
-
-    private final Multimap<ModelProcessingPhase, OnPhaseFinished> phaseListeners = HashMultimap.create();
-    private final Multimap<ModelProcessingPhase, ContextMutation> phaseMutation = HashMultimap.create();
-
-    private D declaredInstance;
-    private E effectiveInstance;
-
-    private StatementContextBase<?, ?, ?> originalCtx;
-    private final List<TypeOfCopy> copyHistory = new ArrayList<>(1);
-
-    private boolean isSupportedToBuildEffective = true;
+    @Override
+    public void setSupportedByFeatures(final boolean isSupported) {
+        this.supportedByFeatures = isSupported ? SupportedByFeatures.SUPPORTED : SupportedByFeatures.NOT_SUPPORTED;
+    }
 
     @Override
     public boolean isSupportedToBuildEffective() {
@@ -114,18 +142,13 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
     }
 
     @Override
-    public List<TypeOfCopy> getCopyHistory() {
+    public CopyHistory getCopyHistory() {
         return copyHistory;
     }
 
     @Override
-    public void addToCopyHistory(final TypeOfCopy typeOfCopy) {
-        this.copyHistory.add(typeOfCopy);
-    }
-
-    @Override
-    public void addAllToCopyHistory(final List<TypeOfCopy> typeOfCopyList) {
-        this.copyHistory.addAll(typeOfCopyList);
+    public void appendCopyHistory(final CopyType typeOfCopy, final CopyHistory toAppend) {
+        copyHistory = copyHistory.append(typeOfCopy, toAppend);
     }
 
     @Override
@@ -158,26 +181,6 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
         this.completedPhase = completedPhase;
     }
 
-    StatementContextBase(@Nonnull final ContextBuilder<A, D, E> builder) {
-        this.definition = builder.getDefinition();
-        this.identifier = builder.createIdentifier();
-        this.statementDeclSource = builder.getStamementSource();
-        this.completedPhase = null;
-        this.copyHistory.add(TypeOfCopy.ORIGINAL);
-    }
-
-    StatementContextBase(final StatementContextBase<A, D, E> original) {
-        this.definition = Preconditions.checkNotNull(original.definition,
-                "Statement context definition cannot be null copying from: %s", original.getStatementSourceReference());
-        this.identifier = Preconditions.checkNotNull(original.identifier,
-                "Statement context identifier cannot be null copying from: %s", original.getStatementSourceReference());
-        this.statementDeclSource = Preconditions.checkNotNull(original.statementDeclSource,
-                "Statement context statementDeclSource cannot be null copying from: %s",
-                original.getStatementSourceReference());
-        this.completedPhase = null;
-        this.copyHistory.add(TypeOfCopy.ORIGINAL);
-    }
-
     /**
      * @return context of parent of statement
      */
@@ -251,9 +254,9 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
     }
 
     public void removeStatementFromEffectiveSubstatements(final StatementDefinition refineSubstatementDef) {
-        Iterator<StatementContextBase<?, ?, ?>> iterator = effective.iterator();
+        final Iterator<StatementContextBase<?, ?, ?>> iterator = effective.iterator();
         while (iterator.hasNext()) {
-            StatementContextBase<?, ?, ?> next = iterator.next();
+            final StatementContextBase<?, ?, ?> next = iterator.next();
             if (next.getPublicDefinition().equals(refineSubstatementDef)) {
                 iterator.remove();
             }
@@ -263,12 +266,11 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
     /**
      * adds effective statement to collection of substatements
      *
+     * @param substatement substatement
      * @throws IllegalStateException
      *             if added in declared phase
      * @throws NullPointerException
      *             if statement parameter is null
-     *
-     * @param substatement substatement
      */
     public void addEffectiveSubstatement(final StatementContextBase<?, ?, ?> substatement) {
 
@@ -284,12 +286,11 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
     /**
      * adds declared statement to collection of substatements
      *
+     * @param substatement substatement
      * @throws IllegalStateException
      *             if added in effective phase
      * @throws NullPointerException
      *             if statement parameter is null
-     *
-     * @param substatement substatement
      */
     public void addDeclaredSubstatement(final StatementContextBase<?, ?, ?> substatement) {
 
@@ -318,8 +319,9 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
             public StatementContextBase build() throws SourceException {
                 StatementContextBase<?, ?, ?> potential = null;
 
-                StatementDefinition stmtDef = getDefinition().getPublicView();
-                if (stmtDef != Rfc6020Mapping.AUGMENT && stmtDef != Rfc6020Mapping.DEVIATION) {
+                final StatementDefinition stmtDef = getDefinition().getPublicView();
+                if (stmtDef != Rfc6020Mapping.AUGMENT && stmtDef != Rfc6020Mapping.DEVIATION
+                        && stmtDef != Rfc6020Mapping.TYPE) {
                     potential = substatements.get(createIdentifier());
                 }
                 if (potential == null) {
@@ -398,20 +400,20 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
      *             when an error occured in source parsing
      */
     boolean tryToCompletePhase(final ModelProcessingPhase phase) {
-        Iterator<ContextMutation> openMutations = phaseMutation.get(phase).iterator();
+        final Iterator<ContextMutation> openMutations = phaseMutation.get(phase).iterator();
         boolean finished = true;
         while (openMutations.hasNext()) {
-            ContextMutation current = openMutations.next();
+            final ContextMutation current = openMutations.next();
             if (current.isFinished()) {
                 openMutations.remove();
             } else {
                 finished = false;
             }
         }
-        for (StatementContextBase<?, ?, ?> child : declared) {
+        for (final StatementContextBase<?, ?, ?> child : declared) {
             finished &= child.tryToCompletePhase(phase);
         }
-        for (StatementContextBase<?, ?, ?> child : effective) {
+        for (final StatementContextBase<?, ?, ?> child : effective) {
             finished &= child.tryToCompletePhase(phase);
         }
 
@@ -432,9 +434,9 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
      */
     private void onPhaseCompleted(final ModelProcessingPhase phase) {
         completedPhase = phase;
-        Iterator<OnPhaseFinished> listener = phaseListeners.get(completedPhase).iterator();
+        final Iterator<OnPhaseFinished> listener = phaseListeners.get(completedPhase).iterator();
         while (listener.hasNext()) {
-            OnPhaseFinished next = listener.next();
+            final OnPhaseFinished next = listener.next();
             if (next.phaseFinished(this, phase)) {
                 listener.remove();
             }
@@ -475,20 +477,20 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
 
     <K, V, N extends IdentifierNamespace<K, V>> void onNamespaceItemAddedAction(final Class<N> type, final K key,
             final OnNamespaceItemAdded listener) throws SourceException {
-        Object potential = getFromNamespace(type, key);
+        final Object potential = getFromNamespace(type, key);
         if (potential != null) {
             listener.namespaceItemAdded(this, type, key, potential);
             return;
         }
-        NamespaceBehaviour<K, V, N> behaviour = getBehaviourRegistry().getNamespaceBehaviour(type);
+        final NamespaceBehaviour<K, V, N> behaviour = getBehaviourRegistry().getNamespaceBehaviour(type);
         if (behaviour instanceof NamespaceBehaviourWithListeners) {
-            NamespaceBehaviourWithListeners<K, V, N> casted = (NamespaceBehaviourWithListeners<K, V, N>) behaviour;
+            final NamespaceBehaviourWithListeners<K, V, N> casted = (NamespaceBehaviourWithListeners<K, V, N>) behaviour;
             casted.addValueListener(new ValueAddedListener<K>(this, key) {
                 @Override
                 void onValueAdded(final Object key, final Object value) {
                     try {
                         listener.namespaceItemAdded(StatementContextBase.this, type, key, value);
-                    } catch (SourceException e) {
+                    } catch (final SourceException e) {
                         throw Throwables.propagate(e);
                     }
                 }