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