Bug 2894 - Yang Data Codec Gson: null pointer exception when trying to
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / ExtendedType.java
index 6e5229d4cb9d712552b47c4f31f254bbd8a7b80b..c4a7ffe3edab286c9396defec69cdb2edebc057b 100644 (file)
@@ -7,6 +7,9 @@
  */
 package org.opendaylight.yangtools.yang.model.util;
 
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+
 import java.net.URI;
 import java.util.Collections;
 import java.util.Date;
@@ -20,7 +23,15 @@ import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
-
+/**
+ * Extended Type represents YANG type derived from other type.
+ *
+ * Extended type object is decorator on top of existing {@link TypeDefinition}
+ * which represents original type, and extended type
+ * may define additional constraints, modify description or reference
+ * of parent type or provide new type capture for specific use-cases.
+ *
+ */
 public class ExtendedType implements TypeDefinition<TypeDefinition<?>> {
 
     private final QName typeName;
@@ -35,10 +46,24 @@ public class ExtendedType implements TypeDefinition<TypeDefinition<?>> {
     private List<PatternConstraint> patterns = Collections.emptyList();
     private Integer fractionDigits = null;
 
-    private Status status;
-    private String units;
-    private Object defaultValue;
-    private boolean addedByUses;
+    private final Status status;
+    private final String units;
+    private final Object defaultValue;
+    private final boolean addedByUses;
+
+    /**
+     *
+     * Creates Builder for extended / derived type.
+     *
+     * @param typeName QName of derived type
+     * @param baseType Base type of derived type
+     * @param description Description of type
+     * @param reference Reference of Type
+     * @param path Schema path to type definition.
+     */
+    public static final Builder builder(final QName typeName,final TypeDefinition<?> baseType,final Optional<String> description,final Optional<String> reference,final SchemaPath path) {
+        return new Builder(typeName, baseType, description.or(""), reference.or(""), path);
+    }
 
     public static class Builder {
         private final QName typeName;
@@ -60,33 +85,55 @@ public class ExtendedType implements TypeDefinition<TypeDefinition<?>> {
         private List<PatternConstraint> patterns = Collections.emptyList();
         private Integer fractionDigits = null;
 
+        /**
+         *
+         * @param actualPath
+         * @param namespace
+         * @param revision
+         * @param typeName
+         * @param baseType
+         * @param description
+         * @param reference
+         *
+         * @deprecated Use {@link ExtendedType#builder(QName, TypeDefinition, Optional, Optional, SchemaPath)} instead.
+         */
+        @Deprecated
         public Builder(final List<String> actualPath, final URI namespace,
                 final Date revision, final QName typeName,
-                TypeDefinition<?> baseType, final String description,
+                final TypeDefinition<?> baseType, final String description,
                 final String reference) {
-            this.typeName = typeName;
-            this.baseType = baseType;
-            this.path = BaseTypes.schemaPath(actualPath, namespace, revision);
-            this.description = description;
-            this.reference = reference;
+            this(typeName,baseType,description,reference,BaseTypes.schemaPath(actualPath, namespace, revision));
         }
 
-        public Builder(final QName typeName, TypeDefinition<?> baseType,
+        /**
+         *
+         * Creates Builder for extended / derived type.
+         *
+         * @param typeName QName of derived type
+         * @param baseType Base type of derived type
+         * @param description Description of type
+         * @param reference Reference of Type
+         * @param path Schema path to type definition.
+         *
+         * @deprecated Use {@link ExtendedType#builder(QName, TypeDefinition, Optional, Optional, SchemaPath)} instead.
+         */
+        @Deprecated
+        public Builder(final QName typeName, final TypeDefinition<?> baseType,
                 final String description, final String reference,
-                SchemaPath path) {
-            this.typeName = typeName;
-            this.baseType = baseType;
-            this.path = path;
+                final SchemaPath path) {
+            this.typeName = Preconditions.checkNotNull(typeName, "type name must not be null.");
+            this.baseType = Preconditions.checkNotNull(baseType, "base type must not be null");
+            this.path = Preconditions.checkNotNull(path, "path must not be null.");
             this.description = description;
             this.reference = reference;
         }
 
-        public Builder status(Status status) {
+        public Builder status(final Status status) {
             this.status = status;
             return this;
         }
 
-        public Builder units(String units) {
+        public Builder units(final String units) {
             this.units = units;
             return this;
         }
@@ -103,7 +150,11 @@ public class ExtendedType implements TypeDefinition<TypeDefinition<?>> {
 
         public Builder unknownSchemaNodes(
                 final List<UnknownSchemaNode> unknownSchemaNodes) {
-            this.unknownSchemaNodes = unknownSchemaNodes;
+            if (unknownSchemaNodes.isEmpty()) {
+                this.unknownSchemaNodes = Collections.emptyList();
+            } else {
+                this.unknownSchemaNodes = unknownSchemaNodes;
+            }
             return this;
         }
 
@@ -138,7 +189,7 @@ public class ExtendedType implements TypeDefinition<TypeDefinition<?>> {
         }
     }
 
-    private ExtendedType(Builder builder) {
+    private ExtendedType(final Builder builder) {
         this.typeName = builder.typeName;
         this.baseType = builder.baseType;
         this.path = builder.path;
@@ -206,7 +257,7 @@ public class ExtendedType implements TypeDefinition<TypeDefinition<?>> {
     }
 
     @Override
-    public boolean equals(Object o) {
+    public boolean equals(final Object o) {
         if (this == o) {
             return true;
         }
@@ -218,8 +269,9 @@ public class ExtendedType implements TypeDefinition<TypeDefinition<?>> {
         if (path != null ? !path.equals(that.path) : that.path != null) {
             return false;
         }
-        if (typeName != null ? !typeName.equals(that.typeName) : that.typeName != null)
+        if (typeName != null ? !typeName.equals(that.typeName) : that.typeName != null) {
             return false;
+        }
 
         return true;
     }
@@ -256,15 +308,15 @@ public class ExtendedType implements TypeDefinition<TypeDefinition<?>> {
         return builder.toString();
     }
 
-    public List<RangeConstraint> getRanges() {
+    public List<RangeConstraint> getRangeConstraints() {
         return ranges;
     }
 
-    public List<LengthConstraint> getLengths() {
+    public List<LengthConstraint> getLengthConstraints() {
         return lengths;
     }
 
-    public List<PatternConstraint> getPatterns() {
+    public List<PatternConstraint> getPatternConstraints() {
         return patterns;
     }