*/
package org.opendaylight.yangtools.yang.parser.builder.impl;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-
import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
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.parser.builder.api.AbstractTypeAwareBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.api.ConstraintsBuilder;
import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
-import org.opendaylight.yangtools.yang.parser.util.Comparators;
+import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.util.AbstractTypeAwareBuilder;
import org.opendaylight.yangtools.yang.parser.util.YangParseException;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
public final class LeafSchemaNodeBuilder extends AbstractTypeAwareBuilder implements DataSchemaNodeBuilder {
- private boolean isBuilt;
- private final LeafSchemaNodeImpl instance;
+ private LeafSchemaNodeImpl instance;
+ private String defaultStr;
+ private String unitsStr;
// SchemaNode args
private SchemaPath schemaPath;
+ private String description;
+ private String reference;
+ private Status status = Status.CURRENT;
// DataSchemaNode args
+ private boolean augmenting;
+ private boolean addedByUses;
+ private LeafSchemaNode originalNode;
+ private LeafSchemaNodeBuilder originalBuilder;
+ private boolean configuration;
private final ConstraintsBuilder constraints;
public LeafSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath schemaPath) {
super(moduleName, line, qname);
- this.schemaPath = schemaPath;
- instance = new LeafSchemaNodeImpl(qname, schemaPath);
- constraints = new ConstraintsBuilder(moduleName, line);
+ this.schemaPath = Preconditions.checkNotNull(schemaPath, "Schema Path must not be null");
+ constraints = new ConstraintsBuilderImpl(moduleName, line);
}
- public LeafSchemaNodeBuilder(String moduleName, int line, QName qname, SchemaPath path, LeafSchemaNode base) {
+ public LeafSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path, final LeafSchemaNode base) {
super(moduleName, line, qname);
- this.schemaPath = path;
- instance = new LeafSchemaNodeImpl(qname, path);
- constraints = new ConstraintsBuilder(moduleName, line, base.getConstraints());
-
- instance.description = base.getDescription();
- instance.reference = base.getReference();
- instance.status = base.getStatus();
- instance.augmenting = base.isAugmenting();
- instance.addedByUses = base.isAddedByUses();
- instance.configuration = base.isConfiguration();
- instance.constraintsDef = base.getConstraints();
+ this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
+ constraints = new ConstraintsBuilderImpl(moduleName, line, base.getConstraints());
+
+ description = base.getDescription();
+ reference = base.getReference();
+ status = base.getStatus();
+ augmenting = base.isAugmenting();
+ addedByUses = base.isAddedByUses();
+ originalNode =base;
+ configuration = base.isConfiguration();
this.type = base.getType();
- instance.unknownNodes.addAll(base.getUnknownSchemaNodes());
- instance.defaultStr = base.getDefault();
- instance.unitsStr = base.getUnits();
+ unknownNodes.addAll(base.getUnknownSchemaNodes());
+
+ defaultStr = base.getDefault();
+ unitsStr = base.getUnits();
}
@Override
public LeafSchemaNode build() {
- if (!isBuilt) {
- instance.setConstraints(constraints.build());
+ if (instance != null) {
+ return instance;
+ }
- if (type == null && typedef == null) {
- throw new YangParseException(moduleName, line, "Failed to resolve leaf type.");
- }
+ instance = new LeafSchemaNodeImpl(qname, schemaPath);
- // TYPE
- if (type == null) {
- instance.setType(typedef.build());
- } else {
- instance.setType(type);
- }
+ instance.description = description;
+ instance.reference = reference;
+ instance.status = status;
+ instance.augmenting = augmenting;
+ instance.addedByUses = addedByUses;
+ instance.configuration = configuration;
+ instance.constraintsDef = constraints.toInstance();
+ instance.defaultStr = defaultStr;
+ instance.unitsStr = unitsStr;
- // UNKNOWN NODES
- for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
- unknownNodes.add(b.build());
- }
- Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
- instance.addUnknownSchemaNodes(unknownNodes);
+ if (type == null && typedef == null) {
+ throw new YangParseException(getModuleName(), getLine(), "Failed to resolve leaf type.");
+ }
- isBuilt = true;
+ // TYPE
+ if (type == null) {
+ instance.type = typedef.build();
+ } else {
+ instance.type = type;
}
+
+ // ORIGINAL NODE
+ if (originalNode == null && originalBuilder != null) {
+ originalNode = originalBuilder.build();
+ }
+ instance.original = originalNode;
+
+ // UNKNOWN NODES
+ for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
+ unknownNodes.add(b.build());
+ }
+ instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
+
return instance;
}
-
@Override
public SchemaPath getPath() {
return schemaPath;
}
@Override
- public void setPath(SchemaPath path) {
- instance.path = path;
+ public void setPath(final SchemaPath path) {
+ this.schemaPath = path;
}
@Override
@Override
public String getDescription() {
- return instance.description;
+ return description;
}
@Override
public void setDescription(final String description) {
- instance.description = description;
+ this.description = description;
}
@Override
public String getReference() {
- return instance.reference;
+ return reference;
}
@Override
public void setReference(final String reference) {
- instance.reference = reference;
+ this.reference = reference;
}
@Override
public Status getStatus() {
- return instance.status;
+ return status;
}
@Override
- public void setStatus(Status status) {
- if (status != null) {
- instance.status = status;
- }
+ public void setStatus(final Status status) {
+ this.status = Preconditions.checkNotNull(status, "status cannot be null");
}
@Override
public boolean isAugmenting() {
- return instance.augmenting;
+ return augmenting;
}
@Override
public void setAugmenting(final boolean augmenting) {
- instance.augmenting = augmenting;
+ this.augmenting = augmenting;
}
@Override
public boolean isAddedByUses() {
- return instance.addedByUses;
+ return addedByUses;
}
@Override
public void setAddedByUses(final boolean addedByUses) {
- instance.addedByUses = addedByUses;
+ this.addedByUses = addedByUses;
}
@Override
- public Boolean isConfiguration() {
- return instance.configuration;
+ public LeafSchemaNodeBuilder getOriginal() {
+ return originalBuilder;
}
@Override
- public void setConfiguration(final Boolean configuration) {
- instance.configuration = configuration;
+ public void setOriginal(final SchemaNodeBuilder builder) {
+ Preconditions.checkArgument(builder instanceof LeafSchemaNodeBuilder, "Original of leaf cannot be " + builder);
+ this.originalBuilder = (LeafSchemaNodeBuilder) builder;
+ }
+
+ @Override
+ public boolean isConfiguration() {
+ return configuration;
+ }
+
+ @Override
+ public void setConfiguration(final boolean configuration) {
+ this.configuration = configuration;
}
public String getDefaultStr() {
- return instance.defaultStr;
+ return defaultStr;
}
- public void setDefaultStr(String defaultStr) {
- instance.defaultStr = defaultStr;
+ public void setDefaultStr(final String defaultStr) {
+ this.defaultStr = defaultStr;
}
public String getUnits() {
- return instance.unitsStr;
+ return unitsStr;
}
- public void setUnits(String unitsStr) {
- instance.unitsStr = unitsStr;
+ public void setUnits(final String unitsStr) {
+ this.unitsStr = unitsStr;
}
@Override
}
@Override
- public boolean equals(Object obj) {
+ public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
} else if (!schemaPath.equals(other.schemaPath)) {
return false;
}
- if (parentBuilder == null) {
- if (other.parentBuilder != null) {
+ if (getParent() == null) {
+ if (other.getParent() != null) {
return false;
}
- } else if (!parentBuilder.equals(other.parentBuilder)) {
+ } else if (!getParent().equals(other.getParent())) {
return false;
}
return true;
return "leaf " + qname.getLocalName();
}
- private static final class LeafSchemaNodeImpl implements LeafSchemaNode {
- private final QName qname;
- private SchemaPath path;
- private String description;
- private String reference;
- private Status status = Status.CURRENT;
- private boolean augmenting;
- private boolean addedByUses;
- private boolean configuration;
- private ConstraintDefinition constraintsDef;
- private TypeDefinition<?> type;
- private final List<UnknownSchemaNode> unknownNodes = new ArrayList<>();
- private String defaultStr;
- private String unitsStr;
-
- private LeafSchemaNodeImpl(final QName qname, final SchemaPath path) {
- this.qname = qname;
- this.path = path;
- }
-
- @Override
- public QName getQName() {
- return qname;
- }
-
- @Override
- public SchemaPath getPath() {
- return path;
- }
-
- @Override
- public String getDescription() {
- return description;
- }
-
- @Override
- public String getReference() {
- return reference;
- }
-
- @Override
- public Status getStatus() {
- return status;
- }
-
- @Override
- public boolean isAugmenting() {
- return augmenting;
- }
-
- @Override
- public boolean isAddedByUses() {
- return addedByUses;
- }
-
- @Override
- public boolean isConfiguration() {
- return configuration;
- }
-
- @Override
- public ConstraintDefinition getConstraints() {
- return constraintsDef;
- }
-
- private void setConstraints(ConstraintDefinition constraintsDef) {
- this.constraintsDef = constraintsDef;
- }
-
- @Override
- public TypeDefinition<?> getType() {
- return type;
- }
-
- private void setType(TypeDefinition<? extends TypeDefinition<?>> type) {
- this.type = type;
- }
-
- @Override
- public List<UnknownSchemaNode> getUnknownSchemaNodes() {
- return Collections.unmodifiableList(unknownNodes);
- }
-
- private void addUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
- if (unknownNodes != null) {
- this.unknownNodes.addAll(unknownNodes);
- }
- }
-
- @Override
- public String getDefault() {
- return defaultStr;
- }
-
- @Override
- public String getUnits() {
- return unitsStr;
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((qname == null) ? 0 : qname.hashCode());
- result = prime * result + ((path == null) ? 0 : path.hashCode());
- return result;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- LeafSchemaNodeImpl other = (LeafSchemaNodeImpl) obj;
- if (qname == null) {
- if (other.qname != null) {
- return false;
- }
- } else if (!qname.equals(other.qname)) {
- return false;
- }
- if (path == null) {
- if (other.path != null) {
- return false;
- }
- } else if (!path.equals(other.path)) {
- return false;
- }
- return true;
- }
-
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder(LeafSchemaNodeImpl.class.getSimpleName());
- sb.append("[");
- sb.append("qname=" + qname);
- sb.append(", path=" + path);
- sb.append("]");
- return sb.toString();
- }
- }
-
}