From bfb308e0cff0991c40321123c814ae71db1f9bae Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Mon, 2 Nov 2015 15:28:13 +0100 Subject: [PATCH] Switch StatementContextBase to use ArrayList This inlines the history initialization and uses an ArrayList instead of a LinkedList. Change-Id: I5980e6525f0b00c46b1f1f87dad941685042abe9 Signed-off-by: Robert Varga --- .../stmt/reactor/StatementContextBase.java | 78 +++++++++---------- 1 file changed, 37 insertions(+), 41 deletions(-) diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java index 5e05a47580..91301d0204 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java @@ -17,7 +17,6 @@ import java.util.Collections; import java.util.EventListener; import java.util.Iterator; import java.util.LinkedHashMap; -import java.util.LinkedList; import java.util.List; import java.util.Map; import javax.annotation.Nonnull; @@ -75,30 +74,32 @@ public abstract class StatementContextBase, E private final StatementSourceReference statementDeclSource; private int order = 0; - private Map> substatements = new LinkedHashMap<>(); + private final Map> substatements = new LinkedHashMap<>(); - private Collection> declared = new ArrayList<>(); - private Collection> effective = new ArrayList<>(); - private Collection> effectOfStatement = new ArrayList<>(); + private final Collection> declared = new ArrayList<>(); + private final Collection> effective = new ArrayList<>(); + private final Collection> effectOfStatement = new ArrayList<>(); + @Override public Collection> getEffectOfStatement() { return effectOfStatement; } - public void addAsEffectOfStatement(StatementContextBase ctx) { + @Override + public void addAsEffectOfStatement(final StatementContextBase ctx) { effectOfStatement.add(ctx); } private ModelProcessingPhase completedPhase; - private Multimap phaseListeners = HashMultimap.create(); - private Multimap phaseMutation = HashMultimap.create(); + private final Multimap phaseListeners = HashMultimap.create(); + private final Multimap phaseMutation = HashMultimap.create(); private D declaredInstance; private E effectiveInstance; private StatementContextBase originalCtx; - private List copyHistory; + private final List copyHistory = new ArrayList<>(1); private boolean isSupportedToBuildEffective = true; @@ -108,7 +109,7 @@ public abstract class StatementContextBase, E } @Override - public void setIsSupportedToBuildEffective(boolean isSupportedToBuildEffective) { + public void setIsSupportedToBuildEffective(final boolean isSupportedToBuildEffective) { this.isSupportedToBuildEffective = isSupportedToBuildEffective; } @@ -118,12 +119,12 @@ public abstract class StatementContextBase, E } @Override - public void addToCopyHistory(TypeOfCopy typeOfCopy) { + public void addToCopyHistory(final TypeOfCopy typeOfCopy) { this.copyHistory.add(typeOfCopy); } @Override - public void addAllToCopyHistory(List typeOfCopyList) { + public void addAllToCopyHistory(final List typeOfCopyList) { this.copyHistory.addAll(typeOfCopyList); } @@ -133,12 +134,12 @@ public abstract class StatementContextBase, E } @Override - public void setOriginalCtx(StatementContextBase originalCtx) { + public void setOriginalCtx(final StatementContextBase originalCtx) { this.originalCtx = originalCtx; } @Override - public void setOrder(int order) { + public void setOrder(final int order) { this.order = order; } @@ -153,19 +154,19 @@ public abstract class StatementContextBase, E } @Override - public void setCompletedPhase(ModelProcessingPhase completedPhase) { + public void setCompletedPhase(final ModelProcessingPhase completedPhase) { this.completedPhase = completedPhase; } - StatementContextBase(@Nonnull ContextBuilder builder) throws SourceException { + StatementContextBase(@Nonnull final ContextBuilder builder) throws SourceException { this.definition = builder.getDefinition(); this.identifier = builder.createIdentifier(); this.statementDeclSource = builder.getStamementSource(); this.completedPhase = null; - initCopyHistory(); + this.copyHistory.add(TypeOfCopy.ORIGINAL); } - StatementContextBase(StatementContextBase original) { + StatementContextBase(final StatementContextBase original) { this.definition = Preconditions .checkNotNull(original.definition, "Statement context definition cannot be null"); this.identifier = Preconditions @@ -173,11 +174,6 @@ public abstract class StatementContextBase, E this.statementDeclSource = Preconditions.checkNotNull(original.statementDeclSource, "Statement context statementDeclSource cannot be null"); this.completedPhase = null; - initCopyHistory(); - } - - private void initCopyHistory() { - this.copyHistory = new LinkedList<>(); this.copyHistory.add(TypeOfCopy.ORIGINAL); } @@ -249,11 +245,11 @@ public abstract class StatementContextBase, E return Collections.unmodifiableCollection(effective); } - public void removeStatementsFromEffectiveSubstatements(Collection> substatements) { + public void removeStatementsFromEffectiveSubstatements(final Collection> substatements) { effective.removeAll(substatements); } - public void removeStatementFromEffectiveSubstatements(StatementDefinition refineSubstatementDef) { + public void removeStatementFromEffectiveSubstatements(final StatementDefinition refineSubstatementDef) { Iterator> iterator = effective.iterator(); while (iterator.hasNext()) { StatementContextBase next = iterator.next(); @@ -273,7 +269,7 @@ public abstract class StatementContextBase, E * * @param substatement substatement */ - public void addEffectiveSubstatement(StatementContextBase substatement) { + public void addEffectiveSubstatement(final StatementContextBase substatement) { final ModelProcessingPhase inProgressPhase = getRoot().getSourceContext().getInProgressPhase(); Preconditions.checkState(inProgressPhase == ModelProcessingPhase.FULL_DECLARATION @@ -294,7 +290,7 @@ public abstract class StatementContextBase, E * * @param substatement substatement */ - public void addDeclaredSubstatement(StatementContextBase substatement) { + public void addDeclaredSubstatement(final StatementContextBase substatement) { final ModelProcessingPhase inProgressPhase = getRoot().getSourceContext().getInProgressPhase(); Preconditions.checkState(inProgressPhase != ModelProcessingPhase.EFFECTIVE_MODEL, @@ -313,8 +309,8 @@ public abstract class StatementContextBase, E * @return instance of ContextBuilder */ @SuppressWarnings({ "rawtypes", "unchecked" }) - public ContextBuilder substatementBuilder(StatementDefinitionContext def, - StatementSourceReference ref) { + public ContextBuilder substatementBuilder(final StatementDefinitionContext def, + final StatementSourceReference ref) { return new ContextBuilder(def, ref) { @Override @@ -399,7 +395,7 @@ public abstract class StatementContextBase, E * @throws SourceException * when an error occured in source parsing */ - boolean tryToCompletePhase(ModelProcessingPhase phase) throws SourceException { + boolean tryToCompletePhase(final ModelProcessingPhase phase) throws SourceException { Iterator openMutations = phaseMutation.get(phase).iterator(); boolean finished = true; while (openMutations.hasNext()) { @@ -432,7 +428,7 @@ public abstract class StatementContextBase, E * @throws SourceException * when an error occured in source parsing */ - private void onPhaseCompleted(ModelProcessingPhase phase) throws SourceException { + private void onPhaseCompleted(final ModelProcessingPhase phase) throws SourceException { completedPhase = phase; Iterator listener = phaseListeners.get(completedPhase).iterator(); while (listener.hasNext()) { @@ -449,7 +445,7 @@ public abstract class StatementContextBase, E * @param ref * @throws SourceException */ - void endDeclared(StatementSourceReference ref, ModelProcessingPhase phase) throws SourceException { + void endDeclared(final StatementSourceReference ref, final ModelProcessingPhase phase) throws SourceException { definition().onDeclarationFinished(this, phase); } @@ -461,7 +457,7 @@ public abstract class StatementContextBase, E } @Override - protected void checkLocalNamespaceAllowed(Class> type) { + protected void checkLocalNamespaceAllowed(final Class> type) { definition().checkNamespaceAllowed(type); } @@ -471,11 +467,11 @@ public abstract class StatementContextBase, E * @throws SourceException instance of SourceException */ @Override - protected > void onNamespaceElementAdded(Class type, K key, V value) { + protected > void onNamespaceElementAdded(final Class type, final K key, final V value) { // definition().onNamespaceElementAdded(this, type, key, value); } - > void onNamespaceItemAddedAction(final Class type, K key, + > void onNamespaceItemAddedAction(final Class type, final K key, final OnNamespaceItemAdded listener) throws SourceException { Object potential = getFromNamespace(type, key); if (potential != null) { @@ -487,7 +483,7 @@ public abstract class StatementContextBase, E NamespaceBehaviourWithListeners casted = (NamespaceBehaviourWithListeners) behaviour; casted.addValueListener(new ValueAddedListener(this, key) { @Override - void onValueAdded(Object key, Object value) { + void onValueAdded(final Object key, final Object value) { try { listener.namespaceItemAdded(StatementContextBase.this, type, key, value); } catch (SourceException e) { @@ -510,7 +506,7 @@ public abstract class StatementContextBase, E * @return new {@link ModelActionBuilder} for the phase */ @Override - public ModelActionBuilder newInferenceAction(ModelProcessingPhase phase) { + public ModelActionBuilder newInferenceAction(final ModelProcessingPhase phase) { return getRoot().getSourceContext().newInferenceAction(phase); } @@ -519,7 +515,7 @@ public abstract class StatementContextBase, E * * @throws SourceException */ - void addPhaseCompletedListener(ModelProcessingPhase phase, OnPhaseFinished listener) throws SourceException { + void addPhaseCompletedListener(final ModelProcessingPhase phase, final OnPhaseFinished listener) throws SourceException { Preconditions.checkNotNull(phase, "Statement context processing phase cannot be null"); Preconditions.checkNotNull(listener, "Statement context phase listener cannot be null"); @@ -541,7 +537,7 @@ public abstract class StatementContextBase, E * @throws IllegalStateException * when the mutation was registered after phase was completed */ - void addMutation(ModelProcessingPhase phase, ContextMutation mutation) { + void addMutation(final ModelProcessingPhase phase, final ContextMutation mutation) { ModelProcessingPhase finishedPhase = completedPhase; while (finishedPhase != null) { if (phase.equals(finishedPhase)) { @@ -563,8 +559,8 @@ public abstract class StatementContextBase, E * to be added to namespace map */ @Override - public > void addContext(Class namespace, KT key, - StmtContext stmt) { + public > void addContext(final Class namespace, final KT key, + final StmtContext stmt) { addContextToNamespace(namespace, (K) key, stmt); } } -- 2.36.6