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