Merge "Bug 1113 - ietf-restconf needs to specify the version of ietf-yangtypes that...
[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.base.Optional;
20 import com.google.common.base.Preconditions;
21 import com.google.common.collect.ImmutableList;
22
23 /**
24  * The <code>default</code> implementation of Enumeration Type Definition
25  * interface.
26  *
27  * @see EnumTypeDefinition
28  */
29 public final class EnumerationType implements EnumTypeDefinition {
30     private static final String DESCRIPTION = "The enumeration built-in type represents values from a set of assigned names.";
31     private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.6";
32     private static final String UNITS = "";
33
34     private final SchemaPath path;
35     private final EnumPair defaultEnum;
36     private final List<EnumPair> enums;
37
38
39     /**
40      * Constructs EnumerationType
41      *
42      * @param path
43      * @param enums
44      * @deprecated Use {@link #create(SchemaPath, List, Optional)} instead.
45      */
46     @Deprecated
47     public EnumerationType(final SchemaPath path, final List<EnumPair> enums) {
48         this(path,enums,Optional.<EnumPair>absent());
49     }
50
51     /**
52      * Constructs EnumerationType
53      *
54      * @param path
55      * @param defaultEnum
56      * @param enums
57      * @deprecated Use {@link #create(SchemaPath, List, Optional)} instead.
58      */
59     @Deprecated
60     public EnumerationType(final SchemaPath path, final EnumPair defaultEnum, final List<EnumPair> enums) {
61         this(path,enums,Optional.fromNullable(defaultEnum));
62     }
63
64     private EnumerationType(final SchemaPath path, final List<EnumPair> enums, final Optional<EnumPair> defaultEnum) {
65         this.path = Preconditions.checkNotNull(path,"path must not be null");
66         this.enums = ImmutableList.copyOf(Preconditions.checkNotNull(enums, "enums must not be null."));
67         if(defaultEnum.isPresent()) {
68             Preconditions.checkArgument(enums.contains(defaultEnum.get()),"defaultEnum must be contained in defined enumerations.");
69             this.defaultEnum = defaultEnum.get();
70         } else {
71             this.defaultEnum = null;
72         }
73     }
74
75     /**
76      *
77      * Constructs new enumeration
78      *
79      * @param path Schema Path to definition point of this enumeration
80      * @param enums List of defined enumeration values
81      * @param defaultValue {@link Optional#of(Object)} of default value, {@link Optional#absent()} if no default value is defined.
82      *        If defaultValue is set, it must be present in provided list of enumerations.
83      *
84      */
85     public static EnumerationType create(final SchemaPath path, final List<EnumPair> enums, final Optional<EnumPair> defaultValue) {
86         return new EnumerationType(path, enums, defaultValue);
87     }
88
89     /*
90      * (non-Javadoc)
91      *
92      * @see
93      * org.opendaylight.yangtools.yang.model.api.TypeDefinition#getBaseType()
94      */
95     @Override
96     public EnumTypeDefinition getBaseType() {
97         return null;
98     }
99
100     /*
101      * (non-Javadoc)
102      *
103      * @see org.opendaylight.yangtools.yang.model.api.TypeDefinition#getUnits()
104      */
105     @Override
106     public String getUnits() {
107         return UNITS;
108     }
109
110     /*
111      * (non-Javadoc)
112      *
113      * @see
114      * org.opendaylight.yangtools.yang.model.api.TypeDefinition#getDefaultValue
115      * ()
116      */
117     @Override
118     public Object getDefaultValue() {
119         return defaultEnum;
120     }
121
122     /*
123      * (non-Javadoc)
124      *
125      * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getQName()
126      */
127     @Override
128     public QName getQName() {
129         return BaseTypes.ENUMERATION_QNAME;
130     }
131
132     /*
133      * (non-Javadoc)
134      *
135      * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getPath()
136      */
137     @Override
138     public SchemaPath getPath() {
139         return path;
140     }
141
142     /*
143      * (non-Javadoc)
144      *
145      * @see
146      * org.opendaylight.yangtools.yang.model.api.SchemaNode#getDescription()
147      */
148     @Override
149     public String getDescription() {
150         return DESCRIPTION;
151     }
152
153     /*
154      * (non-Javadoc)
155      *
156      * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getReference()
157      */
158     @Override
159     public String getReference() {
160         return REFERENCE;
161     }
162
163     /*
164      * (non-Javadoc)
165      *
166      * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getStatus()
167      */
168     @Override
169     public Status getStatus() {
170         return Status.CURRENT;
171     }
172
173     /*
174      * (non-Javadoc)
175      *
176      * @see
177      * org.opendaylight.yangtools.yang.model.base.type.api.EnumTypeDefinition
178      * #getValues()
179      */
180     @Override
181     public List<EnumPair> getValues() {
182         return enums;
183     }
184
185     @Override
186     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
187         return Collections.emptyList();
188     }
189
190     @Override
191     public int hashCode() {
192         final int prime = 31;
193         int result = 1;
194         result = prime * result + ((defaultEnum == null) ? 0 : defaultEnum.hashCode());
195         result = prime * result + ((enums == null) ? 0 : enums.hashCode());
196         result = prime * result + BaseTypes.ENUMERATION_QNAME.hashCode();
197         result = prime * result + ((path == null) ? 0 : path.hashCode());
198         return result;
199     }
200
201     @Override
202     public boolean equals(final Object obj) {
203         if (this == obj) {
204             return true;
205         }
206         if (obj == null) {
207             return false;
208         }
209         if (getClass() != obj.getClass()) {
210             return false;
211         }
212         EnumerationType other = (EnumerationType) obj;
213         if (defaultEnum == null) {
214             if (other.defaultEnum != null) {
215                 return false;
216             }
217         } else if (!defaultEnum.equals(other.defaultEnum)) {
218             return false;
219         }
220         if (enums == null) {
221             if (other.enums != null) {
222                 return false;
223             }
224         } else if (!enums.equals(other.enums)) {
225             return false;
226         }
227         if (path == null) {
228             if (other.path != null) {
229                 return false;
230             }
231         } else if (!path.equals(other.path)) {
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(BaseTypes.ENUMERATION_QNAME);
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 }