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

Commit 68b7aa7

Browse files
author
shotwave
committed
cleanup and fixes: comparator in Patch was broken which caused incorrect delta sorting, Chunk does not store lines as a separate field, in fact it is an attribute of the Chunk.lines collection.Minor cleanup in tests, still more to be done in geenerated udiff tests
git-svn-id: http://java-diff-utils.googlecode.com/svn/trunk@37 d8d7d024-a22d-11de-b755-fd640f38fa9d
1 parent 0d491e1 commit 68b7aa7

File tree

12 files changed

+217
-304
lines changed

12 files changed

+217
-304
lines changed

src/difflib/ChangeDelta.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public ChangeDelta(Chunk original, Chunk revised) {
4040
public void applyTo(List<Object> target) throws PatchFailedException {
4141
verify(target);
4242
int position = getOriginal().getPosition();
43-
int size = getOriginal().getSize();
43+
int size = getOriginal().size();
4444
for (int i = 0; i < size; i++) {
4545
target.remove(position);
4646
}
@@ -57,7 +57,7 @@ public void applyTo(List<Object> target) throws PatchFailedException {
5757
@Override
5858
public void restore(List<Object> target) {
5959
int position = getRevised().getPosition();
60-
int size = getRevised().getSize();
60+
int size = getRevised().size();
6161
for (int i = 0; i < size; i++) {
6262
target.remove(position);
6363
}

src/difflib/Chunk.java

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
*/
1616
package difflib;
1717

18-
import java.util.*;
18+
import java.util.Arrays;
19+
import java.util.List;
1920

2021
/**
2122
* Holds the information about the part of text involved in the diff process
@@ -31,23 +32,20 @@
3132
* @author <a href="dm.naumenko@gmail.com>Dmitry Naumenko</a>
3233
*/
3334
public class Chunk {
34-
private int position;
35-
private int size;
35+
36+
private final int position;
3637
private List<?> lines;
3738

3839
/**
3940
* Creates a chunk and saves a copy of affected lines
4041
*
4142
* @param position
4243
* the start position
43-
* @param size
44-
* the size of a Chunk
4544
* @param lines
4645
* the affected lines
4746
*/
48-
public Chunk(int position, int size, List<?> lines) {
47+
public Chunk(int position, List<?> lines) {
4948
this.position = position;
50-
this.size = size;
5149
this.lines = lines;
5250
}
5351

@@ -56,14 +54,11 @@ public Chunk(int position, int size, List<?> lines) {
5654
*
5755
* @param position
5856
* the start position
59-
* @param size
60-
* the size of a Chunk
6157
* @param lines
6258
* the affected lines
6359
*/
64-
public Chunk(int position, int size, Object[] lines) {
60+
public Chunk(int position, Object[] lines) {
6561
this.position = position;
66-
this.size = size;
6762
this.lines = Arrays.asList(lines);
6863
}
6964

@@ -78,7 +73,7 @@ public void verify(List<?> target) throws PatchFailedException {
7873
if (last() > target.size()) {
7974
throw new PatchFailedException("Incorrect Chunk: the position of chunk > target size");
8075
}
81-
for (int i = 0; i < size; i++) {
76+
for (int i = 0; i < size(); i++) {
8277
if (!target.get(position + i).equals(lines.get(i))) {
8378
throw new PatchFailedException(
8479
"Incorrect Chunk: the chunk content doesn't match the target");
@@ -92,50 +87,27 @@ public void verify(List<?> target) throws PatchFailedException {
9287
public int getPosition() {
9388
return position;
9489
}
95-
96-
/**
97-
* @param position
98-
* the start position to set
99-
*/
100-
public void setPosition(int position) {
101-
this.position = position;
102-
}
103-
104-
/**
105-
* @return the size of Chunk (size of affected lines)
106-
*/
107-
public int getSize() {
108-
return size;
109-
}
110-
111-
/**
112-
* @param size
113-
* the size of affected lines to set
114-
*/
115-
public void setSize(int size) {
116-
this.size = size;
90+
91+
public void setLines(List<?> lines) {
92+
this.lines = lines;
11793
}
118-
94+
11995
/**
12096
* @return the affected lines
12197
*/
12298
public List<?> getLines() {
12399
return lines;
124100
}
125-
126-
/**
127-
* @param lines
128-
* the affected lines to set
129-
*/
130-
public void setLines(List<?> lines) {
131-
this.lines = lines;
101+
102+
public int size() {
103+
return lines.size();
132104
}
133105

134106
/**
135107
* Returns the index of the last line of the chunk.
136108
*/
137109
public int last() {
138-
return getPosition() + getSize() - 1;
110+
return getPosition() + size() - 1;
139111
}
140112

141113
/*
@@ -149,7 +121,7 @@ public int hashCode() {
149121
int result = 1;
150122
result = prime * result + ((lines == null) ? 0 : lines.hashCode());
151123
result = prime * result + position;
152-
result = prime * result + size;
124+
result = prime * result + size();
153125
return result;
154126
}
155127

@@ -174,14 +146,12 @@ public boolean equals(Object obj) {
174146
return false;
175147
if (position != other.position)
176148
return false;
177-
if (size != other.size)
178-
return false;
179149
return true;
180150
}
181151

182152
@Override
183153
public String toString() {
184-
return "[position: " + position + ", size: " + size + ", lines: " + lines + "]";
154+
return "[position: " + position + ", size: " + size() + ", lines: " + lines + "]";
185155
}
186156

187157
}

src/difflib/DeleteDelta.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public DeleteDelta(Chunk original, Chunk revised) {
4040
public void applyTo(List<Object> target) throws PatchFailedException {
4141
verify(target);
4242
int position = getOriginal().getPosition();
43-
int size = getOriginal().getSize();
43+
int size = getOriginal().size();
4444
for (int i = 0; i < size; i++) {
4545
target.remove(position);
4646
}

src/difflib/DeltaComparator.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package difflib;
2+
3+
import java.io.Serializable;
4+
import java.util.Comparator;
5+
6+
/**
7+
* @author mksenzov
8+
*/
9+
public class DeltaComparator implements Comparator<Delta>, Serializable {
10+
11+
public static final Comparator<Delta> INSTANCE = new DeltaComparator();
12+
13+
private DeltaComparator() {
14+
}
15+
16+
public int compare(final Delta a, final Delta b) {
17+
return new Integer(a.getOriginal().getPosition()).compareTo(b.getOriginal().getPosition());
18+
}
19+
}

src/difflib/DiffRowGenerator.java

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

18-
import java.util.ArrayList;
19-
import java.util.Arrays;
20-
import java.util.Collections;
21-
import java.util.Iterator;
22-
import java.util.LinkedList;
23-
import java.util.List;
24-
2518
import difflib.DiffRow.Tag;
2619

20+
import java.util.*;
21+
2722
/**
2823
* This class for generating DiffRows for side-by-sidy view.
2924
* You can customize the way of generating. For example, show inline diffs on not, ignoring
@@ -195,17 +190,18 @@ public List<DiffRow> generateDiffRows(List<String> original, List<String> revise
195190
@SuppressWarnings("unchecked")
196191
public List<DiffRow> generateDiffRows(List<String> original, List<String> revised, Patch patch) {
197192
// normalize the lines (expand tabs, escape html entities)
198-
original = StringUtills.normalize((List<String>) original);
199-
revised = StringUtills.normalize((List<String>) revised);
193+
original = StringUtills.normalize(original);
194+
revised = StringUtills.normalize(revised);
200195

201196
// wrap to the column width
202-
original = StringUtills.wrapText((List<String>) original, this.columnWidth);
203-
revised = StringUtills.wrapText((List<String>) revised, this.columnWidth);
197+
original = StringUtills.wrapText(original, this.columnWidth);
198+
revised = StringUtills.wrapText(revised, this.columnWidth);
204199

205200
List<DiffRow> diffRows = new ArrayList<DiffRow>();
206201
int endPos = 0;
207-
for (int i = 0; i < patch.getDeltas().size(); i++) {
208-
Delta delta = patch.getDelta(i);
202+
final List<Delta> deltaList = patch.getDeltas();
203+
for (int i = 0; i < deltaList.size(); i++) {
204+
Delta delta = deltaList.get(i);
209205
Chunk orig = delta.getOriginal();
210206
Chunk rev = delta.getRevised();
211207

@@ -243,18 +239,18 @@ public List<DiffRow> generateDiffRows(List<String> original, List<String> revise
243239
addInlineDiffs(delta);
244240
}
245241
// the changed size is match
246-
if (orig.getSize() == rev.getSize()) {
247-
for (int j = 0; j < orig.getSize(); j++) {
242+
if (orig.size() == rev.size()) {
243+
for (int j = 0; j < orig.size(); j++) {
248244
diffRows.add(new DiffRow(Tag.CHANGE, (String) orig.getLines().get(j),
249245
(String) rev.getLines().get(j)));
250246
}
251-
} else if (orig.getSize() > rev.getSize()) {
252-
for (int j = 0; j < orig.getSize(); j++) {
247+
} else if (orig.size() > rev.size()) {
248+
for (int j = 0; j < orig.size(); j++) {
253249
diffRows.add(new DiffRow(Tag.CHANGE, (String) orig.getLines().get(j), rev
254250
.getLines().size() > j ? (String) rev.getLines().get(j) : ""));
255251
}
256252
} else {
257-
for (int j = 0; j < rev.getSize(); j++) {
253+
for (int j = 0; j < rev.size(); j++) {
258254
diffRows.add(new DiffRow(Tag.CHANGE, orig.getLines().size() > j ? (String) orig
259255
.getLines().get(j) : "", (String) rev.getLines().get(j)));
260256
}
@@ -294,16 +290,16 @@ private void addInlineDiffs(Delta delta) {
294290
if (inlineDelta.getClass().equals(DeleteDelta.class)) {
295291
origList = wrapInTag(origList, inlineOrig.getPosition(), inlineOrig
296292
.getPosition()
297-
+ inlineOrig.getSize() + 1, this.InlineOldTag, this.InlineOldCssClass);
293+
+ inlineOrig.size() + 1, this.InlineOldTag, this.InlineOldCssClass);
298294
} else if (inlineDelta.getClass().equals(InsertDelta.class)) {
299295
revList = wrapInTag(revList, inlineRev.getPosition(), inlineRev.getPosition()
300-
+ inlineRev.getSize() + 1, this.InlineNewTag, this.InlineNewCssClass);
296+
+ inlineRev.size() + 1, this.InlineNewTag, this.InlineNewCssClass);
301297
} else if (inlineDelta.getClass().equals(ChangeDelta.class)) {
302298
origList = wrapInTag(origList, inlineOrig.getPosition(), inlineOrig
303299
.getPosition()
304-
+ inlineOrig.getSize() + 1, this.InlineOldTag, this.InlineOldCssClass);
300+
+ inlineOrig.size() + 1, this.InlineOldTag, this.InlineOldCssClass);
305301
revList = wrapInTag(revList, inlineRev.getPosition(), inlineRev.getPosition()
306-
+ inlineRev.getSize() + 1, this.InlineNewTag, this.InlineNewCssClass);
302+
+ inlineRev.size() + 1, this.InlineNewTag, this.InlineNewCssClass);
307303
}
308304
}
309305
StringBuilder origResult = new StringBuilder(), revResult = new StringBuilder();

0 commit comments

Comments
 (0)