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