3f472c7887941aaad386a4e7b79f9a43d96e6027
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / 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.rfc7950.stmt;
9
10 import com.google.common.collect.ImmutableSet;
11 import java.util.HashSet;
12 import java.util.LinkedHashMap;
13 import java.util.LinkedHashSet;
14 import java.util.Map;
15 import java.util.Optional;
16 import java.util.Set;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
19 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
21 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.UsesNode;
23 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
24 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
27
28 public abstract class AbstractEffectiveDocumentedDataNodeContainer<A, D extends DeclaredStatement<A>>
29         extends AbstractSchemaEffectiveDocumentedNode<A, D> implements DataNodeContainer {
30
31     private final ImmutableSet<GroupingDefinition> groupings;
32     private final ImmutableSet<UsesNode> uses;
33     private final ImmutableSet<TypeDefinition<?>> typeDefinitions;
34     private final ImmutableSet<DataSchemaNode> publicChildNodes;
35
36     protected AbstractEffectiveDocumentedDataNodeContainer(final StmtContext<A, D, ?> ctx) {
37         super(ctx);
38
39         Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
40         Set<GroupingDefinition> mutableGroupings = new HashSet<>();
41         Set<UsesNode> mutableUses = new HashSet<>();
42         Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
43         Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
44
45         for (EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
46             if (stmt instanceof DataSchemaNode) {
47                 final DataSchemaNode dataSchemaNode = (DataSchemaNode) stmt;
48                 if (mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
49                     throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, stmt);
50                 }
51
52                 mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
53                 mutablePublicChildNodes.add(dataSchemaNode);
54             }
55             if (stmt instanceof UsesNode && !mutableUses.add((UsesNode) stmt)) {
56                 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, stmt);
57             }
58             if (stmt instanceof TypedefEffectiveStatement
59                     && !mutableTypeDefinitions.add(((TypedefEffectiveStatement) stmt).getTypeDefinition())) {
60                 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, stmt);
61             }
62             if (stmt instanceof GroupingDefinition && !mutableGroupings.add((GroupingDefinition) stmt)) {
63                 throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, stmt);
64             }
65         }
66
67         this.groupings = ImmutableSet.copyOf(mutableGroupings);
68         this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
69         this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
70         this.uses = ImmutableSet.copyOf(mutableUses);
71     }
72
73     @Override
74     public final Set<TypeDefinition<?>> getTypeDefinitions() {
75         return typeDefinitions;
76     }
77
78     @Override
79     public final Set<DataSchemaNode> getChildNodes() {
80         return publicChildNodes;
81     }
82
83     @Override
84     public final Set<GroupingDefinition> getGroupings() {
85         return groupings;
86     }
87
88     @Override
89     public final Optional<DataSchemaNode> findDataChildByName(final QName name) {
90         return findDataSchemaNode(name);
91     }
92
93     @Override
94     public Set<UsesNode> getUses() {
95         return uses;
96     }
97 }