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 final DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
53 if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
54 mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
55 mutablePublicChildNodes.add(dataSchemaNode);
57 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
60 if (effectiveStatement instanceof UsesNode) {
61 UsesNode usesNode = (UsesNode) effectiveStatement;
62 if (!mutableUses.contains(usesNode)) {
63 mutableUses.add(usesNode);
65 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
68 if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
69 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
70 TypeDefinition<?> type = typeDef.getTypeDefinition();
71 if (!mutableTypeDefinitions.contains(type)) {
72 mutableTypeDefinitions.add(type);
74 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
77 if (effectiveStatement instanceof GroupingDefinition) {
78 GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
79 if (!mutableGroupings.contains(grp)) {
80 mutableGroupings.add(grp);
82 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
87 this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
88 this.groupings = ImmutableSet.copyOf(mutableGroupings);
89 this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
90 this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
91 this.uses = ImmutableSet.copyOf(mutableUses);
95 public final Set<TypeDefinition<?>> getTypeDefinitions() {
96 return typeDefinitions;
100 public final Set<DataSchemaNode> getChildNodes() {
101 return publicChildNodes;
105 public final Set<GroupingDefinition> getGroupings() {
110 public final DataSchemaNode getDataChildByName(final QName name) {
111 // Child nodes are keyed by their container name, so we can do a direct
113 return childNodes.get(name);
117 public final DataSchemaNode getDataChildByName(final String name) {
118 for (DataSchemaNode node : childNodes.values()) {
119 if (node.getQName().getLocalName().equals(name)) {
127 public Set<UsesNode> getUses() {