*/
package org.opendaylight.controller.yang.parser.builder.impl;
-import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
-import java.util.Date;
import java.util.List;
import org.opendaylight.controller.yang.common.QName;
import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
import org.opendaylight.controller.yang.model.util.UnionType;
import org.opendaylight.controller.yang.parser.builder.api.AbstractTypeAwareBuilder;
-import org.opendaylight.controller.yang.parser.builder.api.Builder;
import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder;
+import org.opendaylight.controller.yang.parser.util.YangParseException;
/**
* Builder for YANG union type. User can add type to this union as
* When build is called, types in builder form will be built and add to resolved
* types.
*/
-public class UnionTypeBuilder extends AbstractTypeAwareBuilder implements
- TypeDefinitionBuilder, Builder {
+public final class UnionTypeBuilder extends AbstractTypeAwareBuilder implements
+ TypeDefinitionBuilder {
+ private final static String NAME = "union";
+
private final int line;
private final List<TypeDefinition<?>> types;
private final List<TypeDefinitionBuilder> typedefs;
- private final UnionType instance;
+ private UnionType instance;
+ private boolean isBuilt;
- private final List<String> actualPath;
- private final URI namespace;
- private final Date revision;
+ private SchemaPath path;
- public UnionTypeBuilder(final List<String> actualPath, final URI namespace,
- final Date revision, final int line) {
+ public UnionTypeBuilder(final int line) {
this.line = line;
types = new ArrayList<TypeDefinition<?>>();
typedefs = new ArrayList<TypeDefinitionBuilder>();
- instance = new UnionType(actualPath, namespace, revision, types);
-
- this.actualPath = actualPath;
- this.namespace = namespace;
- this.revision = revision;
}
@Override
}
@Override
- public void setType(final TypeDefinitionBuilder tdb) {
+ public void setTypedef(final TypeDefinitionBuilder tdb) {
typedefs.add(tdb);
}
@Override
public UnionType build() {
- for (TypeDefinitionBuilder tdb : typedefs) {
- types.add(tdb.build());
+ if (!isBuilt) {
+ instance = new UnionType(path, types);
+ for (TypeDefinitionBuilder tdb : typedefs) {
+ types.add(tdb.build());
+ }
+ isBuilt = true;
}
return instance;
}
@Override
public void setPath(final SchemaPath schemaPath) {
- throw new IllegalStateException("Can not set path to "
- + UnionTypeBuilder.class.getSimpleName());
+ this.path = schemaPath;
}
@Override
public void setDescription(final String description) {
- throw new IllegalStateException("Can not set description to "
- + UnionTypeBuilder.class.getSimpleName());
+ throw new YangParseException(line, "Can not set description to " + NAME);
}
@Override
public void setReference(final String reference) {
- throw new IllegalStateException("Can not set reference to "
- + UnionTypeBuilder.class.getSimpleName());
+ throw new YangParseException(line, "Can not set reference to " + NAME);
}
@Override
public void setStatus(final Status status) {
- throw new IllegalStateException("Can not set status to "
- + UnionTypeBuilder.class.getSimpleName());
+ throw new YangParseException(line, "Can not set status to " + NAME);
}
@Override
public void addUnknownSchemaNode(final UnknownSchemaNodeBuilder unknownNode) {
- throw new IllegalStateException("Can not add unknown node to "
- + UnionTypeBuilder.class.getSimpleName());
+ throw new YangParseException(line, "Can not add unknown node to "
+ + NAME);
}
@Override
@Override
public SchemaPath getPath() {
- return null;
+ return path;
}
@Override
@Override
public void setRanges(List<RangeConstraint> ranges) {
- throw new IllegalStateException("Can not set ranges to "
- + UnionTypeBuilder.class.getSimpleName());
+ throw new YangParseException(line, "Can not set ranges to " + NAME);
}
@Override
@Override
public void setLengths(List<LengthConstraint> lengths) {
- throw new IllegalStateException("Can not set lengths to "
- + UnionTypeBuilder.class.getSimpleName());
+ throw new YangParseException(line, "Can not set lengths to " + NAME);
}
@Override
@Override
public void setPatterns(List<PatternConstraint> patterns) {
- throw new IllegalStateException("Can not set patterns to "
- + UnionTypeBuilder.class.getSimpleName());
+ throw new YangParseException(line, "Can not set patterns to " + NAME);
}
@Override
@Override
public void setFractionDigits(Integer fractionDigits) {
- throw new IllegalStateException("Can not set fraction digits to "
- + UnionTypeBuilder.class.getSimpleName());
+ throw new YangParseException(line, "Can not set fraction digits to "
+ + NAME);
}
@Override
@Override
public void setDefaultValue(Object defaultValue) {
- throw new IllegalStateException("Can not set default value to "
- + UnionTypeBuilder.class.getSimpleName());
+ throw new YangParseException(line, "Can not set default value to "
+ + NAME);
}
@Override
@Override
public void setUnits(String units) {
- throw new IllegalStateException("Can not set units to "
- + UnionTypeBuilder.class.getSimpleName());
- }
-
- public List<String> getActualPath() {
- return actualPath;
- }
-
- public URI getNamespace() {
- return namespace;
- }
-
- public Date getRevision() {
- return revision;
+ throw new YangParseException(line, "Can not set units to " + NAME);
}
@Override