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