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