cdc.mf.model.MfAbstractChildElement Maven / Gradle / Ivy
package cdc.mf.model;
import java.util.Optional;
/**
* Base class of children elements (they have a parent).
*
* @author Damien Carbonne
*
* @param The parent type. The concrete parent may derive from it.
*/
public abstract class MfAbstractChildElement
extends MfAbstractElement {
private final P parent;
protected MfAbstractChildElement(Builder, ?, ? extends P> builder,
MfElementFeatures features) {
super(builder,
features);
this.parent = builder.parent;
}
protected final void addToParent(MfElementFeatures features) {
((MfAbstractElement) getParent()).addChild(this, features);
}
// protected void setChild(Builder, ?, ? extends MfElement> builder) {
// setElement(builder);
// }
/**
* Clone this element as a child of a target parent.
*
* @param tgtParent The target parent.
* @return A clone of this element, attached to the target parent.
*/
public abstract MfAbstractChildElement
duplicate(P tgtParent);
@Override
public P getParent() {
return parent;
}
@Override
public int getIndex() {
return ((MfAbstractElement) parent).getChildIndex(this);
}
@Override
public MfModel getModel() {
return getParent().getModel();
}
/**
* Base builder of children elements.
*
* @author Damien Carbonne
*
* @param The builder type.
* @param The built element type.
* @param The concrete parent type.
*/
public abstract static class Builder,
E extends MfAbstractChildElement super P>,
P extends MfElement>
extends MfAbstractElement.Builder {
protected final P parent;
protected Builder(P parent) {
this.parent = parent;
}
protected MfElementRef toLazy(MfElementRef ref) {
return MfElementRef.toLazy(ref, getModel());
}
/**
* @return The parent element.
*/
public P getParent() {
return parent;
}
/**
* @return The root model.
*/
public final MfModel getModel() {
return parent.getModel();
}
protected Optional getItemWithId(String id,
Class itemClass) {
return getModel().getItemWithId(id, itemClass);
}
/**
* Build the element and returns its parent.
*
* @return The concrete parent of the built element.
*/
public P back() {
build();
return parent;
}
}
}