*/
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.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.EnumMap;
import java.util.EventListener;
import java.util.Iterator;
-import java.util.LinkedHashMap;
-import java.util.Map;
import javax.annotation.Nonnull;
import org.opendaylight.yangtools.concepts.Identifiable;
-import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
extends NamespaceStorageSupport implements StmtContext.Mutable<A, D, E>, Identifiable<StatementIdentifier> {
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ private final class SubContextBuilder extends ContextBuilder {
+ final int childId;
+
+ SubContextBuilder(final int childId, final StatementDefinitionContext def,
+ final StatementSourceReference sourceRef) {
+ super(def, sourceRef);
+ this.childId = childId;
+ }
+
+ @Override
+ public StatementContextBase build() {
+ StatementContextBase<?, ?, ?> potential = substatements.get(childId);
+ if (potential == null) {
+ potential = new SubstatementContext(StatementContextBase.this, this);
+ substatements = substatements.put(childId, potential);
+ getDefinition().onStatementAdded(potential);
+ }
+ potential.resetLists();
+ switch (this.getStamementSource().getStatementSource()) {
+ case DECLARATION:
+ addDeclaredSubstatement(potential);
+ break;
+ case CONTEXT:
+ addEffectiveSubstatement(potential);
+ break;
+ }
+ return potential;
+ }
+ }
+
/**
* event listener when an item is added to model namespace
*/
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 StatementMap substatements = StatementMap.empty();
private SupportedByFeatures supportedByFeatures = SupportedByFeatures.UNDEFINED;
private CopyHistory copyHistory = CopyHistory.original();
@Override
public void addAsEffectOfStatement(final Collection<StatementContextBase<?, ?, ?>> ctxs) {
+ if (ctxs.isEmpty()) {
+ return;
+ }
+
if (effectOfStatement.isEmpty()) {
effectOfStatement = new ArrayList<>(ctxs.size());
}
/**
* @return root context of statement
*/
+ @Nonnull
@Override
public abstract RootStatementContext<?, ?, ?> getRoot();
/**
* @return origin of statement
*/
+ @Nonnull
@Override
public StatementSource getStatementSource() {
return statementDeclSource.getStatementSource();
/**
* @return reference of statement source
*/
+ @Nonnull
@Override
public StatementSourceReference getStatementSourceReference() {
return statementDeclSource;
return Collections.unmodifiableCollection(input);
}
+ @Nonnull
@Override
public Collection<StatementContextBase<?, ?, ?>> declaredSubstatements() {
return maybeWrap(declared);
}
- /**
- * @return collection of all substatements
- */
- @Override
- public Collection<StatementContextBase<?, ?, ?>> substatements() {
- return maybeWrap(substatements.values());
- }
-
+ @Nonnull
@Override
public Collection<StatementContextBase<?, ?, ?>> effectiveSubstatements() {
return maybeWrap(effective);
* 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);
*
* @return instance of ContextBuilder
*/
- @SuppressWarnings({ "rawtypes", "unchecked" })
- public ContextBuilder<?, ?, ?> substatementBuilder(final StatementDefinitionContext<?, ?, ?> def,
+ ContextBuilder<?, ?, ?> substatementBuilder(final int childId, final StatementDefinitionContext<?, ?, ?> def,
final StatementSourceReference ref) {
- return new ContextBuilder(def, ref) {
-
- @Override
- public StatementContextBase build() throws SourceException {
- StatementContextBase<?, ?, ?> potential = null;
-
- 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);
- }
- potential.resetLists();
- switch (this.getStamementSource().getStatementSource()) {
- case DECLARATION:
- addDeclaredSubstatement(potential);
- break;
- case CONTEXT:
- addEffectiveSubstatement(potential);
- break;
- }
- return potential;
- }
- };
+ return new SubContextBuilder(childId, def, ref);
}
/**
/**
* @see StatementSupport#getPublicView()
*/
+ @Nonnull
@Override
public StatementDefinition getPublicDefinition() {
return definition().getPublicView();
@Override
public <K, KT extends K, N extends StatementNamespace<K, ?, ?>> void addContext(final Class<N> namespace, final KT key,
final StmtContext<?, ?, ?> stmt) {
- addContextToNamespace(namespace, (K) key, stmt);
+ addContextToNamespace(namespace, 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);
}
}