Scripted update of if statements
[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  * @deprecated Use {@link org.opendaylight.yangtools.yang.model.util.type.BaseTypes#enumerationTypeBuilder(SchemaPath)} instead
28  */
29 @Deprecated
30 public final class EnumerationType implements EnumTypeDefinition {
31     private static final String DESCRIPTION = "The enumeration built-in type represents values from a set of assigned names.";
32     private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.6";
33     private static final String UNITS = "";
34
35     private final SchemaPath path;
36     private final EnumPair defaultEnum;
37     private final List<EnumPair> enums;
38
39     private EnumerationType(final SchemaPath path, final List<EnumPair> enums, final Optional<EnumPair> defaultEnum) {
40         this.path = Preconditions.checkNotNull(path,"path must not be null");
41         this.enums = ImmutableList.copyOf(Preconditions.checkNotNull(enums, "enums must not be null."));
42         if (defaultEnum.isPresent()) {
43             Preconditions.checkArgument(enums.contains(defaultEnum.get()),"defaultEnum must be contained in defined enumerations.");
44             this.defaultEnum = defaultEnum.get();
45         } else {
46             this.defaultEnum = null;
47         }
48     }
49
50     /**
51      * Constructs a new enumeration
52      *
53      * @param path Schema Path to definition point of this enumeration
54      * @param enums List of defined enumeration values
55      * @param defaultValue {@link Optional#of(Object)} of default value, {@link Optional#absent()} if no default value is defined.
56      *        If defaultValue is set, it must be present in provided list of enumerations.
57      */
58     public static EnumerationType create(final SchemaPath path, final List<EnumPair> enums, final Optional<EnumPair> defaultValue) {
59         return new EnumerationType(path, enums, defaultValue);
60     }
61
62     /*
63      * (non-Javadoc)
64      *
65      * @see
66      * org.opendaylight.yangtools.yang.model.api.TypeDefinition#getBaseType()
67      */
68     @Override
69     public EnumTypeDefinition getBaseType() {
70         return null;
71     }
72
73     /*
74      * (non-Javadoc)
75      *
76      * @see org.opendaylight.yangtools.yang.model.api.TypeDefinition#getUnits()
77      */
78     @Override
79     public String getUnits() {
80         return UNITS;
81     }
82
83     /*
84      * (non-Javadoc)
85      *
86      * @see
87      * org.opendaylight.yangtools.yang.model.api.TypeDefinition#getDefaultValue()
88      */
89     @Override
90     public Object getDefaultValue() {
91         return defaultEnum;
92     }
93
94     /*
95      * (non-Javadoc)
96      *
97      * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getQName()
98      */
99     @Override
100     public QName getQName() {
101         return BaseTypes.ENUMERATION_QNAME;
102     }
103
104     /*
105      * (non-Javadoc)
106      *
107      * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getPath()
108      */
109     @Override
110     public SchemaPath getPath() {
111         return path;
112     }
113
114     /*
115      * (non-Javadoc)
116      *
117      * @see
118      * org.opendaylight.yangtools.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.yangtools.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.yangtools.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
149      * org.opendaylight.yangtools.yang.model.base.type.api.EnumTypeDefinition#getValues()
150      */
151     @Override
152     public List<EnumPair> getValues() {
153         return enums;
154     }
155
156     @Override
157     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
158         return Collections.emptyList();
159     }
160
161     @Override
162     public int hashCode() {
163         final int prime = 31;
164         int result = 1;
165         result = prime * result + Objects.hashCode(defaultEnum);
166         result = prime * result + Objects.hashCode(enums);
167         result = prime * result + BaseTypes.ENUMERATION_QNAME.hashCode();
168         result = prime * result + Objects.hashCode(path);
169         return result;
170     }
171
172     @Override
173     public boolean equals(final Object obj) {
174         if (this == obj) {
175             return true;
176         }
177         if (obj == null) {
178             return false;
179         }
180         if (getClass() != obj.getClass()) {
181             return false;
182         }
183         EnumerationType other = (EnumerationType) obj;
184         if (!Objects.equals(defaultEnum, other.defaultEnum)) {
185             return false;
186         }
187         if (!Objects.equals(enums, other.enums)) {
188             return false;
189         }
190         if (!Objects.equals(path, other.path)) {
191             return false;
192         }
193         return true;
194     }
195
196     @Override
197     public String toString() {
198         return "EnumerationType [name=" +
199                 BaseTypes.ENUMERATION_QNAME +
200                 ", path=" +
201                 path +
202                 ", description=" +
203                 DESCRIPTION +
204                 ", reference=" +
205                 REFERENCE +
206                 ", defaultEnum=" +
207                 defaultEnum +
208                 ", enums=" +
209                 enums +
210                 ", units=" +
211                 UNITS +
212                 "]";
213     }
214 }