ce166240878e6dec499c0674ab0cc359227f488f
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / GroupingBuilderImpl.java
1 /*
2  * Copyright (c) 2013 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.builder.impl;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableList;
12 import java.util.ArrayList;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Objects;
16 import java.util.Set;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
19 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
20 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
21 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
22 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
23 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
24 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
25 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
26
27 public final class GroupingBuilderImpl extends AbstractDocumentedDataNodeContainerBuilder implements GroupingBuilder {
28     private GroupingDefinitionImpl instance;
29     // SchemaNode args
30     private SchemaPath schemaPath;
31     // DataSchemaNode args
32     private boolean addedByUses;
33
34     public GroupingBuilderImpl(final String moduleName, final int line, final QName qname, final SchemaPath path) {
35         super(moduleName, line, qname);
36         this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
37     }
38
39     public GroupingBuilderImpl(final String moduleName, final int line, final QName qname, final SchemaPath path,
40             final GroupingDefinition base) {
41         super(moduleName, line, base.getQName(), Preconditions.checkNotNull(path, "Schema Path must not be null"), base);
42         schemaPath = path;
43         addedByUses = base.isAddedByUses();
44         addedUnknownNodes.addAll(BuilderUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path,
45                 qname));
46     }
47
48     @Override
49     public GroupingDefinition build() {
50         if (instance != null) {
51             return instance;
52         }
53         buildChildren();
54         instance = new GroupingDefinitionImpl(qname, schemaPath, this);
55         instance.addedByUses = addedByUses;
56
57         // UNKNOWN NODES
58         for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
59             unknownNodes.add(b.build());
60         }
61         instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
62
63         return instance;
64     }
65
66     @Override
67     public List<DataSchemaNodeBuilder> instantiateChildNodes(final Builder newParent) {
68         final List<DataSchemaNodeBuilder> nodes = new ArrayList<>();
69         for (DataSchemaNodeBuilder node : getChildNodeBuilders()) {
70             DataSchemaNodeBuilder copy = CopyUtils.copy(node, newParent, true);
71             BuilderUtils.setNodeAddedByUses(copy);
72             nodes.add(copy);
73         }
74         return nodes;
75     }
76
77     @Override
78     public Set<TypeDefinitionBuilder> instantiateTypedefs(final Builder newParent) {
79         final Set<TypeDefinitionBuilder> nodes = new HashSet<>();
80         for (TypeDefinitionBuilder node : getTypeDefinitionBuilders()) {
81             TypeDefinitionBuilder copy = CopyUtils.copy(node, newParent, true);
82             nodes.add(copy);
83         }
84         return nodes;
85     }
86
87     @Override
88     public Set<GroupingBuilder> instantiateGroupings(final Builder newParent) {
89         final Set<GroupingBuilder> nodes = new HashSet<>();
90         for (GroupingBuilder node : getGroupingBuilders()) {
91             GroupingBuilder copy = CopyUtils.copy(node, newParent, true);
92             copy.setAddedByUses(true);
93             for (DataSchemaNodeBuilder childNode : copy.getChildNodeBuilders()) {
94                 BuilderUtils.setNodeAddedByUses(childNode);
95             }
96             nodes.add(copy);
97         }
98         return nodes;
99     }
100
101     @Override
102     public Set<UnknownSchemaNodeBuilder> instantiateUnknownNodes(final Builder newParent) {
103         final Set<UnknownSchemaNodeBuilder> nodes = new HashSet<>();
104         for (UnknownSchemaNodeBuilder node : addedUnknownNodes) {
105             UnknownSchemaNodeBuilderImpl copy = CopyUtils.copy(node, newParent, true);
106             copy.setAddedByUses(true);
107             nodes.add(copy);
108         }
109         return nodes;
110     }
111
112     @Override
113     public SchemaPath getPath() {
114         return schemaPath;
115     }
116
117     @Override
118     public void setPath(final SchemaPath path) {
119         this.schemaPath = path;
120     }
121
122     @Override
123     public boolean isAddedByUses() {
124         return addedByUses;
125     }
126
127     @Override
128     public void setAddedByUses(final boolean addedByUses) {
129         this.addedByUses = addedByUses;
130     }
131
132     @Override
133     public String toString() {
134         return "grouping " + qname.getLocalName();
135     }
136
137     @Override
138     public int hashCode() {
139         final int prime = 31;
140         int result = 1;
141         result = prime * result + Objects.hashCode(getParent());
142         result = prime * result + Objects.hashCode(schemaPath);
143         return result;
144     }
145
146     @Override
147     public boolean equals(final Object obj) {
148         if (this == obj) {
149             return true;
150         }
151         if (obj == null) {
152             return false;
153         }
154         if (getClass() != obj.getClass()) {
155             return false;
156         }
157         if (!super.equals(obj)) {
158             return false;
159         }
160         final GroupingBuilderImpl other = (GroupingBuilderImpl) obj;
161         if (getParent() == null) {
162             if (other.getParent() != null) {
163                 return false;
164             }
165         } else if (!getParent().equals(other.getParent())) {
166             return false;
167         }
168         if (schemaPath == null) {
169             if (other.schemaPath != null) {
170                 return false;
171             }
172         } else if (!schemaPath.equals(other.schemaPath)) {
173             return false;
174         }
175         return true;
176     }
177
178     @Override
179     protected String getStatementName() {
180         return "grouping";
181     }
182
183 }