21
21
22
22
import java .util .ArrayList ;
23
23
import java .util .Collections ;
24
- import java .util .Comparator ;
25
24
import java .util .List ;
26
25
import java .util .Map ;
27
- import java .util .TreeMap ;
26
+ import java .util .concurrent .CompletableFuture ;
27
+ import java .util .concurrent .ConcurrentSkipListMap ;
28
28
29
29
import com .github .checkstyle .parser .CheckstyleReportsParser ;
30
30
@@ -42,21 +42,26 @@ public final class DiffReport {
42
42
* Container for parsed data,
43
43
* note it is a TreeMap for memory keeping purposes.
44
44
*/
45
- private Map <String , List <CheckstyleRecord >> records =
46
- new TreeMap <>();
45
+ private final Map <String , List <CheckstyleRecord >> records =
46
+ new ConcurrentSkipListMap <>();
47
47
48
48
/**
49
49
* Container for statistical data.
50
50
*/
51
- private Statistics statistics = new Statistics ();
51
+ private final Statistics statistics = new Statistics ();
52
+
53
+ /**
54
+ * Asynchronous tasks for difference calculation.
55
+ */
56
+ private final List <CompletableFuture <Void >> asyncTasks = new ArrayList <>();
52
57
53
58
/**
54
59
* Getter for data container.
55
60
*
56
61
* @return map containing parsed data.
57
62
*/
58
63
public Map <String , List <CheckstyleRecord >> getRecords () {
59
- return records ;
64
+ return Collections . unmodifiableMap ( records ) ;
60
65
}
61
66
62
67
public Statistics getStatistics () {
@@ -76,111 +81,68 @@ public Statistics getStatistics() {
76
81
public void addRecords (List <CheckstyleRecord > newRecords ,
77
82
String filename ) {
78
83
if (!newRecords .isEmpty ()) {
84
+ Collections .sort (newRecords );
79
85
final List <CheckstyleRecord > popped =
80
86
records .put (filename , newRecords );
81
87
if (popped != null ) {
82
88
final List <CheckstyleRecord > diff =
83
- produceDiff (popped , newRecords );
89
+ DiffUtils . produceDiff (popped , newRecords );
84
90
if (diff .isEmpty ()) {
85
91
records .remove (filename );
86
92
}
87
93
else {
88
- Collections .sort (diff , new PositionOrderComparator ());
89
94
records .put (filename , diff );
90
95
}
91
96
}
92
97
}
93
98
}
94
99
95
100
/**
96
- * Creates difference between 2 lists of records.
101
+ * Adds new records to the diff report asynchronously,
102
+ * when there are records with this filename, comparison
103
+ * between them and new record is performed and only difference is saved.
97
104
*
98
- * @param list1
99
- * the first list.
100
- * @param list2
101
- * the second list.
102
- * @return the difference list.
105
+ * @param newRecords
106
+ * a new records list.
107
+ * @param filename
108
+ * name of a file which is a cause of records generation.
103
109
*/
104
- private static List <CheckstyleRecord > produceDiff (
105
- List <CheckstyleRecord > list1 , List <CheckstyleRecord > list2 ) {
106
- final List <CheckstyleRecord > diff = new ArrayList <>();
107
- for (CheckstyleRecord rec1 : list1 ) {
108
- if (!isInList (list2 , rec1 )) {
109
- diff .add (rec1 );
110
- }
111
- }
112
- for (CheckstyleRecord rec2 : list2 ) {
113
- if (!isInList (list1 , rec2 )) {
114
- diff .add (rec2 );
115
- }
116
- }
117
- return diff ;
110
+ public void addRecordsAsync (
111
+ List <CheckstyleRecord > newRecords , String filename ) {
112
+ asyncTasks .add (CompletableFuture .runAsync (() -> addRecords (newRecords , filename )));
118
113
}
119
114
120
115
/**
121
- * Compares the record against list of records.
122
- *
123
- * @param list
124
- * of records.
125
- * @param testedRecord
126
- * the record.
127
- * @return true, if has its copy in a list.
116
+ * Await completion of all asynchronous tasks.
128
117
*/
129
- private static boolean isInList (List <CheckstyleRecord > list ,
130
- CheckstyleRecord testedRecord ) {
131
- boolean belongsToList = false ;
132
- for (CheckstyleRecord checkstyleRecord : list ) {
133
- if (testedRecord .specificEquals (checkstyleRecord )) {
134
- belongsToList = true ;
135
- break ;
136
- }
137
- }
138
- return belongsToList ;
118
+ public void joinAsyncTasksAll () {
119
+ asyncTasks .forEach (CompletableFuture ::join );
139
120
}
140
121
141
122
/**
142
123
* Generates statistical information and puts in in the accumulator.
143
124
*/
144
125
public void getDiffStatistics () {
145
126
statistics .setFileNumDiff (records .size ());
146
- for (Map .Entry <String , List <CheckstyleRecord >> entry
147
- : records .entrySet ()) {
148
- final List <CheckstyleRecord > list = entry .getValue ();
149
- for (CheckstyleRecord rec : list ) {
150
- if (rec .getIndex () == CheckstyleReportsParser .BASE_REPORT_INDEX ) {
151
- statistics .addSeverityRecordRemoved (rec .getSeverity ());
152
- statistics .addModuleRecordRemoved (rec .getSource ());
153
- }
154
- else {
155
- statistics .addSeverityRecordAdded (rec .getSeverity ());
156
- statistics .addModuleRecordAdded (rec .getSource ());
157
- }
158
- statistics .incrementUniqueMessageCount (rec .getIndex ());
159
- }
160
- }
127
+ records .entrySet ().parallelStream ()
128
+ .flatMap (entry -> entry .getValue ().stream ())
129
+ .forEach (this ::addRecordStatistics );
161
130
}
162
131
163
132
/**
164
- * Comparator used to sort lists of CheckstyleRecord objects
165
- * by their position in code.
166
- *
167
- * @author atta_troll
133
+ * Generates statistical information for one CheckstyleRecord.
168
134
*
135
+ * @param checkstyleRecord the checkstyleRecord to process
169
136
*/
170
- private static class PositionOrderComparator
171
- implements Comparator <CheckstyleRecord > {
172
-
173
- @ Override
174
- public int compare (final CheckstyleRecord arg0 ,
175
- final CheckstyleRecord arg1 ) {
176
- final int difference = arg0 .getLine () - arg1 .getLine ();
177
- if (difference == 0 ) {
178
- return arg0 .getColumn () - arg1 .getColumn ();
179
- }
180
- else {
181
- return difference ;
182
- }
137
+ private void addRecordStatistics (CheckstyleRecord checkstyleRecord ) {
138
+ if (checkstyleRecord .getIndex () == CheckstyleReportsParser .BASE_REPORT_INDEX ) {
139
+ statistics .addSeverityRecordRemoved (checkstyleRecord .getSeverity ());
140
+ statistics .addModuleRecordRemoved (checkstyleRecord .getSource ());
141
+ }
142
+ else {
143
+ statistics .addSeverityRecordAdded (checkstyleRecord .getSeverity ());
144
+ statistics .addModuleRecordAdded (checkstyleRecord .getSource ());
183
145
}
146
+ statistics .incrementUniqueMessageCount (checkstyleRecord .getIndex ());
184
147
}
185
-
186
148
}
0 commit comments