21
21
22
22
import java .util .ArrayList ;
23
23
import java .util .Collections ;
24
- import java .util .Comparator ;
24
+ import java .util .Iterator ;
25
25
import java .util .List ;
26
26
import java .util .Map ;
27
- import java .util .TreeMap ;
27
+ import java .util .concurrent . ConcurrentSkipListMap ;
28
28
29
29
import com .github .checkstyle .parser .CheckstyleReportsParser ;
30
30
@@ -42,21 +42,21 @@ 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
52
53
53
/**
54
54
* Getter for data container.
55
55
*
56
56
* @return map containing parsed data.
57
57
*/
58
58
public Map <String , List <CheckstyleRecord >> getRecords () {
59
- return records ;
59
+ return Collections . unmodifiableMap ( records ) ;
60
60
}
61
61
62
62
public Statistics getStatistics () {
@@ -76,6 +76,7 @@ public Statistics getStatistics() {
76
76
public void addRecords (List <CheckstyleRecord > newRecords ,
77
77
String filename ) {
78
78
if (!newRecords .isEmpty ()) {
79
+ newRecords .sort (CheckstyleRecord ::specificCompare );
79
80
final List <CheckstyleRecord > popped =
80
81
records .put (filename , newRecords );
81
82
if (popped != null ) {
@@ -85,57 +86,84 @@ public void addRecords(List<CheckstyleRecord> newRecords,
85
86
records .remove (filename );
86
87
}
87
88
else {
88
- Collections .sort (diff , new PositionOrderComparator ());
89
89
records .put (filename , diff );
90
90
}
91
91
}
92
92
}
93
93
}
94
94
95
95
/**
96
- * Creates difference between 2 lists of records.
96
+ * Creates difference between 2 sorted lists of records.
97
97
*
98
- * @param list1
98
+ * @param firstList
99
99
* the first list.
100
- * @param list2
100
+ * @param secondList
101
101
* the second list.
102
102
* @return the difference list.
103
103
*/
104
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
- }
105
+ List <CheckstyleRecord > firstList , List <CheckstyleRecord > secondList ) {
106
+ final List <CheckstyleRecord > result ;
107
+ if (firstList .isEmpty ()) {
108
+ result = secondList ;
111
109
}
112
- for (CheckstyleRecord rec2 : list2 ) {
113
- if (!isInList (list1 , rec2 )) {
114
- diff .add (rec2 );
115
- }
110
+ else if (secondList .isEmpty ()) {
111
+ result = firstList ;
112
+ }
113
+ else {
114
+ result = produceDiff (firstList .iterator (), secondList .iterator ());
116
115
}
117
- return diff ;
116
+ return result ;
118
117
}
119
118
120
119
/**
121
- * Compares the record against list of records.
120
+ * Creates difference between 2 non-empty iterators of records.
122
121
*
123
- * @param list
124
- * of records .
125
- * @param testedRecord
126
- * the record .
127
- * @return true, if has its copy in a list .
122
+ * @param firstIterator
123
+ * the first iterator .
124
+ * @param secondIterator
125
+ * the second iterator .
126
+ * @return the difference list (always sorted) .
128
127
*/
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 ;
128
+ private static List <CheckstyleRecord > produceDiff (
129
+ Iterator <CheckstyleRecord > firstIterator , Iterator <CheckstyleRecord > secondIterator ) {
130
+ CheckstyleRecord firstVal = firstIterator .next ();
131
+ CheckstyleRecord secondVal = secondIterator .next ();
132
+ final List <CheckstyleRecord > result = new ArrayList <>();
133
+ while (true ) {
134
+ final int diff = firstVal .specificCompare (secondVal );
135
+ if (diff < 0 ) {
136
+ result .add (firstVal );
137
+ if (!firstIterator .hasNext ()) {
138
+ result .add (secondVal );
139
+ break ;
140
+ }
141
+ firstVal = firstIterator .next ();
142
+ }
143
+ else if (diff > 0 ) {
144
+ result .add (secondVal );
145
+ if (!secondIterator .hasNext ()) {
146
+ result .add (firstVal );
147
+ break ;
148
+ }
149
+ secondVal = secondIterator .next ();
150
+ }
151
+ else {
152
+ if (!firstIterator .hasNext () || !secondIterator .hasNext ()) {
153
+ break ;
154
+ }
155
+ firstVal = firstIterator .next ();
156
+ secondVal = secondIterator .next ();
136
157
}
137
158
}
138
- return belongsToList ;
159
+ // add tails
160
+ while (firstIterator .hasNext ()) {
161
+ result .add (firstIterator .next ());
162
+ }
163
+ while (secondIterator .hasNext ()) {
164
+ result .add (secondIterator .next ());
165
+ }
166
+ return result ;
139
167
}
140
168
141
169
/**
@@ -160,27 +188,4 @@ public void getDiffStatistics() {
160
188
}
161
189
}
162
190
163
- /**
164
- * Comparator used to sort lists of CheckstyleRecord objects
165
- * by their position in code.
166
- *
167
- * @author atta_troll
168
- *
169
- */
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
- }
183
- }
184
- }
185
-
186
191
}
0 commit comments