2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.model.util;
10 import java.util.Collections;
11 import java.util.List;
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;
19 import com.google.common.base.Optional;
20 import com.google.common.base.Preconditions;
21 import com.google.common.collect.ImmutableList;
24 * The <code>default</code> implementation of Enumeration Type Definition
27 * @see EnumTypeDefinition
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 = "";
34 private final SchemaPath path;
35 private final EnumPair defaultEnum;
36 private final List<EnumPair> enums;
40 * Constructs EnumerationType
44 * @deprecated Use {@link #create(SchemaPath, List, Optional)} instead.
47 public EnumerationType(final SchemaPath path, final List<EnumPair> enums) {
48 this(path,enums,Optional.<EnumPair>absent());
52 * Constructs EnumerationType
57 * @deprecated Use {@link #create(SchemaPath, List, Optional)} instead.
60 public EnumerationType(final SchemaPath path, final EnumPair defaultEnum, final List<EnumPair> enums) {
61 this(path,enums,Optional.fromNullable(defaultEnum));
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();
71 this.defaultEnum = null;
77 * Constructs new enumeration
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.
85 public static EnumerationType create(final SchemaPath path, final List<EnumPair> enums, final Optional<EnumPair> defaultValue) {
86 return new EnumerationType(path, enums, defaultValue);
93 * org.opendaylight.yangtools.yang.model.api.TypeDefinition#getBaseType()
96 public EnumTypeDefinition getBaseType() {
103 * @see org.opendaylight.yangtools.yang.model.api.TypeDefinition#getUnits()
106 public String getUnits() {
114 * org.opendaylight.yangtools.yang.model.api.TypeDefinition#getDefaultValue
118 public Object getDefaultValue() {
125 * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getQName()
128 public QName getQName() {
129 return BaseTypes.ENUMERATION_QNAME;
135 * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getPath()
138 public SchemaPath getPath() {
146 * org.opendaylight.yangtools.yang.model.api.SchemaNode#getDescription()
149 public String getDescription() {
156 * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getReference()
159 public String getReference() {
166 * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getStatus()
169 public Status getStatus() {
170 return Status.CURRENT;
177 * org.opendaylight.yangtools.yang.model.base.type.api.EnumTypeDefinition
181 public List<EnumPair> getValues() {
186 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
187 return Collections.emptyList();
191 public int hashCode() {
192 final int prime = 31;
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());
202 public boolean equals(final Object obj) {
209 if (getClass() != obj.getClass()) {
212 EnumerationType other = (EnumerationType) obj;
213 if (defaultEnum == null) {
214 if (other.defaultEnum != null) {
217 } else if (!defaultEnum.equals(other.defaultEnum)) {
221 if (other.enums != null) {
224 } else if (!enums.equals(other.enums)) {
228 if (other.path != null) {
231 } else if (!path.equals(other.path)) {
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);
255 return builder.toString();