1818import java .util .Objects ;
1919import java .util .stream .Collectors ;
2020
21+ /**
22+ * Represents a product with features and an abstract syntax tree (AST).
23+ *
24+ * This class defines a product with a set of features and a product
25+ * AST.
26+ * It also stores the AST nodes of the main tree that correspond to the AST
27+ * nodes of the product AST.
28+ */
2129public class Product implements Serializable {
2230 private final EccoSet <Feature > features ;
2331 private final String name ;
@@ -27,6 +35,14 @@ public class Product implements Serializable {
2735 // product AST
2836 private EccoSet <ASTNode > astNodesMainTree ;
2937
38+ /**
39+ * Constructs a new Product object with the given parameters.
40+ *
41+ * @param name the name of the product
42+ * @param astNodesMainTree the main tree of AST nodes for the product
43+ * @param productAST the abstract syntax tree for the product
44+ * @param features the set of features associated with the product
45+ */
3046 public Product (final String name , final EccoSet <ASTNode > astNodesMainTree , final AbstractAST productAST ,
3147 final EccoSet <Feature > features ) {
3248 this .name = name ;
@@ -35,52 +51,92 @@ public Product(final String name, final EccoSet<ASTNode> astNodesMainTree, final
3551 this .features = features ;
3652 }
3753
54+ /**
55+ * Creates a new Product object by copying the contents of another Product
56+ * object.
57+ *
58+ * @param other The Product object to copy from
59+ * @throws UnsupportedOperationException if the productAST type is not JavaAST
60+ * or LineAST
61+ */
3862 public Product (final Product other ) {
63+ // Copy the name from the other Product object
3964 this .name = other .name ;
65+
66+ // Create a new set of ASTNodes to copy from the other Product object
4067 final EccoSet <ASTNode > nodesToCopy = new EccoSet <>(other .astNodesMainTree );
4168 nodesToCopy .addAll (other .productAST .getAstNodes ());
4269 nodesToCopy .add (other .productAST .getRoot ());
70+
71+ // Create a mapping of original ASTNodes to copied ASTNodes
4372 final Map <ASTNode , ASTNode > originalToCopyMap = copyAstNodes (nodesToCopy );
4473
74+ // Copy the ASTNodes from the other Product object and update the main tree
4575 final EccoSet <ASTNode > astNodes = new EccoSet <>(
4676 other .astNodesMainTree
4777 .stream ()
4878 .map (originalToCopyMap ::get )
4979 .collect (Collectors .toCollection (EccoSet ::new )));
5080 this .astNodesMainTree = new EccoSet <>(astNodes );
5181
82+ // Copy the ASTNodes from the other Product object and update the tree
5283 final EccoSet <ASTNode > treeNodes = new EccoSet <>(
5384 other .productAST .getAstNodes ()
5485 .stream ()
5586 .map (originalToCopyMap ::get )
5687 .collect (Collectors .toCollection (EccoSet ::new )));
88+
89+ // Create a new productAST object based on the type of the other Product
90+ // object's productAST
5791 if (other .productAST instanceof JavaAST ) {
5892 final JavaAST ast = (JavaAST ) other .productAST ;
5993 this .productAST = new JavaAST (originalToCopyMap .get (ast .getRoot ()), treeNodes );
6094 } else if (other .productAST instanceof LineAST ) {
6195 final LineAST ast = (LineAST ) other .productAST ;
6296 this .productAST = new LineAST (originalToCopyMap .get (ast .getRoot ()), treeNodes );
6397 } else {
64- throw new UnsupportedOperationException ();
98+ throw new UnsupportedOperationException ("Unsupported productAST type" );
6599 }
100+
101+ // Copy the features from the other Product object
66102 this .features = new EccoSet <>(other .getFeatures ());
67103 }
68104
105+ /**
106+ * Clears the product Abstract Syntax Tree (AST) by setting it to null.
107+ */
69108 public void forgetAST () {
70109 this .productAST = null ;
71110 }
72111
112+ /**
113+ * Retrieves the set of features associated with the product.
114+ *
115+ * @return The set of features associated with the product.
116+ */
73117 public EccoSet <Feature > getFeatures () {
74118 return features ;
75119 }
76120
121+ /**
122+ * Returns the set of AST nodes in the main tree.
123+ *
124+ * @return EccoSet<ASTNode> - a set of AST nodes in the main tree
125+ */
77126 public EccoSet <ASTNode > getAstNodesMainTree () {
78127 return astNodesMainTree ;
79128 }
80129
81- // returns the mapping of the node at given position in the product or (if it
82- // was already merged into the main tree) the mapping of the corresponding node
83- // in the main tree
130+ /**
131+ * Returns the mapping of the node at the given position in the product AST or,
132+ * if it has already been merged into the main tree, the mapping of the
133+ * corresponding
134+ * node in the main tree.
135+ *
136+ * @param position The position of the node to find the mapping for
137+ * @return The mapping of the node at the given position, or null if no node is
138+ * found
139+ */
84140 public Formula getMappingFromPosition (final Position position ) {
85141 // look for the right node in the product AST's nodes
86142 for (final ASTNode oldNode : productAST .getAstNodes ()) {
@@ -97,12 +153,24 @@ public Formula getMappingFromPosition(final Position position) {
97153 return null ;
98154 }
99155
156+ /**
157+ * Returns the name of the object.
158+ *
159+ * @return the name of the object as a String
160+ */
100161 public String getName () {
101162 return name ;
102163 }
103164
104- // returns the node at given position in the product or (if it was already
105- // merged into the main tree) the corresponding node in the main tree
165+ /**
166+ * Returns the ASTNode at the given position in the product AST or, if it has
167+ * already been merged into the main tree,
168+ * returns the corresponding node in the main tree.
169+ *
170+ * @param position The position to search for in the product AST.
171+ * @return The ASTNode at the given position in the product AST, or the
172+ * corresponding node in the main tree if merged.
173+ */
106174 public ASTNode getNodeFromPosition (final Position position ) {
107175 // look for the right node in the product AST's nodes
108176 for (final ASTNode oldNode : productAST .getAstNodes ()) {
@@ -119,10 +187,20 @@ public ASTNode getNodeFromPosition(final Position position) {
119187 return null ;
120188 }
121189
190+ /**
191+ * Returns the Abstract Syntax Tree (AST) representing the product.
192+ *
193+ * @return the AST representing the product
194+ */
122195 public AbstractAST getProductAst () {
123196 return productAST ;
124197 }
125198
199+ /**
200+ * Sets the AST nodes for the main tree.
201+ *
202+ * @param astNodesMainTree the AST nodes for the main tree
203+ */
126204 public void setAstNodesMainTree (final EccoSet <ASTNode > astNodesMainTree ) {
127205 this .astNodesMainTree = astNodesMainTree ;
128206 }
0 commit comments