7c36706d07b3cb0aace0088468f6a7a40f085e51
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / ChoiceEffectiveStatementImpl.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 org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
11
12 import java.util.Collection;
13 import java.util.HashSet;
14 import java.util.LinkedList;
15 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
16 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
19 import com.google.common.base.Optional;
20 import com.google.common.collect.ImmutableList;
21 import com.google.common.collect.ImmutableSet;
22 import java.util.List;
23 import java.util.Set;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
26 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
27 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
29 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
31 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
32
33 public class ChoiceEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<QName, ChoiceStatement> implements ChoiceSchemaNode, DerivableSchemaNode {
34     private final QName qname;
35     private final SchemaPath path;
36
37     boolean augmenting;
38     boolean addedByUses;
39     ChoiceSchemaNode original;
40     boolean configuration;
41     ConstraintDefinition constraints;
42     String defaultCase;
43
44     ImmutableSet<ChoiceCaseNode> cases;
45     ImmutableSet<AugmentationSchema> augmentations;
46     ImmutableList<UnknownSchemaNode> unknownNodes;
47
48     public ChoiceEffectiveStatementImpl(StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
49         super(ctx);
50
51         this.qname = ctx.getStatementArgument();
52         this.path = Utils.getSchemaPath(ctx);
53         //:TODO init other fields
54
55         initSubstatementCollections();
56         initCopyType(ctx);
57     }
58
59     private void initCopyType(
60             StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
61
62         TypeOfCopy typeOfCopy = ctx.getTypeOfCopy();
63         switch (typeOfCopy) {
64         case ADDED_BY_AUGMENTATION:
65             augmenting = true;
66             original = (ChoiceSchemaNode) ctx.getOriginalCtx().buildEffective();
67             break;
68         case ADDED_BY_USES:
69             addedByUses = true;
70             original = (ChoiceSchemaNode) ctx.getOriginalCtx().buildEffective();
71             break;
72         default:
73             break;
74         }
75     }
76
77     private void initSubstatementCollections() {
78         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
79
80         List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
81         Set<AugmentationSchema> augmentationsInit = new HashSet<>();
82         Set<ChoiceCaseNode> casesInit = new HashSet<>();
83
84         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
85             if (effectiveStatement instanceof UnknownSchemaNode) {
86                 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
87                 unknownNodesInit.add(unknownNode);
88             }
89             if (effectiveStatement instanceof AugmentationSchema) {
90                 AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
91                 augmentationsInit.add(augmentationSchema);
92             }
93             if (effectiveStatement instanceof ChoiceCaseNode) {
94                 ChoiceCaseNode choiceCaseNode = (ChoiceCaseNode) effectiveStatement;
95                 casesInit.add(choiceCaseNode);
96             }
97         }
98
99         this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
100         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
101         this.cases = ImmutableSet.copyOf(casesInit);
102     }
103
104     @Override
105     public QName getQName() {
106         return qname;
107     }
108
109     @Override
110     public SchemaPath getPath() {
111         return path;
112     }
113
114     @Override
115     public boolean isAugmenting() {
116         return augmenting;
117     }
118
119     @Override
120     public boolean isAddedByUses() {
121         return addedByUses;
122     }
123
124     @Override
125     public Optional<ChoiceSchemaNode> getOriginal() {
126         return Optional.fromNullable(original);
127     }
128
129     @Override
130     public boolean isConfiguration() {
131         return configuration;
132     }
133
134     @Override
135     public ConstraintDefinition getConstraints() {
136         return constraints;
137     }
138
139     @Override
140     public Set<AugmentationSchema> getAvailableAugmentations() {
141         return augmentations;
142     }
143
144     @Override
145     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
146         return unknownNodes;
147     }
148
149     @Override
150     public Set<ChoiceCaseNode> getCases() {
151         return cases;
152     }
153
154     @Override
155     public ChoiceCaseNode getCaseNodeByName(final QName name) {
156         if (name == null) {
157             throw new IllegalArgumentException("Choice Case QName cannot be NULL!");
158         }
159         for (final ChoiceCaseNode caseNode : cases) {
160             if (caseNode != null && name.equals(caseNode.getQName())) {
161                 return caseNode;
162             }
163         }
164         return null;
165     }
166
167     @Override
168     public ChoiceCaseNode getCaseNodeByName(final String name) {
169         if (name == null) {
170             throw new IllegalArgumentException("Choice Case string Name cannot be NULL!");
171         }
172         for (final ChoiceCaseNode caseNode : cases) {
173             if (caseNode != null && (caseNode.getQName() != null)
174                     && name.equals(caseNode.getQName().getLocalName())) {
175                 return caseNode;
176             }
177         }
178         return null;
179     }
180
181     @Override
182     public String getDefaultCase() {
183         return defaultCase;
184     }
185
186     @Override
187     public int hashCode() {
188         final int prime = 31;
189         int result = 1;
190         result = prime * result + ((qname == null) ? 0 : qname.hashCode());
191         result = prime * result + ((path == null) ? 0 : path.hashCode());
192         return result;
193     }
194
195     @Override
196     public boolean equals(final Object obj) {
197         if (this == obj) {
198             return true;
199         }
200         if (obj == null) {
201             return false;
202         }
203         if (getClass() != obj.getClass()) {
204             return false;
205         }
206         ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
207         if (qname == null) {
208             if (other.qname != null) {
209                 return false;
210             }
211         } else if (!qname.equals(other.qname)) {
212             return false;
213         }
214         if (path == null) {
215             if (other.path != null) {
216                 return false;
217             }
218         } else if (!path.equals(other.path)) {
219             return false;
220         }
221         return true;
222     }
223
224     @Override
225     public String toString() {
226         StringBuilder sb = new StringBuilder(ChoiceEffectiveStatementImpl.class.getSimpleName());
227         sb.append("[");
228         sb.append("qname=").append(qname);
229         sb.append("]");
230         return sb.toString();
231     }
232
233 }