Merge "Bug 2361: Added prototype implementation using new SPI."
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / ChoiceNodeImpl.java
1 package org.opendaylight.yangtools.yang.parser.builder.impl;
2
3 import com.google.common.base.Optional;
4 import com.google.common.collect.ImmutableList;
5 import com.google.common.collect.ImmutableSet;
6 import java.util.List;
7 import java.util.Set;
8 import org.opendaylight.yangtools.yang.common.QName;
9 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
10 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
11 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
12 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
13 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
14 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
16 import org.opendaylight.yangtools.yang.model.api.Status;
17 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
18
19 // FIXME: BUG-1513: remove ChoiceNode in Beryllium timeframe
20 final class ChoiceNodeImpl implements ChoiceNode, ChoiceSchemaNode, DerivableSchemaNode {
21     private final QName qname;
22     private final SchemaPath path;
23     String description;
24     String reference;
25     Status status;
26     boolean augmenting;
27     boolean addedByUses;
28     ChoiceSchemaNode original;
29     boolean configuration;
30     ConstraintDefinition constraints;
31     ImmutableSet<ChoiceCaseNode> cases;
32     ImmutableSet<AugmentationSchema> augmentations;
33     ImmutableList<UnknownSchemaNode> unknownNodes;
34     String defaultCase;
35
36     ChoiceNodeImpl(final QName qname, final SchemaPath path) {
37         this.qname = qname;
38         this.path = path;
39     }
40
41     @Override
42     public QName getQName() {
43         return qname;
44     }
45
46     @Override
47     public SchemaPath getPath() {
48         return path;
49     }
50
51     @Override
52     public String getDescription() {
53         return description;
54     }
55
56     @Override
57     public String getReference() {
58         return reference;
59     }
60
61     @Override
62     public Status getStatus() {
63         return status;
64     }
65
66     @Override
67     public boolean isAugmenting() {
68         return augmenting;
69     }
70
71     @Override
72     public boolean isAddedByUses() {
73         return addedByUses;
74     }
75
76     @Override
77     public Optional<ChoiceSchemaNode> getOriginal() {
78         return Optional.fromNullable(original);
79     }
80
81     @Override
82     public boolean isConfiguration() {
83         return configuration;
84     }
85
86     @Override
87     public ConstraintDefinition getConstraints() {
88         return constraints;
89     }
90
91     @Override
92     public Set<AugmentationSchema> getAvailableAugmentations() {
93         return augmentations;
94     }
95
96     @Override
97     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
98         return unknownNodes;
99     }
100
101     @Override
102     public Set<ChoiceCaseNode> getCases() {
103         return cases;
104     }
105
106     @Override
107     public ChoiceCaseNode getCaseNodeByName(final QName name) {
108         if (name == null) {
109             throw new IllegalArgumentException("Choice Case QName cannot be NULL!");
110         }
111         for (final ChoiceCaseNode caseNode : cases) {
112             if (caseNode != null && name.equals(caseNode.getQName())) {
113                 return caseNode;
114             }
115         }
116         return null;
117     }
118
119     @Override
120     public ChoiceCaseNode getCaseNodeByName(final String name) {
121         if (name == null) {
122             throw new IllegalArgumentException("Choice Case string Name cannot be NULL!");
123         }
124         for (final ChoiceCaseNode caseNode : cases) {
125             if (caseNode != null && (caseNode.getQName() != null)
126                     && name.equals(caseNode.getQName().getLocalName())) {
127                 return caseNode;
128             }
129         }
130         return null;
131     }
132
133     @Override
134     public String getDefaultCase() {
135         return defaultCase;
136     }
137
138     @Override
139     public int hashCode() {
140         final int prime = 31;
141         int result = 1;
142         result = prime * result + ((qname == null) ? 0 : qname.hashCode());
143         result = prime * result + ((path == null) ? 0 : path.hashCode());
144         return result;
145     }
146
147     @Override
148     public boolean equals(final Object obj) {
149         if (this == obj) {
150             return true;
151         }
152         if (obj == null) {
153             return false;
154         }
155         if (getClass() != obj.getClass()) {
156             return false;
157         }
158         ChoiceNodeImpl other = (ChoiceNodeImpl) obj;
159         if (qname == null) {
160             if (other.qname != null) {
161                 return false;
162             }
163         } else if (!qname.equals(other.qname)) {
164             return false;
165         }
166         if (path == null) {
167             if (other.path != null) {
168                 return false;
169             }
170         } else if (!path.equals(other.path)) {
171             return false;
172         }
173         return true;
174     }
175
176     @Override
177     public String toString() {
178         StringBuilder sb = new StringBuilder(ChoiceNodeImpl.class.getSimpleName());
179         sb.append("[");
180         sb.append("qname=").append(qname);
181         sb.append("]");
182         return sb.toString();
183     }
184
185 }