*/
package org.opendaylight.yangtools.yang.parser.stmt.reactor;
+import com.google.common.base.MoreObjects;
+import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
-import com.google.common.collect.HashMultimap;
+import com.google.common.collect.ImmutableCollection;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableMultimap;
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;
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;
* event listener when an item is added to model namespace
*/
interface OnNamespaceItemAdded extends EventListener {
-
- void namespaceItemAdded(StatementContextBase<?, ?, ?> context, Class<?> namespace, Object key, Object value)
- throws SourceException;
-
+ /**
+ * @throws SourceException
+ */
+ void namespaceItemAdded(StatementContextBase<?, ?, ?> context, Class<?> namespace, Object key, Object value);
}
/**
* event listener when a parsing {@link ModelProcessingPhase} is completed
*/
interface OnPhaseFinished extends EventListener {
-
- boolean phaseFinished(StatementContextBase<?, ?, ?> context, ModelProcessingPhase phase) throws SourceException;
-
+ /**
+ * @throws SourceException
+ */
+ boolean phaseFinished(StatementContextBase<?, ?, ?> context, ModelProcessingPhase phase);
}
/**
interface ContextMutation {
boolean isFinished();
-
}
private final StatementDefinitionContext<A, D, E> definition;
private final StatementIdentifier identifier;
private final StatementSourceReference statementDeclSource;
+
+ private Multimap<ModelProcessingPhase, OnPhaseFinished> phaseListeners = ImmutableMultimap.of();
+ private Multimap<ModelProcessingPhase, ContextMutation> phaseMutation = ImmutableMultimap.of();
+ private Map<StatementIdentifier, StatementContextBase<?, ?, ?>> substatements = ImmutableMap.of();
+ private Collection<StatementContextBase<?, ?, ?>> declared = ImmutableList.of();
+ private Collection<StatementContextBase<?, ?, ?>> effective = ImmutableList.of();
+ private Collection<StatementContextBase<?, ?, ?>> effectOfStatement = ImmutableList.of();
+
+ 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() {
@Override
public void addAsEffectOfStatement(final StatementContextBase<?, ?, ?> ctx) {
+ if (effectOfStatement.isEmpty()) {
+ effectOfStatement = new ArrayList<>(1);
+ }
effectOfStatement.add(ctx);
}
- private ModelProcessingPhase completedPhase;
-
- private final Multimap<ModelProcessingPhase, OnPhaseFinished> phaseListeners = HashMultimap.create();
- private final Multimap<ModelProcessingPhase, ContextMutation> phaseMutation = HashMultimap.create();
+ @Override
+ public void addAsEffectOfStatement(final Collection<StatementContextBase<?, ?, ?>> ctxs) {
+ if (ctxs.isEmpty()) {
+ return;
+ }
- private D declaredInstance;
- private E effectiveInstance;
+ if (effectOfStatement.isEmpty()) {
+ effectOfStatement = new ArrayList<>(ctxs.size());
+ }
+ effectOfStatement.addAll(ctxs);
+ }
- private StatementContextBase<?, ?, ?> originalCtx;
- private final List<TypeOfCopy> copyHistory = new ArrayList<>(1);
+ @Override
+ public SupportedByFeatures getSupportedByFeatures() {
+ return supportedByFeatures;
+ }
- private boolean isSupportedToBuildEffective = true;
+ @Override
+ public void setSupportedByFeatures(final boolean isSupported) {
+ this.supportedByFeatures = isSupported ? SupportedByFeatures.SUPPORTED : SupportedByFeatures.NOT_SUPPORTED;
+ }
@Override
public boolean isSupportedToBuildEffective() {
}
@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
this.completedPhase = completedPhase;
}
- StatementContextBase(@Nonnull final ContextBuilder<A, D, E> builder) throws SourceException {
- 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");
- this.identifier = Preconditions
- .checkNotNull(original.identifier, "Statement context identifier cannot be null");
- this.statementDeclSource = Preconditions.checkNotNull(original.statementDeclSource,
- "Statement context statementDeclSource cannot be null");
- this.completedPhase = null;
- this.copyHistory.add(TypeOfCopy.ORIGINAL);
- }
-
/**
* @return context of parent of statement
*/
return identifier.getArgument();
}
- /**
- * @return collection of declared substatements
- */
+ private static final <T> Collection<T> maybeWrap(final Collection<T> input) {
+ if (input instanceof ImmutableCollection) {
+ return input;
+ }
+
+ return Collections.unmodifiableCollection(input);
+ }
+
@Override
public Collection<StatementContextBase<?, ?, ?>> declaredSubstatements() {
- return Collections.unmodifiableCollection(declared);
+ return maybeWrap(declared);
}
/**
- * @return collection of substatements
+ * @return collection of all substatements
*/
@Override
public Collection<StatementContextBase<?, ?, ?>> substatements() {
- return Collections.unmodifiableCollection(substatements.values());
+ return maybeWrap(substatements.values());
}
- /**
- * @return collection of effective substatements
- */
@Override
public Collection<StatementContextBase<?, ?, ?>> effectiveSubstatements() {
- return Collections.unmodifiableCollection(effective);
+ return maybeWrap(effective);
}
public void removeStatementsFromEffectiveSubstatements(final Collection<StatementContextBase<?, ?, ?>> substatements) {
- effective.removeAll(substatements);
+ if (!effective.isEmpty()) {
+ effective.removeAll(substatements);
+ shrinkEffective();
+ }
+ }
+
+ private void shrinkEffective() {
+ if (effective.isEmpty()) {
+ effective = ImmutableList.of();
+ }
}
public void removeStatementFromEffectiveSubstatements(final StatementDefinition refineSubstatementDef) {
- Iterator<StatementContextBase<?, ?, ?>> iterator = effective.iterator();
+ if (effective.isEmpty()) {
+ return;
+ }
+
+ 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();
}
}
+
+ shrinkEffective();
}
/**
* 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) {
+ Preconditions.checkNotNull(substatement, "StatementContextBase effective substatement cannot be null at: %s",
+ getStatementSourceReference());
+ beforeAddEffectiveStatement(1);
+ effective.add(substatement);
+ }
+ /**
+ * adds effective statement to collection of substatements
+ *
+ * @param substatements substatements
+ * @throws IllegalStateException
+ * if added in declared phase
+ * @throws NullPointerException
+ * if statement parameter is null
+ */
+ public void addEffectiveSubstatements(final Collection<StatementContextBase<?, ?, ?>> substatements) {
+ if (substatements.isEmpty()) {
+ return;
+ }
+
+ substatements.forEach(Preconditions::checkNotNull);
+ beforeAddEffectiveStatement(substatements.size());
+ effective.addAll(substatements);
+ }
+
+ private void beforeAddEffectiveStatement(final int toAdd) {
final ModelProcessingPhase inProgressPhase = getRoot().getSourceContext().getInProgressPhase();
Preconditions.checkState(inProgressPhase == ModelProcessingPhase.FULL_DECLARATION
|| inProgressPhase == ModelProcessingPhase.EFFECTIVE_MODEL,
- "Effective statement cannot be added in declared phase");
+ "Effective statement cannot be added in declared phase at: %s", getStatementSourceReference());
- effective.add(Preconditions.checkNotNull(substatement,
- "StatementContextBase effective substatement cannot be null"));
+ if (effective.isEmpty()) {
+ effective = new ArrayList<>(toAdd);
+ }
}
/**
* 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) {
final ModelProcessingPhase inProgressPhase = getRoot().getSourceContext().getInProgressPhase();
Preconditions.checkState(inProgressPhase != ModelProcessingPhase.EFFECTIVE_MODEL,
- "Declared statement cannot be added in effective phase");
+ "Declared statement cannot be added in effective phase at: %s", getStatementSourceReference());
+ if (declared.isEmpty()) {
+ declared = new ArrayList<>(1);
+ }
declared.add(Preconditions.checkNotNull(substatement,
- "StatementContextBase declared substatement cannot be null"));
+ "StatementContextBase declared substatement cannot be null at: %s", getStatementSourceReference()));
}
/**
- * builds new substatement from statement definition context and statement source reference
+ * builds a new substatement from statement definition context and statement source reference
*
* @param def definition context
* @param ref source reference
public StatementContextBase build() throws SourceException {
StatementContextBase<?, ?, ?> potential = null;
- if (getDefinition().getPublicView() != Rfc6020Mapping.AUGMENT) {
+ final StatementDefinition stmtDef = getDefinition().getPublicView();
+ if (stmtDef != Rfc6020Mapping.AUGMENT && stmtDef != Rfc6020Mapping.DEVIATION
+ && stmtDef != Rfc6020Mapping.TYPE) {
potential = substatements.get(createIdentifier());
}
if (potential == null) {
potential = new SubstatementContext(StatementContextBase.this, this);
+ if (substatements.isEmpty()) {
+ substatements = new LinkedHashMap<>(1);
+ }
substatements.put(createIdentifier(), potential);
getDefinition().onStatementAdded(potential);
}
final SourceSpecificContext sourceContext = getRoot().getSourceContext();
Preconditions.checkState(sourceContext.getInProgressPhase() != ModelProcessingPhase.EFFECTIVE_MODEL,
- "Declared statements list cannot be cleared in effective phase");
+ "Declared statements list cannot be cleared in effective phase at: %s", getStatementSourceReference());
- declared.clear();
+ declared = ImmutableList.of();
}
/**
* @throws SourceException
* when an error occured in source parsing
*/
- boolean tryToCompletePhase(final ModelProcessingPhase phase) throws SourceException {
- Iterator<ContextMutation> openMutations = phaseMutation.get(phase).iterator();
+ boolean tryToCompletePhase(final ModelProcessingPhase phase) {
+
boolean finished = true;
- while (openMutations.hasNext()) {
- ContextMutation current = openMutations.next();
- if (current.isFinished()) {
- openMutations.remove();
- } else {
- finished = false;
+ final Collection<ContextMutation> openMutations = phaseMutation.get(phase);
+ if (!openMutations.isEmpty()) {
+ final Iterator<ContextMutation> it = openMutations.iterator();
+ while (it.hasNext()) {
+ final ContextMutation current = it.next();
+ if (current.isFinished()) {
+ it.remove();
+ } else {
+ finished = false;
+ }
+ }
+
+ if (openMutations.isEmpty()) {
+ phaseMutation.removeAll(phase);
+ if (phaseMutation.isEmpty()) {
+ phaseMutation = ImmutableMultimap.of();
+ }
}
}
- 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);
}
* @throws SourceException
* when an error occured in source parsing
*/
- private void onPhaseCompleted(final ModelProcessingPhase phase) throws SourceException {
+ private void onPhaseCompleted(final ModelProcessingPhase phase) {
completedPhase = phase;
- Iterator<OnPhaseFinished> listener = phaseListeners.get(completedPhase).iterator();
+
+ final Collection<OnPhaseFinished> listeners = phaseListeners.get(phase);
+ if (listeners.isEmpty()) {
+ return;
+ }
+
+ final Iterator<OnPhaseFinished> listener = listeners.iterator();
while (listener.hasNext()) {
- OnPhaseFinished next = listener.next();
+ final OnPhaseFinished next = listener.next();
if (next.phaseFinished(this, phase)) {
listener.remove();
}
}
+
+ if (listeners.isEmpty()) {
+ phaseListeners.removeAll(phase);
+ if (phaseListeners.isEmpty()) {
+ phaseListeners = ImmutableMultimap.of();
+ }
+ }
}
/**
* @param ref
* @throws SourceException
*/
- void endDeclared(final StatementSourceReference ref, final ModelProcessingPhase phase) throws SourceException {
+ void endDeclared(final StatementSourceReference ref, final ModelProcessingPhase phase) {
definition().onDeclarationFinished(this, phase);
}
<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);
}
}
return getRoot().getSourceContext().newInferenceAction(phase);
}
+ private static <T> Multimap<ModelProcessingPhase, T> newMultimap() {
+ return Multimaps.newListMultimap(new EnumMap<>(ModelProcessingPhase.class), () -> new ArrayList<>(1));
+ }
+
/**
* adds {@link OnPhaseFinished} listener for a {@link ModelProcessingPhase} end
*
* @throws SourceException
*/
- void addPhaseCompletedListener(final ModelProcessingPhase phase, final OnPhaseFinished listener) throws SourceException {
+ void addPhaseCompletedListener(final ModelProcessingPhase phase, final OnPhaseFinished listener) {
- Preconditions.checkNotNull(phase, "Statement context processing phase cannot be null");
- Preconditions.checkNotNull(listener, "Statement context phase listener cannot be null");
+ Preconditions.checkNotNull(phase, "Statement context processing phase cannot be null at: %s",
+ getStatementSourceReference());
+ Preconditions.checkNotNull(listener, "Statement context phase listener cannot be null at: %s",
+ getStatementSourceReference());
ModelProcessingPhase finishedPhase = completedPhase;
while (finishedPhase != null) {
}
finishedPhase = finishedPhase.getPreviousPhase();
}
+ if (phaseListeners.isEmpty()) {
+ phaseListeners = newMultimap();
+ }
+
phaseListeners.put(phase, listener);
}
ModelProcessingPhase finishedPhase = completedPhase;
while (finishedPhase != null) {
if (phase.equals(finishedPhase)) {
- throw new IllegalStateException("Mutation registered after phase was completed.");
+ throw new IllegalStateException("Mutation registered after phase was completed at: " +
+ getStatementSourceReference());
}
finishedPhase = finishedPhase.getPreviousPhase();
}
+
+ if (phaseMutation.isEmpty()) {
+ phaseMutation = newMultimap();
+ }
phaseMutation.put(phase, mutation);
}
final StmtContext<?, ?, ?> stmt) {
addContextToNamespace(namespace, (K) key, stmt);
}
+
+ @Override
+ public final String toString() {
+ return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
+ }
+
+ protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
+ return toStringHelper.add("definition", definition).add("id", identifier);
+ }
}