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

Commit 303a4f0

Browse files
committed
let user uses other string to represent line which does not exist.
1 parent 2833d5b commit 303a4f0

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

src/main/java/difflib/DiffRow.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,27 @@
1515
*/
1616
package difflib;
1717

18+
import static com.google.common.base.Preconditions.checkNotNull;
19+
20+
import javax.annotation.Nonnull;
21+
import javax.annotation.Nullable;
22+
1823
/**
1924
* Describes the diff row in form [tag, oldLine, newLine) for showing the
2025
* difference between two texts
2126
*
2227
* @author <a href="dm.naumenko@gmail.com">Dmitry Naumenko</a>
2328
*/
2429
public class DiffRow {
30+
@Nonnull
2531
private Tag tag;
32+
@Nullable
2633
private String oldLine;
34+
@Nullable
2735
private String newLine;
2836

29-
public DiffRow(Tag tag, String oldLine, String newLine) {
30-
this.tag = tag;
37+
public DiffRow(@Nonnull Tag tag, @Nullable String oldLine, @Nullable String newLine) {
38+
this.tag = checkNotNull(tag);
3139
this.oldLine = oldLine;
3240
this.newLine = newLine;
3341
}
@@ -39,6 +47,7 @@ public enum Tag {
3947
/**
4048
* @return the tag
4149
*/
50+
@Nonnull
4251
public Tag getTag() {
4352
return tag;
4453
}
@@ -53,28 +62,30 @@ public void setTag(Tag tag) {
5362
/**
5463
* @return the oldLine
5564
*/
65+
@Nullable
5666
public String getOldLine() {
5767
return oldLine;
5868
}
5969

6070
/**
6171
* @param oldLine the oldLine to set
6272
*/
63-
public void setOldLine(String oldLine) {
73+
public void setOldLine(@Nullable String oldLine) {
6474
this.oldLine = oldLine;
6575
}
6676

6777
/**
6878
* @return the newLine
6979
*/
80+
@Nullable
7081
public String getNewLine() {
7182
return newLine;
7283
}
7384

7485
/**
7586
* @param newLine the newLine to set
7687
*/
77-
public void setNewLine(String newLine) {
88+
public void setNewLine(@Nullable String newLine) {
7889
this.newLine = newLine;
7990
}
8091

src/main/java/difflib/DiffRowGenerator.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020

2121
import java.util.*;
2222

23+
import javax.annotation.Nonnull;
24+
import javax.annotation.Nullable;
25+
2326
/**
2427
* This class for generating DiffRows for side-by-sidy view.
2528
* You can customize the way of generating. For example, show inline diffs on not, ignoring
@@ -49,6 +52,8 @@ public class DiffRowGenerator {
4952
private final String InlineOldCssClass;
5053
private final String InlineNewCssClass;
5154
private final int columnWidth;
55+
@Nullable
56+
private final String defaultString;
5257
private final Equalizer<String> equalizer;
5358

5459
/**
@@ -65,6 +70,8 @@ public static class Builder {
6570
private String InlineOldCssClass = "editOldInline";
6671
private String InlineNewCssClass = "editNewInline";
6772
private int columnWidth = 80;
73+
@Nullable
74+
private String defaultString = "";
6875

6976
/**
7077
* Show inline diffs in generating diff rows or not.
@@ -148,6 +155,12 @@ public Builder columnWidth(int width) {
148155
return this;
149156
}
150157

158+
@Nonnull
159+
public Builder defaultString(@Nullable String defaultString) {
160+
this.defaultString = defaultString;
161+
return this;
162+
}
163+
151164
/**
152165
* Build the DiffRowGenerator. If some parameters is not set, the default values are used.
153166
* @return the customized DiffRowGenerator
@@ -166,6 +179,7 @@ private DiffRowGenerator(Builder builder) {
166179
InlineOldCssClass = builder.InlineOldCssClass;
167180
InlineNewCssClass = builder.InlineNewCssClass;
168181
columnWidth = builder.columnWidth; //
182+
defaultString = builder.defaultString;
169183
equalizer = new Equalizer<String>() {
170184
public boolean equals(String original, String revised) {
171185
if (ignoreWhiteSpaces) {
@@ -242,7 +256,7 @@ public List<DiffRow> generateDiffRows(List<String> original, List<String> revise
242256
if (delta.getClass().equals(InsertDelta.class)) {
243257
endPos = orig.last() + 1;
244258
for (String line : (List<String>) rev.getLines()) {
245-
diffRows.add(new DiffRow(Tag.INSERT, "", line));
259+
diffRows.add(new DiffRow(Tag.INSERT, defaultString, line));
246260
}
247261
continue;
248262
}
@@ -251,7 +265,7 @@ public List<DiffRow> generateDiffRows(List<String> original, List<String> revise
251265
if (delta.getClass().equals(DeleteDelta.class)) {
252266
endPos = orig.last() + 1;
253267
for (String line : (List<String>) orig.getLines()) {
254-
diffRows.add(new DiffRow(Tag.DELETE, line, ""));
268+
diffRows.add(new DiffRow(Tag.DELETE, line, defaultString));
255269
}
256270
continue;
257271
}
@@ -268,12 +282,12 @@ public List<DiffRow> generateDiffRows(List<String> original, List<String> revise
268282
} else if (orig.size() > rev.size()) {
269283
for (int j = 0; j < orig.size(); j++) {
270284
diffRows.add(new DiffRow(Tag.CHANGE, (String) orig.getLines().get(j), rev
271-
.getLines().size() > j ? (String) rev.getLines().get(j) : ""));
285+
.getLines().size() > j ? (String) rev.getLines().get(j) : defaultString));
272286
}
273287
} else {
274288
for (int j = 0; j < rev.size(); j++) {
275289
diffRows.add(new DiffRow(Tag.CHANGE, orig.getLines().size() > j ? (String) orig
276-
.getLines().get(j) : "", (String) rev.getLines().get(j)));
290+
.getLines().get(j) : defaultString, (String) rev.getLines().get(j)));
277291
}
278292
}
279293
endPos = orig.last() + 1;

0 commit comments

Comments
 (0)