Skip to content

Commit a408b66

Browse files
committed
integrated only index result
1 parent 8578a89 commit a408b66

File tree

11 files changed

+158
-83
lines changed

11 files changed

+158
-83
lines changed

src/main/java/difflib/DiffUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public static <T> Patch<T> diff(List<T> original, List<T> revised,
9898
Objects.requireNonNull(revised,"revised must not be null");
9999
Objects.requireNonNull(algorithm,"algorithm must not be null");
100100

101-
return algorithm.diff(original, revised);
101+
return Patch.generate(original, revised, algorithm.diff(original, revised));
102102
}
103103

104104
/**
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2017 java-diff-utils.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package difflib.algorithm;
17+
18+
import difflib.patch.DeltaType;
19+
20+
/**
21+
*
22+
* @author toben
23+
*/
24+
public class Change {
25+
public final DeltaType deltaType;
26+
public final int startOriginal;
27+
public final int endOriginal;
28+
public final int startRevised;
29+
public final int endRevised;
30+
31+
public Change(DeltaType deltaType, int startOriginal, int endOriginal, int startRevised, int endRevised) {
32+
this.deltaType = deltaType;
33+
this.startOriginal = startOriginal;
34+
this.endOriginal = endOriginal;
35+
this.startRevised = startRevised;
36+
this.endRevised = endRevised;
37+
}
38+
}

src/main/java/difflib/algorithm/DiffAlgorithm.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public interface DiffAlgorithm<T> {
3838
* @param revised The revised sequence. Must not be {@code null}.
3939
* @return The patch representing the diff of the given sequences. Never {@code null}.
4040
*/
41-
public default Patch<T> diff(T[] original, T[] revised) throws DiffException {
41+
public default List<Change> diff(T[] original, T[] revised) throws DiffException {
4242
return diff(Arrays.asList(original), Arrays.asList(revised));
4343
}
4444

@@ -50,5 +50,5 @@ public default Patch<T> diff(T[] original, T[] revised) throws DiffException {
5050
* @param revised The revised sequence. Must not be {@code null}.
5151
* @return The patch representing the diff of the given sequences. Never {@code null}.
5252
*/
53-
public Patch<T> diff(List<T> original, List<T> revised) throws DiffException;
53+
public List<Change> diff(List<T> original, List<T> revised) throws DiffException;
5454
}

src/main/java/difflib/algorithm/jgit/JGitDiff.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
*/
1616
package difflib.algorithm.jgit;
1717

18+
import difflib.algorithm.Change;
1819
import difflib.algorithm.DiffAlgorithm;
1920
import difflib.algorithm.DiffException;
2021
import difflib.patch.ChangeDelta;
21-
import difflib.patch.Chunk;
2222
import difflib.patch.DeleteDelta;
23+
import difflib.patch.DeltaType;
2324
import difflib.patch.InsertDelta;
24-
import difflib.patch.Patch;
25+
import java.util.ArrayList;
2526
import java.util.List;
2627
import org.eclipse.jgit.diff.Edit;
2728
import org.eclipse.jgit.diff.EditList;
@@ -38,24 +39,24 @@
3839
public class JGitDiff<T> implements DiffAlgorithm<T> {
3940

4041
@Override
41-
public Patch diff(List<T> original, List<T> revised) throws DiffException {
42+
public List<Change> diff(List<T> original, List<T> revised) throws DiffException {
4243
EditList diffList = new EditList();
4344
diffList.addAll(new HistogramDiff().diff(new DataListComparator<>(), new DataList<>(original), new DataList<>(revised)));
44-
Patch<T> patch = new Patch<>();
45+
List<Change> patch = new ArrayList<>();
4546
for (Edit edit : diffList) {
46-
Chunk<T> orgChunk = new Chunk<>(edit.getBeginA(), original.subList(edit.getBeginA(), edit.getEndA()));
47-
Chunk<T> revChunk = new Chunk<>(edit.getBeginA(), revised.subList(edit.getBeginB(), edit.getEndB()));
47+
DeltaType type = DeltaType.EQUAL;
4848
switch (edit.getType()) {
4949
case DELETE:
50-
patch.addDelta(new DeleteDelta<>(orgChunk, revChunk));
50+
type = DeltaType.DELETE;
5151
break;
5252
case INSERT:
53-
patch.addDelta(new InsertDelta<>(orgChunk, revChunk));
53+
type = DeltaType.INSERT;
5454
break;
5555
case REPLACE:
56-
patch.addDelta(new ChangeDelta<>(orgChunk, revChunk));
56+
type = DeltaType.CHANGE;
5757
break;
5858
}
59+
patch.add(new Change(type,edit.getBeginA(), edit.getEndA(), edit.getBeginB(), edit.getEndB()));
5960
}
6061
return patch;
6162
}

src/main/java/difflib/algorithm/myers/MyersDiff.java

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,12 @@
1919
*/
2020
package difflib.algorithm.myers;
2121

22+
import difflib.algorithm.Change;
2223
import difflib.algorithm.DifferentiationFailedException;
2324
import difflib.algorithm.DiffAlgorithm;
2425
import difflib.algorithm.DiffException;
25-
import difflib.patch.ChangeDelta;
26-
import difflib.patch.Chunk;
27-
import difflib.patch.DeleteDelta;
28-
import difflib.patch.Delta;
26+
import difflib.patch.DeltaType;
2927
import difflib.patch.Equalizer;
30-
import difflib.patch.InsertDelta;
3128
import difflib.patch.Patch;
3229
import java.util.ArrayList;
3330
import java.util.List;
@@ -71,7 +68,7 @@ public MyersDiff(final Equalizer<T> equalizer) {
7168
* Return empty diff if get the error while procession the difference.
7269
*/
7370
@Override
74-
public Patch<T> diff(final List<T> original, final List<T> revised) throws DiffException {
71+
public List<Change> diff(final List<T> original, final List<T> revised) throws DiffException {
7572
Objects.requireNonNull(original, "original list must not be null");
7673
Objects.requireNonNull(revised, "revised list must not be null");
7774

@@ -80,8 +77,9 @@ public Patch<T> diff(final List<T> original, final List<T> revised) throws DiffE
8077
}
8178

8279
/**
83-
* Computes the minimum diffpath that expresses de differences between the original and revised
84-
* sequences, according to Gene Myers differencing algorithm.
80+
* Computes the minimum diffpath that expresses de differences between the
81+
* original and revised sequences, according to Gene Myers differencing
82+
* algorithm.
8583
*
8684
* @param orig The original sequence.
8785
* @param rev The revised sequence.
@@ -108,9 +106,9 @@ private PathNode buildPath(final List<T> orig, final List<T> rev)
108106
final int kmiddle = middle + k;
109107
final int kplus = kmiddle + 1;
110108
final int kminus = kmiddle - 1;
111-
PathNode prev = null;
109+
PathNode prev;
112110
int i;
113-
111+
114112
if ((k == -d) || (k != d && diagonal[kminus].i < diagonal[kplus].i)) {
115113
i = diagonal[kplus].i;
116114
prev = diagonal[kplus];
@@ -153,16 +151,16 @@ private PathNode buildPath(final List<T> orig, final List<T> rev)
153151
* @param orig The original sequence.
154152
* @param rev The revised sequence.
155153
* @return A {@link Patch} script corresponding to the path.
156-
* @throws DifferentiationFailedException if a {@link Patch} could not be built from the given
157-
* path.
154+
* @throws DifferentiationFailedException if a {@link Patch} could not be
155+
* built from the given path.
158156
*/
159-
private Patch<T> buildRevision(PathNode actualPath, List<T> orig, List<T> rev) {
157+
private List<Change> buildRevision(PathNode actualPath, List<T> orig, List<T> rev) {
160158
Objects.requireNonNull(actualPath, "path is null");
161159
Objects.requireNonNull(orig, "original sequence is null");
162160
Objects.requireNonNull(rev, "revised sequence is null");
163161

164162
PathNode path = actualPath;
165-
Patch<T> patch = new Patch<>();
163+
List<Change> changes = new ArrayList<>();
166164
if (path.isSnake()) {
167165
path = path.prev;
168166
}
@@ -177,35 +175,29 @@ private Patch<T> buildRevision(PathNode actualPath, List<T> orig, List<T> rev) {
177175
int ianchor = path.i;
178176
int janchor = path.j;
179177

180-
Chunk<T> original = new Chunk<>(ianchor, copyOfRange(orig, ianchor, i));
181-
Chunk<T> revised = new Chunk<>(janchor, copyOfRange(rev, janchor, j));
182-
Delta<T> delta = null;
183-
if (original.size() == 0 && revised.size() != 0) {
184-
delta = new InsertDelta<>(original, revised);
185-
} else if (original.size() > 0 && revised.size() == 0) {
186-
delta = new DeleteDelta<>(original, revised);
178+
if (ianchor == i && janchor != j) {
179+
changes.add(new Change(DeltaType.INSERT, ianchor, i, janchor, j));
180+
} else if (ianchor != i && janchor == j) {
181+
changes.add(new Change(DeltaType.DELETE, ianchor, i, janchor, j));
187182
} else {
188-
delta = new ChangeDelta<>(original, revised);
183+
changes.add(new Change(DeltaType.CHANGE, ianchor, i, janchor, j));
189184
}
190-
191-
patch.addDelta(delta);
185+
// Chunk<T> original = new Chunk<>(ianchor, copyOfRange(orig, ianchor, i));
186+
// Chunk<T> revised = new Chunk<>(janchor, copyOfRange(rev, janchor, j));
187+
// Delta<T> delta = null;
188+
// if (original.size() == 0 && revised.size() != 0) {
189+
// delta = new InsertDelta<>(original, revised);
190+
// } else if (original.size() > 0 && revised.size() == 0) {
191+
// delta = new DeleteDelta<>(original, revised);
192+
// } else {
193+
// delta = new ChangeDelta<>(original, revised);
194+
// }
195+
//
196+
// patch.addDelta(delta);
192197
if (path.isSnake()) {
193198
path = path.prev;
194199
}
195200
}
196-
return patch;
197-
}
198-
199-
/**
200-
* Creates a new list containing the elements returned by {@link List#subList(int, int)}.
201-
*
202-
* @param original The original sequence. Must not be {@code null}.
203-
* @param fromIndex low endpoint (inclusive) of the subList.
204-
* @param to high endpoint (exclusive) of the subList.
205-
* @return A new list of the specified range within the original list.
206-
*
207-
*/
208-
private List<T> copyOfRange(final List<T> original, final int fromIndex, final int to) {
209-
return new ArrayList<>(original.subList(fromIndex, to));
201+
return changes;
210202
}
211203
}

src/main/java/difflib/patch/Delta.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,6 @@ public abstract class Delta<T> {
3333
private final Chunk<T> original;
3434
private final Chunk<T> revised;
3535

36-
/**
37-
* Specifies the type of the delta.
38-
*
39-
*/
40-
public static enum DeltaType {
41-
/**
42-
* A change in the original.
43-
*/
44-
CHANGE,
45-
/**
46-
* A delete from the original.
47-
*/
48-
DELETE,
49-
/**
50-
* An insert into the original.
51-
*/
52-
INSERT
53-
}
54-
5536
/**
5637
* Construct the delta for original and revised chunks
5738
*
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2017 java-diff-utils.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package difflib.patch;
17+
18+
/**
19+
* Specifies the type of the delta.
20+
*
21+
*/
22+
public enum DeltaType {
23+
/**
24+
* A change in the original.
25+
*/
26+
CHANGE,
27+
/**
28+
* A delete from the original.
29+
*/
30+
DELETE,
31+
/**
32+
* An insert into the original.
33+
*/
34+
INSERT,
35+
36+
/**
37+
* An do nothing.
38+
*/
39+
EQUAL
40+
}

src/main/java/difflib/patch/Patch.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
*/
2020
package difflib.patch;
2121

22+
import difflib.algorithm.Change;
23+
import static difflib.patch.DeltaType.DELETE;
24+
import static difflib.patch.DeltaType.INSERT;
25+
import java.util.ArrayList;
2226
import java.util.Collections;
2327
import static java.util.Comparator.comparing;
2428
import java.util.LinkedList;
@@ -90,4 +94,24 @@ public List<Delta<T>> getDeltas() {
9094
public String toString() {
9195
return "Patch{" + "deltas=" + deltas + '}';
9296
}
97+
98+
public static <T> Patch<T> generate(List<T> original, List<T> revised, List<Change> changes) {
99+
Patch<T> patch = new Patch<>();
100+
for (Change change : changes) {
101+
Chunk<T> orgChunk = new Chunk<>(change.startOriginal, new ArrayList<>(original.subList(change.startOriginal, change.endOriginal)));
102+
Chunk<T> revChunk = new Chunk<>(change.startOriginal, new ArrayList<>(revised.subList(change.startRevised, change.endRevised)));
103+
switch (change.deltaType) {
104+
case DELETE:
105+
patch.addDelta(new DeleteDelta<>(orgChunk, revChunk));
106+
break;
107+
case INSERT:
108+
patch.addDelta(new InsertDelta<>(orgChunk, revChunk));
109+
break;
110+
case CHANGE:
111+
patch.addDelta(new ChangeDelta<>(orgChunk, revChunk));
112+
break;
113+
}
114+
}
115+
return patch;
116+
}
93117
}

src/test/java/difflib/algorithm/jgit/JGitDiffTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void tearDown() {
6363
public void testDiff() throws DiffException, PatchFailedException {
6464
List<String> orgList = Arrays.asList("A","B","C","A","B","B","A");
6565
List<String> revList = Arrays.asList("C","B","A","B","A","C");
66-
final Patch<String> patch = new JGitDiff().diff(orgList, revList);
66+
final Patch<String> patch = Patch.generate(orgList, revList, new JGitDiff().diff(orgList, revList));
6767
System.out.println(patch);
6868
assertNotNull(patch);
6969
assertEquals(3, patch.getDeltas().size());

src/test/java/difflib/algorithm/jgit/LRJGitDiffTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void testPossibleDiffHangOnLargeDatasetDnaumenkoIssue26() throws IOExcept
6363
List<String> original = readStringListFromInputStream(zip.getInputStream(zip.getEntry("ta")));
6464
List<String> revised = readStringListFromInputStream(zip.getInputStream(zip.getEntry("tb")));
6565

66-
Patch<String> patch = new JGitDiff().diff(original, revised);
66+
Patch<String> patch = Patch.generate(original, revised, new JGitDiff().diff(original, revised));
6767

6868
assertEquals(34, patch.getDeltas().size());
6969

0 commit comments

Comments
 (0)