edits
[cdsspec-compiler.git] / src / edu / uci / eecs / codeGenerator / CodeAdditions.java
1 package edu.uci.eecs.codeGenerator;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.Collections;
6 import java.util.Comparator;
7
8 import edu.uci.eecs.specExtraction.Code;
9
10 /**
11  * <p>
12  * This class represents all the code additions that should be added to a
13  * specific file.
14  * </p>
15  * 
16  * @author Peizhao Ou
17  * 
18  */
19 public class CodeAdditions {
20
21         /**
22          * <p>
23          * This class represents the addition of code for a specific file. It
24          * records a list of lines to be inserted for a specific file, and the line
25          * after which the code should be inserted to the file.
26          * </p>
27          * 
28          * @author Peizhao Ou
29          * 
30          */
31         public static class CodeAddition {
32                 // The line after which the code should be inserted to the file
33                 // E.g. insertingLine == 0 => insert the lines ine very beginning.
34                 public final int insertingLine;
35
36                 // The code to be added to the specified place
37                 public final Code code;
38
39                 public CodeAddition(int insertingLine, Code code) {
40                         this.insertingLine = insertingLine;
41                         this.code = code;
42                 }
43
44                 public static Comparator<CodeAddition> lineNumComparator = new Comparator<CodeAddition>() {
45                         public int compare(CodeAddition addition1, CodeAddition addition2) {
46                                 return addition1.insertingLine - addition2.insertingLine;
47                         }
48                 };
49         }
50
51         // A list of code addition for the same file
52         public final ArrayList<CodeAddition> codeAdditions;
53
54         // The file that the list of additions belong to
55         public final File file;
56
57         public CodeAdditions(File file) {
58                 this.file = file;
59                 codeAdditions = new ArrayList<CodeAddition>();
60         }
61
62         public void addCodeAddition(CodeAddition a) {
63                 this.codeAdditions.add(a);
64         }
65
66         /**
67          * <p>
68          * Whether the addition list is empty
69          * </p>
70          * 
71          * @return
72          */
73         public boolean isEmpty() {
74                 return this.codeAdditions.size() == 0;
75         }
76
77         /**
78          * <p>
79          * Sort the list of code additions to the same file in an increasing order
80          * by the inserting line number of the code additions. We will call this
81          * function so that we can process code addition more conveniently.
82          * </p>
83          */
84         public void sort() {
85                 Collections.sort(codeAdditions, CodeAddition.lineNumComparator);
86         }
87 }