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