2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
10 import java.util.Collection;
11 import java.util.HashSet;
12 import java.util.LinkedList;
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;
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;
32 public class ChoiceEffectiveStatementImpl extends AbstractEffectiveDocumentedNode<QName, ChoiceStatement> implements ChoiceSchemaNode, DerivableSchemaNode {
33 private final QName qname;
34 private final SchemaPath path;
38 ChoiceSchemaNode original;
39 boolean configuration;
40 ConstraintDefinition constraints;
43 ImmutableSet<ChoiceCaseNode> cases;
44 ImmutableSet<AugmentationSchema> augmentations;
45 ImmutableList<UnknownSchemaNode> unknownNodes;
47 public ChoiceEffectiveStatementImpl(StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
50 this.qname = ctx.getStatementArgument();
51 this.path = Utils.getSchemaPath(ctx);
52 //:TODO init other fields
54 initSubstatementCollections();
58 private void initSubstatementCollections() {
59 Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
61 LinkedList<UnknownSchemaNode> unknownNodes = new LinkedList<UnknownSchemaNode>();
62 HashSet<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
63 HashSet<ChoiceCaseNode> cases = new HashSet<ChoiceCaseNode>();
65 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
66 if (effectiveStatement instanceof UnknownSchemaNode) {
67 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
68 unknownNodes.add(unknownNode);
70 if (effectiveStatement instanceof AugmentationSchema) {
71 AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
72 augmentations.add(augmentationSchema);
74 if (effectiveStatement instanceof ChoiceCaseNode) {
75 ChoiceCaseNode choiceCaseNode = (ChoiceCaseNode) effectiveStatement;
76 cases.add(choiceCaseNode);
80 this.unknownNodes = ImmutableList.copyOf(unknownNodes);
81 this.augmentations = ImmutableSet.copyOf(augmentations);
82 this.cases = ImmutableSet.copyOf(cases);
86 public QName getQName() {
91 public SchemaPath getPath() {
96 public boolean isAugmenting() {
101 public boolean isAddedByUses() {
106 public Optional<ChoiceSchemaNode> getOriginal() {
107 return Optional.fromNullable(original);
111 public boolean isConfiguration() {
112 return configuration;
116 public ConstraintDefinition getConstraints() {
121 public Set<AugmentationSchema> getAvailableAugmentations() {
122 return augmentations;
126 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
131 public Set<ChoiceCaseNode> getCases() {
136 public ChoiceCaseNode getCaseNodeByName(final QName name) {
138 throw new IllegalArgumentException("Choice Case QName cannot be NULL!");
140 for (final ChoiceCaseNode caseNode : cases) {
141 if (caseNode != null && name.equals(caseNode.getQName())) {
149 public ChoiceCaseNode getCaseNodeByName(final String name) {
151 throw new IllegalArgumentException("Choice Case string Name cannot be NULL!");
153 for (final ChoiceCaseNode caseNode : cases) {
154 if (caseNode != null && (caseNode.getQName() != null)
155 && name.equals(caseNode.getQName().getLocalName())) {
163 public String getDefaultCase() {
168 public int hashCode() {
169 final int prime = 31;
171 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
172 result = prime * result + ((path == null) ? 0 : path.hashCode());
177 public boolean equals(final Object obj) {
184 if (getClass() != obj.getClass()) {
187 ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
189 if (other.qname != null) {
192 } else if (!qname.equals(other.qname)) {
196 if (other.path != null) {
199 } else if (!path.equals(other.path)) {
206 public String toString() {
207 StringBuilder sb = new StringBuilder(ChoiceEffectiveStatementImpl.class.getSimpleName());
209 sb.append("qname=").append(qname);
211 return sb.toString();