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
27 * @deprecated Use {@link org.opendaylight.yangtools.yang.model.util.type.BaseTypes#enumerationTypeBuilder(SchemaPath)} instead
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 = "";
35 private final SchemaPath path;
36 private final EnumPair defaultEnum;
37 private final List<EnumPair> enums;
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();
46 this.defaultEnum = null;
51 * Constructs a new enumeration
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.
58 public static EnumerationType create(final SchemaPath path, final List<EnumPair> enums, final Optional<EnumPair> defaultValue) {
59 return new EnumerationType(path, enums, defaultValue);
66 * org.opendaylight.yangtools.yang.model.api.TypeDefinition#getBaseType()
69 public EnumTypeDefinition getBaseType() {
76 * @see org.opendaylight.yangtools.yang.model.api.TypeDefinition#getUnits()
79 public String getUnits() {
87 * org.opendaylight.yangtools.yang.model.api.TypeDefinition#getDefaultValue()
90 public Object getDefaultValue() {
97 * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getQName()
100 public QName getQName() {
101 return BaseTypes.ENUMERATION_QNAME;
107 * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getPath()
110 public SchemaPath getPath() {
118 * org.opendaylight.yangtools.yang.model.api.SchemaNode#getDescription()
121 public String getDescription() {
128 * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getReference()
131 public String getReference() {
138 * @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getStatus()
141 public Status getStatus() {
142 return Status.CURRENT;
149 * org.opendaylight.yangtools.yang.model.base.type.api.EnumTypeDefinition#getValues()
152 public List<EnumPair> getValues() {
157 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
158 return Collections.emptyList();
162 public int hashCode() {
163 final int prime = 31;
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);
173 public boolean equals(final Object obj) {
180 if (getClass() != obj.getClass()) {
183 EnumerationType other = (EnumerationType) obj;
184 if (!Objects.equals(defaultEnum, other.defaultEnum)) {
187 if (!Objects.equals(enums, other.enums)) {
190 if (!Objects.equals(path, other.path)) {
197 public String toString() {
198 StringBuilder builder = new StringBuilder();
199 builder.append("EnumerationType [name=");
200 builder.append(BaseTypes.ENUMERATION_QNAME);
201 builder.append(", path=");
202 builder.append(path);
203 builder.append(", description=");
204 builder.append(DESCRIPTION);
205 builder.append(", reference=");
206 builder.append(REFERENCE);
207 builder.append(", defaultEnum=");
208 builder.append(defaultEnum);
209 builder.append(", enums=");
210 builder.append(enums);
211 builder.append(", units=");
212 builder.append(UNITS);
214 return builder.toString();