cdc.mf.model.MfProperty Maven / Gradle / Ivy
package cdc.mf.model;
import cdc.mf.model.MfElementFeatures.Feature;
import cdc.util.lang.Checks;
import cdc.util.lang.Introspection;
import cdc.util.paths.Path;
/**
* Property class.
*
* @author Damien Carbonne
*/
public final class MfProperty extends MfMember
implements MfConstraintOwner, MfOrderedItem, MfTypeRefItem, MfQNameItem, MfDerivedItem, MfReadOnlyItem {
public static final Class PARENT_CLASS =
MfMemberOwner.class;
public static final Class> BUILDER_CLASS =
Introspection.uncheckedCast(Builder.class);
public static final MfElementFeatures FEATURES =
MfElementFeatures.builder()
.feature(Feature.REQUIRES_NAME)
.feature(Feature.NAME_UNIQUENESS)
.build();
private final MfElementRef typeRef;
private final MfCardinality cardinality;
private final String def;
private final boolean isOrdered;
private final boolean isReadOnly;
private final boolean isDerived;
MfProperty(Builder extends MfMemberOwner> builder) {
super(builder,
FEATURES);
this.typeRef = Checks.isNotNull(builder.typeRef, MfNames.TYPE);
this.cardinality = builder.cardinality;
this.def = builder.def;
this.isOrdered = builder.isOrdered;
this.isDerived = builder.isDerived;
this.isReadOnly = builder.isReadOnly;
addToParent(FEATURES);
addToModel();
}
@Override
public MfProperty duplicate(MfMemberOwner tgtParent) {
return tgtParent.property()
.set(this)
.build();
}
@Override
public Path getQName() {
return MfUtils.toQName(this);
}
@Override
public boolean canResolve(Path path) {
return MfUtils.canResolve(this, path);
}
@Override
public MfQNameItem resolve(Path path) {
return MfUtils.resolve(this, path);
}
@Override
public MfElementRef getTypeRef() {
return typeRef;
}
@Override
public MfCardinality getCardinality() {
return cardinality;
}
/**
* @return The default value.
*/
public String getDef() {
return def;
}
@Override
public boolean isOrdered() {
return isOrdered;
}
@Override
public boolean isDerived() {
return isDerived;
}
@Override
public boolean isReadOnly() {
return isReadOnly;
}
@Override
public MfDocumentation.Builder documentation() {
return MfDocumentation.builder(this);
}
@Override
public MfTag.Builder tag() {
return MfTag.builder(this);
}
@Override
public MfDependency.Builder dependency() {
return MfDependency.builder(this);
}
@Override
public MfConstraint.Builder constraint() {
return MfConstraint.builder(this);
}
static Builder
builder(P parent) {
return new Builder<>(parent);
}
/**
* Builder of properties.
*
* @author Damien Carbonne
*
* @param
The concrete parent type.
*/
public static final class Builder
extends MfMember.Builder, MfProperty, P> {
private MfElementRef typeRef = MfElementRef.NO_TYPE;
private MfCardinality cardinality;
private String def;
private boolean isOrdered;
private boolean isReadOnly;
private boolean isDerived;
protected Builder(P parent) {
super(parent);
}
@Override
public Builder set(MfProperty element) {
return super.set(element).typeRef(toLazy(element.getTypeRef()))
.cardinality(element.getCardinality())
.def(element.getDef())
.isOrdered(element.isOrdered())
.isReadOnly(element.isReadOnly())
.isDerived(element.isDerived());
}
@Override
public Class getElementClass() {
return MfProperty.class;
}
public MfElementRef getTypeRef() {
return typeRef;
}
public Builder typeRef(MfElementRef typeRef) {
this.typeRef = typeRef;
return self();
}
public Builder typeRef(String typeId,
String typeQName) {
this.typeRef = MfElementRef.of(getModel(),
MfType.class,
typeId,
typeQName == null ? null : Path.of(typeQName));
return self();
}
public Builder
type(MfType type) {
if (type == null) {
this.typeRef = MfElementRef.NO_TYPE;
} else {
this.typeRef = MfElementRef.of(type);
}
return self();
}
public Builder
typeId(String typeId) {
return typeRef(typeId, null);
}
public MfCardinality getCardinality() {
return cardinality;
}
public Builder
cardinality(MfCardinality cardinality) {
this.cardinality = cardinality;
return self();
}
public Builder
cardinality(String cardinality) {
this.cardinality = MfCardinality.of(cardinality);
return self();
}
public Builder
cardinality(String lower,
String upper) {
this.cardinality = MfCardinality.of(lower, upper);
return self();
}
public String getDef() {
return def;
}
public Builder
def(String def) {
this.def = def;
return self();
}
public boolean isOrdered() {
return isOrdered;
}
public Builder
isOrdered(boolean isOrdered) {
this.isOrdered = isOrdered;
return self();
}
public boolean isDerived() {
return isDerived;
}
public Builder
isDerived(boolean isDerived) {
this.isDerived = isDerived;
return self();
}
public boolean isReadOnly() {
return isReadOnly;
}
public Builder
isReadOnly(boolean isReadOnly) {
this.isReadOnly = isReadOnly;
return self();
}
@Override
public MfProperty build() {
return new MfProperty(this);
}
}
}