diff --git a/src/main/java/org/rosi_project/model_sync/model_join/representation/grammar/KeepSuperTypeExpression.java b/src/main/java/org/rosi_project/model_sync/model_join/representation/grammar/KeepSuperTypeExpression.java index c5801712ed50d2f42dae7f923904ec88e2859e54..3b68024d8894d24e1344556915419b6ad9413e8d 100644 --- a/src/main/java/org/rosi_project/model_sync/model_join/representation/grammar/KeepSuperTypeExpression.java +++ b/src/main/java/org/rosi_project/model_sync/model_join/representation/grammar/KeepSuperTypeExpression.java @@ -1,48 +1,138 @@ package org.rosi_project.model_sync.model_join.representation.grammar; +import java.util.ArrayList; import java.util.List; import java.util.Objects; import javax.annotation.Nonnull; -import org.rosi_project.model_sync.model_join.representation.core.AttributePath; import org.rosi_project.model_sync.model_join.representation.core.ClassResource; +import org.rosi_project.model_sync.model_join.representation.util.Assert; + +// TODO add doc /** + * A {@code KeepSuperTypeExpression} instructs the {@code ModelJoin} runtime to instantiate the a + * supertype of some class in a join along an instance of that class. The additional instance will + * be built according to the {@code KeepExpression keep statements} that it is constructed of. * * @author Rico Bergmann */ public class KeepSuperTypeExpression extends KeepExpression { + /** + * The {@code KeepSuperTypeBuilder} enables the construction of new {@code KeepSuperTypeExpression} + * instances through a nice and fluent interface. + * + * @author Rico Bergmann + */ + public static class KeepSuperTypeBuilder { + + private ClassResource typeToKeep; + private ClassResource target; + private List<KeepExpression> keeps; + + /** + * Default constructor. + */ + private KeepSuperTypeBuilder() { + this.keeps = new ArrayList<>(); + } + + /** + * Specifies the supertype that should be instantiated. + */ + @Nonnull + public KeepSuperTypeBuilder supertype(@Nonnull ClassResource type) { + this.typeToKeep = type; + return this; + } + + /** + * Specifies the name of the class that should be generated for the subtype instances. + */ + @Nonnull + public KeepSuperTypeBuilder as(@Nonnull ClassResource target) { + this.target = target; + return this; + } + + /** + * Adds a {@link KeepExpression keep statement} for the subtype-class. The described attribute + * will be initialized for each instantiated subtype element. + */ + @Nonnull + public KeepSuperTypeBuilder keep(@Nonnull KeepExpression keep) { + this.keeps.add(keep); + return this; + } + + /** + * Finishes the construction process. + */ + @Nonnull + public KeepSuperTypeExpression buildExpression() { + Assert.noNullArguments("All components must be specified", typeToKeep, target, keeps); + return new KeepSuperTypeExpression(typeToKeep, target, keeps); + } + + } + + /** + * Starts the creation process for a new {@code KeepSuperTypeExpression}. + */ + @Nonnull + public static KeepSuperTypeBuilder keep() { + return new KeepSuperTypeBuilder(); + } + @Nonnull private final ClassResource typeToKeep; @Nonnull - private final AttributePath target; + private final ClassResource target; @Nonnull private final List<KeepExpression> keeps; + /** + * Full constructor. + * + * @param typeToKeep the supertype that should be instantiated. + * @param target the name of the view that should be generated for all matching instances + * @param keeps the keep statements that should form the attributes of the generated view + * instances + */ public KeepSuperTypeExpression( @Nonnull ClassResource typeToKeep, - @Nonnull AttributePath target, + @Nonnull ClassResource target, @Nonnull List<KeepExpression> keeps) { this.typeToKeep = typeToKeep; this.target = target; this.keeps = keeps; } + /** + * Provides the supertype that should be instantiated. + */ @Nonnull public ClassResource getType() { return typeToKeep; } + /** + * Provides the name of the view that should be generated for all matching instances. + */ @Nonnull - public AttributePath getTarget() { + public ClassResource getTarget() { return target; } + /** + * Provides all {@code KeepExpression keep expressions} that should be used to build the Join for + * the instances of the superclass instances. + */ @Nonnull public List<KeepExpression> getKeeps() { - return keeps; + return new ArrayList<>(keeps); } @Override