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