1111import java .util .List ;
1212import java .util .Map ;
1313import java .util .Set ;
14+ import java .util .function .Function ;
1415
15- /**
16- *
17- */
1816public class FeatureModelBuilder {
1917
2018 private FeatureModel fmInConstruction ;
2119
20+ private AbstractUVLElementFactory elementFactory ;
21+
2222 public FeatureModelBuilder () {
23- fmInConstruction = new FeatureModel ();
23+ this (new DefaultUVLElementFactory ());
24+ }
25+
26+ public FeatureModelBuilder (AbstractUVLElementFactory factory ) {
27+ this .elementFactory = factory ;
28+ this .fmInConstruction = new FeatureModel ();
2429 }
2530
2631 public FeatureModelBuilder (FeatureModel old ) {
2732 fmInConstruction = old ;
2833 }
2934
30-
3135 public void addImport (Import importToAdd ) {
3236 fmInConstruction .getImports ().add (importToAdd );
3337 }
@@ -49,7 +53,7 @@ public boolean setRootFeature(Feature feature) {
4953 }
5054
5155 public Feature addRootFeature (String featureName , FeatureType featureType , Cardinality cardinality ) {
52- Feature rootFeature = new Feature (featureName );
56+ Feature rootFeature = elementFactory . createFeature (featureName );
5357 rootFeature .setFeatureType (featureType );
5458 rootFeature .setCardinality (cardinality );
5559
@@ -65,13 +69,14 @@ public Feature addFeature(String featureName, Group group) {
6569 public Feature addFeature (String featureName , Group group , Import featureOrigin ) {
6670 return addFeature (featureName , group , featureOrigin , FeatureType .BOOL );
6771 }
68-
69- public Feature addFeature (String featureName , Group group , Import featureOrigin , FeatureType type ) {
72+
73+ public Feature addFeature (String featureName , Group group , Import featureOrigin , FeatureType type ) {
7074 return addFeature (featureName , group , featureOrigin , type , Cardinality .getStandardFeatureCardinality ());
7175 }
7276
73- public Feature addFeature (String featureName , Group group , Import featureOrigin , FeatureType type , Cardinality cardinality ) {
74- Feature feature = new Feature (featureName );
77+ public Feature addFeature (String featureName , Group group , Import featureOrigin , FeatureType type ,
78+ Cardinality cardinality ) {
79+ Feature feature = elementFactory .createFeature (featureName );
7580 feature .setRelatedImport (featureOrigin );
7681 feature .setFeatureType (type );
7782 feature .setCardinality (cardinality );
@@ -99,9 +104,11 @@ public void addAttribute(Feature feature, Attribute<?> attribute) {
99104
100105 /**
101106 * Renames a feature and propagates the change through the mdoel
107+ *
102108 * @param oldName current name of feature to be renamed in the model
103109 * @param newName target name
104- * @return indicator whether oldName successfully changed in the feature model; fails if oldName is not present
110+ * @return indicator whether oldName successfully changed in the feature model;
111+ * fails if oldName is not present
105112 */
106113 public boolean renameFeature (String oldName , String newName ) {
107114 Map <String , Feature > featureMap = fmInConstruction .getFeatureMap ();
@@ -137,6 +144,7 @@ public void renameAttributeGlobally(String oldName, String newName) {
137144 crawlConstraintsToRenameGlobalAttribute (constraint , oldName , newName );
138145 }
139146 }
147+
140148 private void crawlConstraintsToRenameGlobalAttribute (Constraint constraint , String attributeName , String replace ) {
141149 if (constraint instanceof ExpressionConstraint ) {
142150 for (Expression exp : ((ExpressionConstraint ) constraint ).getExpressionSubParts ()) {
@@ -154,7 +162,8 @@ private void crawlExpressionsToRenameGlobalAttribute(Expression expression, Stri
154162 AggregateFunctionExpression aggregateFunctionExpression = (AggregateFunctionExpression ) expression ;
155163 if (aggregateFunctionExpression .getAttribute ().getIdentifier ().equals (attributeName )) {
156164 aggregateFunctionExpression .getAttribute ().renameGlobalAttribute (replace );
157- };
165+ }
166+ ;
158167 } else {
159168 for (Expression exp : expression .getExpressionSubParts ()) {
160169 crawlExpressionsToRenameGlobalAttribute (exp , attributeName , replace );
@@ -163,15 +172,16 @@ private void crawlExpressionsToRenameGlobalAttribute(Expression expression, Stri
163172 }
164173
165174 public void addConstraint (Constraint constraint ) {
166- fmInConstruction .getOwnConstraints ().add (0 ,constraint );
175+ fmInConstruction .getOwnConstraints ().add (0 , constraint );
167176 }
168177
169178 public void addConstraintAtPosition (Constraint constraint , int position ) {
170- fmInConstruction .getOwnConstraints ().add (position ,constraint );
179+ fmInConstruction .getOwnConstraints ().add (position , constraint );
171180 }
172181
173182 public boolean doesFeatureModelSatisfyLanguageLevels (Set <LanguageLevel > languageLevelsToSatisfy ) {
174- return fmInConstruction .isExplicitLanguageLevels () && !fmInConstruction .getUsedLanguageLevels ().equals (languageLevelsToSatisfy );
183+ return fmInConstruction .isExplicitLanguageLevels ()
184+ && !fmInConstruction .getUsedLanguageLevels ().equals (languageLevelsToSatisfy );
175185 }
176186
177187 public FeatureModel getFeatureModel () {
@@ -192,7 +202,7 @@ public LiteralConstraint createFeatureLiteral(String name) {
192202 }
193203
194204 public GlobalAttribute createGlobalAttribute (String name ) {
195- GlobalAttribute toCreate = new GlobalAttribute (name , fmInConstruction );
205+ GlobalAttribute toCreate = elementFactory . createGlobalAttribute (name , fmInConstruction );
196206 if (toCreate .getType () == null ) {
197207 System .err .println ("Tried to reference " + name + " but attribute with that name does not exist" );
198208 return null ;
0 commit comments