Merge "Fixing findbugs"
[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.ImmutableSet;
11
12 import com.google.common.collect.ImmutableMap;
13 import java.util.Collection;
14 import java.util.HashMap;
15 import java.util.HashSet;
16 import java.util.Map;
17 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
19 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
20 import java.util.Set;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
25 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.UsesNode;
27
28 public abstract class AbstractEffectiveDocumentedDataNodeContainer<A, D extends DeclaredStatement<A>>
29         extends AbstractEffectiveDocumentedNode<A, D> implements
30         DataNodeContainer {
31
32     private final ImmutableMap<QName, DataSchemaNode> childNodes;
33     private final ImmutableSet<GroupingDefinition> groupings;
34     private final ImmutableSet<UsesNode> uses;
35     private final ImmutableSet<TypeDefinition<?>> typeDefinitions;
36     private final ImmutableSet<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 HashMap<QName, DataSchemaNode>();
45         Set<GroupingDefinition> mutableGroupings = new HashSet<GroupingDefinition>();
46         Set<UsesNode> mutableUses = new HashSet<UsesNode>();
47         Set<TypeDefinition<?>> mutableTypeDefinitions = new HashSet<TypeDefinition<?>>();
48         Set<DataSchemaNode> mutablePublicChildNodes = new HashSet<DataSchemaNode>();
49
50         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
51             if (effectiveStatement instanceof DataSchemaNode) {
52                 DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
53
54                 mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
55                 mutablePublicChildNodes.add(dataSchemaNode);
56             }
57             if (effectiveStatement instanceof UsesNode) {
58                 UsesNode usesNode = (UsesNode) effectiveStatement;
59                 mutableUses.add(usesNode);
60             }
61             if (effectiveStatement instanceof TypeDefinition) {
62                 TypeDefinition<?> typeDef = (TypeDefinition<?>) effectiveStatement;
63                 mutableTypeDefinitions.add(typeDef);
64             }
65             if (effectiveStatement instanceof GroupingDefinition) {
66                 GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
67                 mutableGroupings.add(grp);
68             }
69         }
70
71         this.childNodes = ImmutableMap.copyOf(mutableChildNodes);
72         this.groupings = ImmutableSet.copyOf(mutableGroupings);
73         this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes);
74         this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions);
75         this.uses = ImmutableSet.copyOf(mutableUses);
76     }
77
78     @Override
79     public final Set<TypeDefinition<?>> getTypeDefinitions() {
80         return typeDefinitions;
81     }
82
83     @Override
84     public final Set<DataSchemaNode> getChildNodes() {
85         return publicChildNodes;
86     }
87
88     @Override
89     public final Set<GroupingDefinition> getGroupings() {
90         return groupings;
91     }
92
93     @Override
94     public final DataSchemaNode getDataChildByName(final QName name) {
95         // Child nodes are keyed by their container name, so we can do a direct
96         // lookup
97         return childNodes.get(name);
98     }
99
100     @Override
101     public final DataSchemaNode getDataChildByName(final String name) {
102         for (DataSchemaNode node : childNodes.values()) {
103             if (node.getQName().getLocalName().equals(name)) {
104                 return node;
105             }
106         }
107         return null;
108     }
109
110     @Override
111     public Set<UsesNode> getUses() {
112         return uses;
113     }
114
115 }