*/
package org.opendaylight.yangtools.yang.model.util;
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
import java.util.Collections;
import java.util.List;
-
+import java.util.Objects;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
import org.opendaylight.yangtools.yang.model.api.Status;
import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
/**
- * The <code>default</code> implementation of Enumertaion Type Definition
+ * The <code>default</code> implementation of Enumeration Type Definition
* interface.
- *
+ *
* @see EnumTypeDefinition
+ * @deprecated Use {@link org.opendaylight.yangtools.yang.model.util.type.BaseTypes#enumerationTypeBuilder(SchemaPath)} instead
*/
+@Deprecated
public final class EnumerationType implements EnumTypeDefinition {
- private final QName name = BaseTypes.constructQName("enumeration");
- private final SchemaPath path;
private static final String DESCRIPTION = "The enumeration built-in type represents values from a set of assigned names.";
private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.6";
+ private static final String UNITS = "";
+ private final SchemaPath path;
private final EnumPair defaultEnum;
private final List<EnumPair> enums;
- private static final String UNITS = "";
- private final EnumTypeDefinition baseType;
-
- public EnumerationType(final SchemaPath path, final List<EnumPair> enums) {
- super();
- this.path = path;
- this.enums = Collections.unmodifiableList(enums);
- this.defaultEnum = null;
- this.baseType = this;
+
+ private EnumerationType(final SchemaPath path, final List<EnumPair> enums, final Optional<EnumPair> defaultEnum) {
+ this.path = Preconditions.checkNotNull(path,"path must not be null");
+ this.enums = ImmutableList.copyOf(Preconditions.checkNotNull(enums, "enums must not be null."));
+ if (defaultEnum.isPresent()) {
+ Preconditions.checkArgument(enums.contains(defaultEnum.get()),"defaultEnum must be contained in defined enumerations.");
+ this.defaultEnum = defaultEnum.get();
+ } else {
+ this.defaultEnum = null;
+ }
}
- public EnumerationType(final SchemaPath path, final EnumPair defaultEnum, final List<EnumPair> enums) {
- super();
- this.path = path;
- this.baseType = this;
- this.defaultEnum = defaultEnum;
- this.enums = Collections.unmodifiableList(enums);
+ /**
+ * Constructs a new enumeration
+ *
+ * @param path Schema Path to definition point of this enumeration
+ * @param enums List of defined enumeration values
+ * @param defaultValue {@link Optional#of(Object)} of default value, {@link Optional#absent()} if no default value is defined.
+ * If defaultValue is set, it must be present in provided list of enumerations.
+ */
+ public static EnumerationType create(final SchemaPath path, final List<EnumPair> enums, final Optional<EnumPair> defaultValue) {
+ return new EnumerationType(path, enums, defaultValue);
}
/*
* (non-Javadoc)
- *
+ *
* @see
* org.opendaylight.yangtools.yang.model.api.TypeDefinition#getBaseType()
*/
@Override
public EnumTypeDefinition getBaseType() {
- return baseType;
+ return null;
}
/*
* (non-Javadoc)
- *
+ *
* @see org.opendaylight.yangtools.yang.model.api.TypeDefinition#getUnits()
*/
@Override
/*
* (non-Javadoc)
- *
+ *
* @see
- * org.opendaylight.yangtools.yang.model.api.TypeDefinition#getDefaultValue
- * ()
+ * org.opendaylight.yangtools.yang.model.api.TypeDefinition#getDefaultValue()
*/
@Override
public Object getDefaultValue() {
/*
* (non-Javadoc)
- *
+ *
* @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getQName()
*/
@Override
public QName getQName() {
- return name;
+ return BaseTypes.ENUMERATION_QNAME;
}
/*
* (non-Javadoc)
- *
+ *
* @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getPath()
*/
@Override
/*
* (non-Javadoc)
- *
+ *
* @see
* org.opendaylight.yangtools.yang.model.api.SchemaNode#getDescription()
*/
/*
* (non-Javadoc)
- *
+ *
* @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getReference()
*/
@Override
/*
* (non-Javadoc)
- *
+ *
* @see org.opendaylight.yangtools.yang.model.api.SchemaNode#getStatus()
*/
@Override
/*
* (non-Javadoc)
- *
+ *
* @see
- * org.opendaylight.yangtools.yang.model.base.type.api.EnumTypeDefinition
- * #getValues()
+ * org.opendaylight.yangtools.yang.model.base.type.api.EnumTypeDefinition#getValues()
*/
@Override
public List<EnumPair> getValues() {
public int hashCode() {
final int prime = 31;
int result = 1;
- result = prime * result + ((defaultEnum == null) ? 0 : defaultEnum.hashCode());
- result = prime * result + ((enums == null) ? 0 : enums.hashCode());
- result = prime * result + ((name == null) ? 0 : name.hashCode());
- result = prime * result + ((path == null) ? 0 : path.hashCode());
+ result = prime * result + Objects.hashCode(defaultEnum);
+ result = prime * result + Objects.hashCode(enums);
+ result = prime * result + BaseTypes.ENUMERATION_QNAME.hashCode();
+ result = prime * result + Objects.hashCode(path);
return result;
}
@Override
- public boolean equals(Object obj) {
+ public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
return false;
}
EnumerationType other = (EnumerationType) obj;
- if (defaultEnum == null) {
- if (other.defaultEnum != null) {
- return false;
- }
- } else if (!defaultEnum.equals(other.defaultEnum)) {
- return false;
- }
- if (enums == null) {
- if (other.enums != null) {
- return false;
- }
- } else if (!enums.equals(other.enums)) {
+ if (!Objects.equals(defaultEnum, other.defaultEnum)) {
return false;
}
- if (name == null) {
- if (other.name != null) {
- return false;
- }
- } else if (!name.equals(other.name)) {
+ if (!Objects.equals(enums, other.enums)) {
return false;
}
- if (path == null) {
- if (other.path != null) {
- return false;
- }
- } else if (!path.equals(other.path)) {
+ if (!Objects.equals(path, other.path)) {
return false;
}
return true;
@Override
public String toString() {
- StringBuilder builder = new StringBuilder();
- builder.append("EnumerationType [name=");
- builder.append(name);
- builder.append(", path=");
- builder.append(path);
- builder.append(", description=");
- builder.append(DESCRIPTION);
- builder.append(", reference=");
- builder.append(REFERENCE);
- builder.append(", defaultEnum=");
- builder.append(defaultEnum);
- builder.append(", enums=");
- builder.append(enums);
- builder.append(", units=");
- builder.append(UNITS);
- builder.append("]");
- return builder.toString();
+ return "EnumerationType [name=" +
+ BaseTypes.ENUMERATION_QNAME +
+ ", path=" +
+ path +
+ ", description=" +
+ DESCRIPTION +
+ ", reference=" +
+ REFERENCE +
+ ", defaultEnum=" +
+ defaultEnum +
+ ", enums=" +
+ enums +
+ ", units=" +
+ UNITS +
+ "]";
}
}