23
23
import javax .annotation .Nonnull ;
24
24
import javax .annotation .Nullable ;
25
25
26
+ import com .google .common .base .Function ;
26
27
import com .google .common .base .Joiner ;
28
+ import com .google .common .collect .Lists ;
27
29
28
30
/**
29
31
* This class for generating DiffRows for side-by-sidy view.
@@ -74,6 +76,11 @@ public static class Builder {
74
76
private int columnWidth = 80 ;
75
77
@ Nullable
76
78
private String defaultString = "" ;
79
+ private Equalizer <String > stringEqualizer = new Equalizer <String >() {
80
+ public boolean equals (String original , String revised ) {
81
+ return Objects .equals (original , revised );
82
+ }
83
+ };
77
84
78
85
/**
79
86
* Show inline diffs in generating diff rows or not.
@@ -154,7 +161,18 @@ public Builder defaultString(@Nullable String defaultString) {
154
161
}
155
162
156
163
/**
157
- * Build the DiffRowGenerator. If some parameters is not set, the default values are used.
164
+ * Set the custom equalizer to use while comparing the lines of the revisions.
165
+ * @param stringEqualizer to use (custom one)
166
+ * @return builder with configured stringEqualizer
167
+ */
168
+ public Builder stringEqualizer (Equalizer <String > stringEqualizer ) {
169
+ this .stringEqualizer = stringEqualizer ;
170
+ return this ;
171
+ }
172
+
173
+ /**
174
+ * Build the DiffRowGenerator using the default Equalizer for rows.
175
+ * If some parameters are not set, the default values are used.
158
176
* @return the customized DiffRowGenerator
159
177
*/
160
178
public DiffRowGenerator build () {
@@ -171,19 +189,7 @@ private DiffRowGenerator(Builder builder) {
171
189
InlineNewCssClass = builder .InlineNewCssClass ;
172
190
columnWidth = builder .columnWidth ; //
173
191
defaultString = builder .defaultString ;
174
- equalizer = new Equalizer <String >() {
175
- public boolean equals (@ Nullable String original , @ Nullable String revised ) {
176
- if (ignoreWhiteSpaces ) {
177
- if (original != null ) {
178
- original = original .trim ().replaceAll ("\\ s+" , " " );
179
- }
180
- if (revised != null ) {
181
- revised = revised .trim ().replaceAll ("\\ s+" , " " );
182
- }
183
- }
184
- return Objects .equals (original , revised );
185
- }
186
- };
192
+ equalizer = builder .stringEqualizer ;
187
193
}
188
194
189
195
/**
@@ -195,6 +201,20 @@ public boolean equals(@Nullable String original, @Nullable String revised) {
195
201
* @return the DiffRows between original and revised texts
196
202
*/
197
203
public List <DiffRow > generateDiffRows (List <String > original , List <String > revised ) {
204
+ if (ignoreWhiteSpaces ) {
205
+ Function <String , String > whiteSpaceReplacer = new Function <String , String >(){
206
+ @ Override
207
+ public String apply (String string ) {
208
+ if (string == null ) {
209
+ return null ;
210
+ } else {
211
+ return string .trim ().replaceAll ("\\ s+" , " " );
212
+ }
213
+ }
214
+ };
215
+ original = Lists .transform (original , whiteSpaceReplacer );
216
+ revised = Lists .transform (revised , whiteSpaceReplacer );
217
+ }
198
218
return generateDiffRows (original , revised , DiffUtils .diff (original , revised , equalizer ));
199
219
}
200
220
0 commit comments