2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
10 import com.google.common.collect.ImmutableMap;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.Collection;
13 import java.util.HashSet;
14 import java.util.LinkedHashMap;
15 import java.util.LinkedHashSet;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
22 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.UsesNode;
24 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
25 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
28 abstract class AbstractEffectiveDocumentedDataNodeContainer<A, D extends DeclaredStatement<A>>
29 extends AbstractEffectiveDocumentedNode<A, D> implements
32 private final Map<QName, DataSchemaNode> childNodes;
33 private final Set<GroupingDefinition> groupings;
34 private final Set<UsesNode> uses;
35 private final Set<TypeDefinition<?>> typeDefinitions;
36 private final Set<DataSchemaNode> publicChildNodes;
38 protected AbstractEffectiveDocumentedDataNodeContainer(
39 final StmtContext<A, D, ?> ctx) {
42 Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
44 Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
45 Set<GroupingDefinition> mutableGroupings = new HashSet<>();
46 Set<UsesNode> mutableUses = new HashSet<>();
47 Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
48 Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
50 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
51 if (effectiveStatement instanceof DataSchemaNode) {
52 DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
53 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
54 mutableChildNodes.put(dataSchemaNode.getQName(),
56 mutablePublicChildNodes.add(dataSchemaNode);
58 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
61 if (effectiveStatement instanceof UsesNode) {
62 UsesNode usesNode = (UsesNode) effectiveStatement;
63 if (!mutableUses.contains(usesNode)) {
64 mutableUses.add(usesNode);
66 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
69 if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
70 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
71 TypeDefinition<?> type = typeDef.getTypeDefinition();
72 if (!mutableTypeDefinitions.contains(type)) {
73 mutableTypeDefinitions.add(type);
75 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
78 if (effectiveStatement instanceof GroupingDefinition) {
79 GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
80 if (!mutableGroupings.contains(grp)) {
81 mutableGroupings.add(grp);
83 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
88 this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
89 this.groupings = ImmutableSet.copyOf(mutableGroupings);
90 this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
91 this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
92 this.uses = ImmutableSet.copyOf(mutableUses);
96 public final Set<TypeDefinition<?>> getTypeDefinitions() {
97 return typeDefinitions;
101 public final Set<DataSchemaNode> getChildNodes() {
102 return publicChildNodes;
106 public final Set<GroupingDefinition> getGroupings() {
111 public final DataSchemaNode getDataChildByName(final QName name) {
112 // Child nodes are keyed by their container name, so we can do a direct
114 return childNodes.get(name);
118 public final DataSchemaNode getDataChildByName(final String name) {
119 for (DataSchemaNode node : childNodes.values()) {
120 if (node.getQName().getLocalName().equals(name)) {
128 public Set<UsesNode> getUses() {