Scripted update of if statements
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / EnumerationType.java
index ea0e9f3fe1e250c1682f3b3b2e7b87db10fc9101..8cf162b26c79db31620fe90b2e5057698a644bdd 100644 (file)
@@ -7,9 +7,12 @@
  */
 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;
@@ -17,33 +20,43 @@ import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
 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 = "";
 
-    public EnumerationType(final SchemaPath path, final List<EnumPair> enums) {
-        super();
-        this.path = path;
-        this.enums = Collections.unmodifiableList(enums);
-        this.defaultEnum = null;
+    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.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);
     }
 
     /*
@@ -71,8 +84,7 @@ public final class EnumerationType implements EnumTypeDefinition {
      * (non-Javadoc)
      *
      * @see
-     * org.opendaylight.yangtools.yang.model.api.TypeDefinition#getDefaultValue
-     * ()
+     * org.opendaylight.yangtools.yang.model.api.TypeDefinition#getDefaultValue()
      */
     @Override
     public Object getDefaultValue() {
@@ -86,7 +98,7 @@ public final class EnumerationType implements EnumTypeDefinition {
      */
     @Override
     public QName getQName() {
-        return name;
+        return BaseTypes.ENUMERATION_QNAME;
     }
 
     /*
@@ -134,8 +146,7 @@ public final class EnumerationType implements EnumTypeDefinition {
      * (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() {
@@ -151,15 +162,15 @@ public final class EnumerationType implements EnumTypeDefinition {
     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;
         }
@@ -170,32 +181,13 @@ public final class EnumerationType implements EnumTypeDefinition {
             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;
@@ -203,22 +195,20 @@ public final class EnumerationType implements EnumTypeDefinition {
 
     @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 +
+                "]";
     }
 }