Fixing sonar issues 4
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / EnumerationType.java
1 /*
2  * Copyright (c) 2013 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.model.util;
9
10 import java.util.Collections;
11 import java.util.List;
12
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
15 import org.opendaylight.yangtools.yang.model.api.Status;
16 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
18
19 /**
20  * The <code>default</code> implementation of Enumertaion Type Definition
21  * interface.
22  * 
23  * @see EnumTypeDefinition
24  */
25 public final class EnumerationType implements EnumTypeDefinition {
26     private final QName name = BaseTypes.constructQName("enumeration");
27     private final SchemaPath path;
28     private static final String DESCRIPTION = "The enumeration built-in type represents values from a set of assigned names.";
29     private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.6";
30
31     private final EnumPair defaultEnum;
32     private final List<EnumPair> enums;
33     private static final String UNITS = "";
34     private final EnumTypeDefinition baseType;
35
36     public EnumerationType(final SchemaPath path, final List<EnumPair> enums) {
37         super();
38         this.path = path;
39         this.enums = Collections.unmodifiableList(enums);
40         this.defaultEnum = null;
41         this.baseType = this;
42     }
43
44     public EnumerationType(final SchemaPath path, final EnumPair defaultEnum, final List<EnumPair> enums) {
45         super();
46         this.path = path;
47         this.baseType = this;
48         this.defaultEnum = defaultEnum;
49         this.enums = Collections.unmodifiableList(enums);
50     }
51
52     /*
53      * (non-Javadoc)
54      * 
55      * @see
56      * org.opendaylight.yangtools.yang.model.api.TypeDefinition#getBaseType()
57      */
58     @Override
59     public EnumTypeDefinition getBaseType() {
60         return baseType;
61     }
62
63     /*
64      * (non-Javadoc)
65      * 
66      * @see org.opendaylight.yangtools.yang.model.api.TypeDefinition#getUnits()
67      */
68     @Override
69     public String getUnits() {
70         return UNITS;
71     }
72
73     /*
74      * (non-Javadoc)
75      * 
76      * @see
77      * org.opendaylight.yangtools.yang.model.api.TypeDefinition#getDefaultValue
78      * ()
79      */
80     @Override
81     public Object getDefaultValue() {
82         return defaultEnum;
83     }
84
85     /*
86      * (non-Javadoc)
87      * 
88      * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getQName()
89      */
90     @Override
91     public QName getQName() {
92         return name;
93     }
94
95     /*
96      * (non-Javadoc)
97      * 
98      * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getPath()
99      */
100     @Override
101     public SchemaPath getPath() {
102         return path;
103     }
104
105     /*
106      * (non-Javadoc)
107      * 
108      * @see
109      * org.opendaylight.yangtools.yang.model.api.SchemaNode#getDescription()
110      */
111     @Override
112     public String getDescription() {
113         return DESCRIPTION;
114     }
115
116     /*
117      * (non-Javadoc)
118      * 
119      * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getReference()
120      */
121     @Override
122     public String getReference() {
123         return REFERENCE;
124     }
125
126     /*
127      * (non-Javadoc)
128      * 
129      * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getStatus()
130      */
131     @Override
132     public Status getStatus() {
133         return Status.CURRENT;
134     }
135
136     /*
137      * (non-Javadoc)
138      * 
139      * @see
140      * org.opendaylight.yangtools.yang.model.base.type.api.EnumTypeDefinition
141      * #getValues()
142      */
143     @Override
144     public List<EnumPair> getValues() {
145         return enums;
146     }
147
148     @Override
149     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
150         return Collections.emptyList();
151     }
152
153     @Override
154     public int hashCode() {
155         final int prime = 31;
156         int result = 1;
157         result = prime * result + ((defaultEnum == null) ? 0 : defaultEnum.hashCode());
158         result = prime * result + ((enums == null) ? 0 : enums.hashCode());
159         result = prime * result + ((name == null) ? 0 : name.hashCode());
160         result = prime * result + ((path == null) ? 0 : path.hashCode());
161         return result;
162     }
163
164     @Override
165     public boolean equals(Object obj) {
166         if (this == obj) {
167             return true;
168         }
169         if (obj == null) {
170             return false;
171         }
172         if (getClass() != obj.getClass()) {
173             return false;
174         }
175         EnumerationType other = (EnumerationType) obj;
176         if (defaultEnum == null) {
177             if (other.defaultEnum != null) {
178                 return false;
179             }
180         } else if (!defaultEnum.equals(other.defaultEnum)) {
181             return false;
182         }
183         if (enums == null) {
184             if (other.enums != null) {
185                 return false;
186             }
187         } else if (!enums.equals(other.enums)) {
188             return false;
189         }
190         if (name == null) {
191             if (other.name != null) {
192                 return false;
193             }
194         } else if (!name.equals(other.name)) {
195             return false;
196         }
197         if (path == null) {
198             if (other.path != null) {
199                 return false;
200             }
201         } else if (!path.equals(other.path)) {
202             return false;
203         }
204         return true;
205     }
206
207     @Override
208     public String toString() {
209         StringBuilder builder = new StringBuilder();
210         builder.append("EnumerationType [name=");
211         builder.append(name);
212         builder.append(", path=");
213         builder.append(path);
214         builder.append(", description=");
215         builder.append(DESCRIPTION);
216         builder.append(", reference=");
217         builder.append(REFERENCE);
218         builder.append(", defaultEnum=");
219         builder.append(defaultEnum);
220         builder.append(", enums=");
221         builder.append(enums);
222         builder.append(", units=");
223         builder.append(UNITS);
224         builder.append("]");
225         return builder.toString();
226     }
227 }