BUG-865: deprecate pre-Beryllium parser elements
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / TypeDefinitionBuilderImpl.java
index fd158a753dd18925d999ed1ea5e4509dd3fcb889..a131d9f9bc48b55041238a8c7d727b69e8c14fc1 100644 (file)
@@ -7,25 +7,29 @@
  */
 package org.opendaylight.yangtools.yang.parser.builder.impl;
 
-import java.util.ArrayList;
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
 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.TypeDefinition;
-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;
 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
-import org.opendaylight.yangtools.yang.model.util.UnknownType;
-import org.opendaylight.yangtools.yang.parser.builder.api.AbstractTypeAwareBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
-import org.opendaylight.yangtools.yang.parser.util.Comparators;
+import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.util.AbstractTypeAwareBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.util.Comparators;
 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
 
+/**
+ * @deprecated Pre-Beryllium implementation, scheduled for removal.
+ */
+@Deprecated
 public final class TypeDefinitionBuilderImpl extends AbstractTypeAwareBuilder implements TypeDefinitionBuilder {
     private SchemaPath schemaPath;
     private List<RangeConstraint> ranges = Collections.emptyList();
@@ -40,48 +44,65 @@ public final class TypeDefinitionBuilderImpl extends AbstractTypeAwareBuilder im
     private Object defaultValue;
     private boolean addedByUses;
 
-    public TypeDefinitionBuilderImpl(final String moduleName, final int line, final QName qname) {
+    public TypeDefinitionBuilderImpl(final String moduleName, final int line, final QName qname, final SchemaPath path) {
         super(moduleName, line, qname);
+        this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
+    }
+
+    public TypeDefinitionBuilderImpl(final String moduleName, final int line, final QName qname, final SchemaPath path, final ExtendedType base) {
+        super(moduleName, line, base.getQName());
+        this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
+
+        this.type = base.getBaseType();
+        this.description = base.getDescription();
+        this.reference = base.getReference();
+        this.status = base.getStatus();
+        this.units = base.getUnits();
+        this.defaultValue = base.getDefaultValue();
+
+        this.addedByUses = base.isAddedByUses();
+        this.ranges = base.getRangeConstraints();
+        this.lengths = base.getLengthConstraints();
+        this.patterns = base.getPatternConstraints();
+        this.fractionDigits = base.getFractionDigits();
+        this.unknownNodes.addAll(base.getUnknownSchemaNodes());
     }
 
     @Override
     public TypeDefinition<? extends TypeDefinition<?>> build() {
-        TypeDefinition<?> result = null;
-        ExtendedType.Builder typeBuilder = null;
-        if ((type == null || type instanceof UnknownType) && typedef == null) {
-            throw new YangParseException("Unresolved type: '" + qname.getLocalName() + "'.");
-        }
-        if (type == null || type instanceof UnknownType) {
-            type = typedef.build();
+        TypeDefinition<?> result;
+        ExtendedType.Builder typeBuilder;
+        if (type == null) {
+            if (typedef == null) {
+                throw new YangParseException("Unresolved type: '" + qname.getLocalName() + "'.");
+            } else {
+                type = typedef.build();
+            }
         }
 
-        typeBuilder = new ExtendedType.Builder(qname, type, description, reference, schemaPath);
-
+        typeBuilder = ExtendedType.builder(qname, type, Optional.fromNullable(description),
+                Optional.fromNullable(reference), schemaPath);
         typeBuilder.status(status);
         typeBuilder.units(units);
         typeBuilder.defaultValue(defaultValue);
         typeBuilder.addedByUses(addedByUses);
-
         typeBuilder.ranges(ranges);
         typeBuilder.lengths(lengths);
         typeBuilder.patterns(patterns);
         typeBuilder.fractionDigits(fractionDigits);
 
         // UNKNOWN NODES
-        if (unknownNodes == null) {
-            unknownNodes = new ArrayList<UnknownSchemaNode>();
-            for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
-                unknownNodes.add(b.build());
-            }
-            Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
+        for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
+            unknownNodes.add(b.build());
         }
+        Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
         typeBuilder.unknownSchemaNodes(unknownNodes);
         result = typeBuilder.build();
         return result;
     }
 
     @Override
-    public void setQName(QName qname) {
+    public void setQName(final QName qname) {
         this.qname = qname;
     }
 
@@ -91,8 +112,8 @@ public final class TypeDefinitionBuilderImpl extends AbstractTypeAwareBuilder im
     }
 
     @Override
-    public void setPath(final SchemaPath schemaPath) {
-        this.schemaPath = schemaPath;
+    public void setPath(final SchemaPath path) {
+        this.schemaPath = path;
     }
 
     @Override
@@ -157,11 +178,6 @@ public final class TypeDefinitionBuilderImpl extends AbstractTypeAwareBuilder im
         this.defaultValue = defaultValue;
     }
 
-    @Override
-    public List<UnknownSchemaNode> getUnknownNodes() {
-        return Collections.emptyList();
-    }
-
     @Override
     public List<RangeConstraint> getRanges() {
         return ranges;
@@ -213,4 +229,40 @@ public final class TypeDefinitionBuilderImpl extends AbstractTypeAwareBuilder im
         return "typedef " + qname.getLocalName();
     }
 
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = super.hashCode();
+        result = prime * result + Objects.hashCode(schemaPath);
+
+        return result;
+    }
+
+    @Override
+    public boolean equals(final Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        if (!super.equals(obj)) {
+            return false;
+        }
+        TypeDefinitionBuilderImpl other = (TypeDefinitionBuilderImpl) obj;
+
+        if (schemaPath == null) {
+            if (other.schemaPath != null) {
+                return false;
+            }
+        } else if (!schemaPath.equals(other.schemaPath)) {
+            return false;
+        }
+
+        return true;
+    }
+
 }