2323import javax .annotation .Nonnull ;
2424import javax .annotation .Nullable ;
2525
26+ import com .google .common .base .Function ;
2627import com .google .common .base .Joiner ;
28+ import com .google .common .collect .Lists ;
2729
2830/**
2931 * This class for generating DiffRows for side-by-sidy view.
@@ -74,6 +76,11 @@ public static class Builder {
7476 private int columnWidth = 80 ;
7577 @ Nullable
7678 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+ };
7784
7885 /**
7986 * Show inline diffs in generating diff rows or not.
@@ -154,7 +161,18 @@ public Builder defaultString(@Nullable String defaultString) {
154161 }
155162
156163 /**
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.
158176 * @return the customized DiffRowGenerator
159177 */
160178 public DiffRowGenerator build () {
@@ -171,19 +189,7 @@ private DiffRowGenerator(Builder builder) {
171189 InlineNewCssClass = builder .InlineNewCssClass ;
172190 columnWidth = builder .columnWidth ; //
173191 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 ;
187193 }
188194
189195 /**
@@ -195,6 +201,20 @@ public boolean equals(@Nullable String original, @Nullable String revised) {
195201 * @return the DiffRows between original and revised texts
196202 */
197203 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+ }
198218 return generateDiffRows (original , revised , DiffUtils .diff (original , revised , equalizer ));
199219 }
200220
0 commit comments