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 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;
23 * The <code>default</code> implementation of Enumeration Type Definition
26 * @see EnumTypeDefinition
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 = "";
33 private final SchemaPath path;
34 private final EnumPair defaultEnum;
35 private final List<EnumPair> enums;
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();
44 this.defaultEnum = null;
49 * Constructs a new enumeration
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.
56 public static EnumerationType create(final SchemaPath path, final List<EnumPair> enums, final Optional<EnumPair> defaultValue) {
57 return new EnumerationType(path, enums, defaultValue);
64 * org.opendaylight.yangtools.yang.model.api.TypeDefinition#getBaseType()
67 public EnumTypeDefinition getBaseType() {
74 * @see org.opendaylight.yangtools.yang.model.api.TypeDefinition#getUnits()
77 public String getUnits() {
85 * org.opendaylight.yangtools.yang.model.api.TypeDefinition#getDefaultValue()
88 public Object getDefaultValue() {
95 * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getQName()
98 public QName getQName() {
99 return BaseTypes.ENUMERATION_QNAME;
105 * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getPath()
108 public SchemaPath getPath() {
116 * org.opendaylight.yangtools.yang.model.api.SchemaNode#getDescription()
119 public String getDescription() {
126 * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getReference()
129 public String getReference() {
136 * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getStatus()
139 public Status getStatus() {
140 return Status.CURRENT;
147 * org.opendaylight.yangtools.yang.model.base.type.api.EnumTypeDefinition#getValues()
150 public List<EnumPair> getValues() {
155 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
156 return Collections.emptyList();
160 public int hashCode() {
161 final int prime = 31;
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);
171 public boolean equals(final Object obj) {
178 if (getClass() != obj.getClass()) {
181 EnumerationType other = (EnumerationType) obj;
182 if (defaultEnum == null) {
183 if (other.defaultEnum != null) {
186 } else if (!defaultEnum.equals(other.defaultEnum)) {
190 if (other.enums != null) {
193 } else if (!enums.equals(other.enums)) {
197 if (other.path != null) {
200 } else if (!path.equals(other.path)) {
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);
224 return builder.toString();