*/
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.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.builder.api.GroupingMember;
-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.util.YangParseException;
-public final class LeafSchemaNodeBuilder extends AbstractTypeAwareBuilder implements DataSchemaNodeBuilder,
- GroupingMember {
- private boolean isBuilt;
- private final LeafSchemaNodeImpl instance;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+
+public final class LeafSchemaNodeBuilder extends AbstractTypeAwareBuilder implements DataSchemaNodeBuilder {
+ private LeafSchemaNodeImpl instance;
+ private String defaultStr;
+ private String unitsStr;
// SchemaNode args
private SchemaPath schemaPath;
private String description;
// DataSchemaNode args
private boolean augmenting;
private boolean addedByUses;
- private Boolean configuration;
+ private boolean configuration;
private final ConstraintsBuilder constraints;
- // leaf args
- private String defaultStr;
- private String unitsStr;
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);
- constraints = new ConstraintsBuilder(moduleName, line);
+ this.schemaPath = Preconditions.checkNotNull(schemaPath, "Schema Path must not be null");
+ constraints = new ConstraintsBuilderImpl(moduleName, line);
+ }
+
+ public LeafSchemaNodeBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path, final LeafSchemaNode base) {
+ super(moduleName, line, qname);
+ 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();
+ configuration = base.isConfiguration();
+ this.type = base.getType();
+ unknownNodes.addAll(base.getUnknownSchemaNodes());
+
+ defaultStr = base.getDefault();
+ unitsStr = base.getUnits();
}
@Override
public LeafSchemaNode build() {
- if (!isBuilt) {
- instance.setPath(schemaPath);
- instance.setConstraints(constraints.build());
- instance.setDescription(description);
- instance.setReference(reference);
- instance.setStatus(status);
- instance.setAugmenting(augmenting);
- instance.setAddedByUses(addedByUses);
- instance.setConfiguration(configuration);
- instance.setDefault(defaultStr);
- instance.setUnits(unitsStr);
-
- if (type == null && typedef == null) {
- throw new YangParseException(moduleName, line, "Failed to resolve leaf type.");
- }
+ if (instance != null) {
+ return instance;
+ }
- // TYPE
- if (type == null) {
- instance.setType(typedef.build());
- } else {
- instance.setType(type);
- }
+ instance = new LeafSchemaNodeImpl(qname, schemaPath);
- // UNKNOWN NODES
- if (unknownNodes == null) {
- unknownNodes = new ArrayList<UnknownSchemaNode>();
- for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
- unknownNodes.add(b.build());
- }
- Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
- }
- instance.setUnknownSchemaNodes(unknownNodes);
+ 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;
- isBuilt = true;
+ if (type == null && typedef == null) {
+ throw new YangParseException(getModuleName(), getLine(), "Failed to resolve leaf type.");
}
+
+ // TYPE
+ if (type == null) {
+ instance.type = typedef.build();
+ } else {
+ instance.type = type;
+ }
+
+ // UNKNOWN NODES
+ for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
+ unknownNodes.add(b.build());
+ }
+ instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
+
return instance;
}
+ @Override
public SchemaPath getPath() {
return schemaPath;
}
return constraints;
}
+ @Override
public String getDescription() {
return description;
}
this.description = description;
}
+ @Override
public String getReference() {
return reference;
}
this.reference = reference;
}
+ @Override
public Status getStatus() {
return status;
}
@Override
public void setStatus(final Status status) {
- if (status != null) {
- this.status = status;
- }
+ this.status = Preconditions.checkNotNull(status, "status cannot be null");
}
+ @Override
public boolean isAugmenting() {
return augmenting;
}
this.addedByUses = addedByUses;
}
- public Boolean isConfiguration() {
+ @Override
+ public boolean isConfiguration() {
return configuration;
}
@Override
- public void setConfiguration(final Boolean configuration) {
+ public void setConfiguration(final boolean configuration) {
this.configuration = configuration;
}
return defaultStr;
}
- public void setDefaultStr(String defaultStr) {
+ public void setDefaultStr(final String defaultStr) {
this.defaultStr = defaultStr;
}
return unitsStr;
}
- public void setUnits(String unitsStr) {
+ public void setUnits(final String unitsStr) {
this.unitsStr = unitsStr;
}
}
@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 (parent == null) {
- if (other.parent != null) {
+ if (getParent() == null) {
+ if (other.getParent() != null) {
return false;
}
- } else if (!parent.equals(other.parent)) {
+ } else if (!getParent().equals(other.getParent())) {
return false;
}
return true;
return "leaf " + qname.getLocalName();
}
- private final class LeafSchemaNodeImpl implements LeafSchemaNode {
+ private static final class LeafSchemaNodeImpl implements LeafSchemaNode {
private final QName qname;
- private SchemaPath path;
+ private final SchemaPath path;
private String description;
private String reference;
- private Status status = Status.CURRENT;
+ private Status status;
private boolean augmenting;
private boolean addedByUses;
private boolean configuration;
private ConstraintDefinition constraintsDef;
private TypeDefinition<?> type;
- private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
+ private ImmutableList<UnknownSchemaNode> unknownNodes;
private String defaultStr;
private String unitsStr;
- private LeafSchemaNodeImpl(final QName qname) {
+ private LeafSchemaNodeImpl(final QName qname, final SchemaPath path) {
this.qname = qname;
+ this.path = path;
}
@Override
return path;
}
- private void setPath(final SchemaPath path) {
- this.path = path;
- }
-
@Override
public String getDescription() {
return description;
}
- private void setDescription(String description) {
- this.description = description;
- }
-
@Override
public String getReference() {
return reference;
}
- private void setReference(String reference) {
- this.reference = reference;
- }
-
@Override
public Status getStatus() {
return status;
}
- private void setStatus(Status status) {
- if (status != null) {
- this.status = status;
- }
- }
-
@Override
public boolean isAugmenting() {
return augmenting;
}
- private void setAugmenting(boolean augmenting) {
- this.augmenting = augmenting;
- }
-
@Override
public boolean isAddedByUses() {
return addedByUses;
}
- private void setAddedByUses(final boolean addedByUses) {
- this.addedByUses = addedByUses;
- }
-
@Override
public boolean isConfiguration() {
return configuration;
}
- private void setConfiguration(boolean configuration) {
- this.configuration = 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 unknownNodes;
}
- private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
- if (unknownNodes != null) {
- this.unknownNodes = unknownNodes;
- }
- }
-
@Override
public String getDefault() {
return defaultStr;
}
- private void setDefault(String defaultStr) {
- this.defaultStr = defaultStr;
- }
-
@Override
public String getUnits() {
return unitsStr;
}
- public void setUnits(String unitsStr) {
- this.unitsStr = unitsStr;
- }
-
@Override
public int hashCode() {
final int prime = 31;
}
@Override
- public boolean equals(Object obj) {
+ public boolean equals(final Object obj) {
if (this == obj) {
return true;
}