Skip to content
This repository was archived by the owner on Nov 12, 2019. It is now read-only.

Commit ff5cefc

Browse files
committed
refs #1: Equalizer does not have to care about ignoreWhiteSpace.
Now we have 2 classes which has to care about `ignoreWhiteSpace` and here is 2 bugs: 1. it converts `null` to empty string 2. `Builder.ignoreWhiteSpace` doesn't work when we set custom Equalizer This change solves these problem.
1 parent 05ae3ef commit ff5cefc

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/main/java/difflib/DiffRowGenerator.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
import javax.annotation.Nonnull;
2424
import javax.annotation.Nullable;
2525

26+
import com.google.common.base.Function;
2627
import 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.
@@ -76,15 +78,9 @@ public static class Builder {
7678
private String defaultString = "";
7779
private Equalizer<String> stringEqualizer = new Equalizer<String>() {
7880
public boolean equals(String original, String revised) {
79-
if (ignoreWhiteSpaces) {
80-
original = original == null ? "" : original;
81-
revised = revised == null ? "" : revised;
82-
original = original.trim().replaceAll("\\s+", " ");
83-
revised = revised.trim().replaceAll("\\s+", " ");
84-
}
85-
return original.equals(revised);
81+
return Objects.equals(original, revised);
8682
}
87-
};;
83+
};
8884

8985
/**
9086
* Show inline diffs in generating diff rows or not.
@@ -205,6 +201,20 @@ private DiffRowGenerator(Builder builder) {
205201
* @return the DiffRows between original and revised texts
206202
*/
207203
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+
}
208218
return generateDiffRows(original, revised, DiffUtils.diff(original, revised, equalizer));
209219
}
210220

0 commit comments

Comments
 (0)