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