Introduce formatting methods for SourceException
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / AbstractEffectiveDocumentedDataNodeContainer.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
9
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;
16 import java.util.Map;
17 import java.util.Set;
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;
27
28 abstract class AbstractEffectiveDocumentedDataNodeContainer<A, D extends DeclaredStatement<A>>
29         extends AbstractEffectiveDocumentedNode<A, D> implements
30         DataNodeContainer {
31
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;
37
38     protected AbstractEffectiveDocumentedDataNodeContainer(
39             final StmtContext<A, D, ?> ctx) {
40         super(ctx);
41
42         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
43
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<>();
49
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);
56                 } else {
57                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
58                 }
59             }
60             if (effectiveStatement instanceof UsesNode) {
61                 UsesNode usesNode = (UsesNode) effectiveStatement;
62                 if (!mutableUses.contains(usesNode)) {
63                     mutableUses.add(usesNode);
64                 } else {
65                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
66                 }
67             }
68             if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
69                 TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
70                 TypeDefinition<?> type = typeDef.getTypeDefinition();
71                 if (!mutableTypeDefinitions.contains(type)) {
72                     mutableTypeDefinitions.add(type);
73                 } else {
74                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
75                 }
76             }
77             if (effectiveStatement instanceof GroupingDefinition) {
78                 GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
79                 if (!mutableGroupings.contains(grp)) {
80                     mutableGroupings.add(grp);
81                 } else {
82                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement);
83                 }
84             }
85         }
86
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);
92     }
93
94     @Override
95     public final Set<TypeDefinition<?>> getTypeDefinitions() {
96         return typeDefinitions;
97     }
98
99     @Override
100     public final Set<DataSchemaNode> getChildNodes() {
101         return publicChildNodes;
102     }
103
104     @Override
105     public final Set<GroupingDefinition> getGroupings() {
106         return groupings;
107     }
108
109     @Override
110     public final DataSchemaNode getDataChildByName(final QName name) {
111         // Child nodes are keyed by their container name, so we can do a direct
112         // lookup
113         return childNodes.get(name);
114     }
115
116     @Override
117     public final DataSchemaNode getDataChildByName(final String name) {
118         for (DataSchemaNode node : childNodes.values()) {
119             if (node.getQName().getLocalName().equals(name)) {
120                 return node;
121             }
122         }
123         return null;
124     }
125
126     @Override
127     public Set<UsesNode> getUses() {
128         return uses;
129     }
130
131 }