Skip to content
This repository was archived by the owner on Oct 21, 2020. It is now read-only.

Commit 3d639c1

Browse files
committed
Added Chunk surroundings
1 parent 88146c6 commit 3d639c1

File tree

13 files changed

+401
-272
lines changed

13 files changed

+401
-272
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ This is a test ~senctence~**for diffutils**.
5050
But it can easily replaced by any other which is better for handing your texts. I have plan to add implementation of some in future.
5151

5252
### Changelog ###
53+
* Version 2.2
54+
* Added chunk to support surroundings of differences
5355
* Version 2.0
5456
* switch to maven and removed other artifacts
5557
* changed groupid to **com.github.java-diff-utils** due to different forks at github

pom.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<groupId>com.github.java-diff-utils</groupId>
44
<artifactId>diffutils</artifactId>
55
<packaging>jar</packaging>
6-
<version>2.1-SNAPSHOT</version>
6+
<version>2.2-SNAPSHOT</version>
77

88
<name>java-diff-utils</name>
99
<description>The DiffUtils library for computing diffs, applying patches, generationg side-by-side view in Java.</description>
@@ -21,7 +21,7 @@
2121
<connection>scm:git:https://github.com/wumpz/java-diff-utils.git</connection>
2222
<developerConnection>scm:git:ssh://git@github.com:wumpz/java-diff-utils.git</developerConnection>
2323
<url>https://github.com/wumpz/java-diff-utils.git</url>
24-
<tag>diffutils-2.0</tag>
24+
<tag>diffutils-2.2</tag>
2525
</scm>
2626

2727
<issueManagement>
@@ -34,6 +34,10 @@
3434
</organization>
3535

3636
<developers>
37+
<developer>
38+
<name>Christopher Sontag</name>
39+
<email>ch.sontag@gmail.com</email>
40+
</developer>
3741
<developer>
3842
<name>Tobias Warneke</name>
3943
<email>t.warneke@gmx.net</email>

src/main/java/com/github/difflib/DiffUtils.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,18 @@
2525
import com.github.difflib.patch.Delta;
2626
import com.github.difflib.patch.Patch;
2727
import com.github.difflib.patch.PatchFailedException;
28-
import java.util.ArrayList;
29-
import java.util.Arrays;
30-
import java.util.Collections;
31-
import java.util.List;
32-
import java.util.Objects;
28+
29+
import java.util.*;
3330
import java.util.function.BiPredicate;
31+
3432
import static java.util.stream.Collectors.joining;
3533

3634
/**
3735
* Implements the difference and patching engine
3836
*
3937
* @author <a href="dm.naumenko@gmail.com">Dmitry Naumenko</a>
40-
* @version 0.4.1
38+
* @author <a href="ch.sontag@gmail.com">Christopher Sontag</a>
39+
* @version 0.4.2
4140
*/
4241
public final class DiffUtils {
4342

@@ -47,18 +46,19 @@ public final class DiffUtils {
4746
*
4847
* @param original The original text. Must not be {@code null}.
4948
* @param revised The revised text. Must not be {@code null}.
49+
* @param linesBeforeAfter - Amount of lines for before and after chunk content
5050
* @return The patch describing the difference between the original and revised sequences. Never
5151
* {@code null}.
5252
*/
53-
public static <T> Patch<T> diff(List<T> original, List<T> revised) throws DiffException {
54-
return DiffUtils.diff(original, revised, new MyersDiff<>());
53+
public static <T> Patch<T> diff(List<T> original, List<T> revised, int linesBeforeAfter) throws DiffException {
54+
return DiffUtils.diff(original, revised, new MyersDiff<>(), linesBeforeAfter);
5555
}
5656

5757
/**
5858
* Computes the difference between the original and revised text.
5959
*/
60-
public static Patch<String> diff(String originalText, String revisedText) throws DiffException {
61-
return DiffUtils.diff(Arrays.asList(originalText.split("\n")), Arrays.asList(revisedText.split("\n")));
60+
public static Patch<String> diff(String originalText, String revisedText, int linesBeforeAfter) throws DiffException {
61+
return DiffUtils.diff(Arrays.asList(originalText.split("\n")), Arrays.asList(revisedText.split("\n")), linesBeforeAfter);
6262
}
6363

6464
/**
@@ -70,16 +70,17 @@ public static Patch<String> diff(String originalText, String revisedText) throws
7070
*
7171
* @param equalizer the equalizer object to replace the default compare algorithm
7272
* (Object.equals). If {@code null} the default equalizer of the default algorithm is used..
73+
* @param linesBeforeAfter - Amount of lines for before and after chunk content
7374
* @return The patch describing the difference between the original and revised sequences. Never
7475
* {@code null}.
7576
*/
7677
public static <T> Patch<T> diff(List<T> original, List<T> revised,
77-
BiPredicate<T,T> equalizer) throws DiffException {
78+
BiPredicate<T, T> equalizer, int linesBeforeAfter) throws DiffException {
7879
if (equalizer != null) {
7980
return DiffUtils.diff(original, revised,
80-
new MyersDiff<>(equalizer));
81+
new MyersDiff<>(equalizer), linesBeforeAfter);
8182
}
82-
return DiffUtils.diff(original, revised, new MyersDiff<>());
83+
return DiffUtils.diff(original, revised, new MyersDiff<>(), linesBeforeAfter);
8384
}
8485

8586
/**
@@ -89,16 +90,17 @@ public static <T> Patch<T> diff(List<T> original, List<T> revised,
8990
* @param original The original text. Must not be {@code null}.
9091
* @param revised The revised text. Must not be {@code null}.
9192
* @param algorithm The diff algorithm. Must not be {@code null}.
93+
* @param linesBeforeAfter - Amount of lines for before and after chunk content
9294
* @return The patch describing the difference between the original and revised sequences. Never
9395
* {@code null}.
9496
*/
9597
public static <T> Patch<T> diff(List<T> original, List<T> revised,
96-
DiffAlgorithm<T> algorithm) throws DiffException {
98+
DiffAlgorithm<T> algorithm, int linesBeforeAfter) throws DiffException {
9799
Objects.requireNonNull(original,"original must not be null");
98100
Objects.requireNonNull(revised,"revised must not be null");
99101
Objects.requireNonNull(algorithm,"algorithm must not be null");
100-
101-
return Patch.generate(original, revised, algorithm.diff(original, revised));
102+
103+
return Patch.generate(original, revised, algorithm.diff(original, revised), linesBeforeAfter);
102104
}
103105

104106
/**
@@ -108,9 +110,10 @@ public static <T> Patch<T> diff(List<T> original, List<T> revised,
108110
*
109111
* @param original
110112
* @param revised
113+
* @param linesBeforeAfter - Amount of lines for before and after chunk content
111114
* @return
112115
*/
113-
public static Patch<String> diffInline(String original, String revised) throws DiffException {
116+
public static Patch<String> diffInline(String original, String revised, int linesBeforeAfter) throws DiffException {
114117
List<String> origList = new ArrayList<>();
115118
List<String> revList = new ArrayList<>();
116119
for (Character character : original.toCharArray()) {
@@ -119,7 +122,7 @@ public static Patch<String> diffInline(String original, String revised) throws D
119122
for (Character character : revised.toCharArray()) {
120123
revList.add(character.toString());
121124
}
122-
Patch<String> patch = DiffUtils.diff(origList, revList);
125+
Patch<String> patch = DiffUtils.diff(origList, revList, linesBeforeAfter);
123126
for (Delta<String> delta : patch.getDeltas()) {
124127
delta.getOriginal().setLines(compressLines(delta.getOriginal().getLines(), ""));
125128
delta.getRevised().setLines(compressLines(delta.getRevised().getLines(), ""));

src/main/java/com/github/difflib/patch/Chunk.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020
package com.github.difflib.patch;
2121

22+
import java.util.ArrayList;
2223
import java.util.Arrays;
2324
import java.util.List;
2425

@@ -31,14 +32,17 @@
3132
* {@link java.lang.Object#hashCode hashCode()} and {@link java.lang.Object#equals equals()}
3233
* correctly can be subject to differencing using this library.
3334
* </p>
35+
* T The type of the compared elements in the 'lines'.
3436
*
35-
* @author <a href="dm.naumenko@gmail.com>Dmitry Naumenko</a>
36-
* @param T The type of the compared elements in the 'lines'.
37+
* @author <a href="dm.naumenko@gmail.com">Dmitry Naumenko</a>
38+
* @author <a href="ch.sontag@gmail.com">Christopher Sontag</a>
3739
*/
3840
public final class Chunk<T> {
3941

4042
private final int position;
4143
private List<T> lines;
44+
private List<T> before = new ArrayList<>();
45+
private List<T> after = new ArrayList<>();
4246

4347
/**
4448
* Creates a chunk and saves a copy of affected lines
@@ -108,6 +112,28 @@ public int last() {
108112
return getPosition() + size() - 1;
109113
}
110114

115+
/**
116+
* @return the lines before the affected lines
117+
*/
118+
public List<T> getBefore() {
119+
return before;
120+
}
121+
122+
public void setBefore(List<T> before) {
123+
this.before = before;
124+
}
125+
126+
/**
127+
* @return the lines after the affected lines
128+
*/
129+
public List<T> getAfter() {
130+
return after;
131+
}
132+
133+
public void setAfter(List<T> after) {
134+
this.after = after;
135+
}
136+
111137
/*
112138
* (non-Javadoc)
113139
*
@@ -147,6 +173,8 @@ public boolean equals(Object obj) {
147173
} else if (!lines.equals(other.lines)) {
148174
return false;
149175
}
176+
if (!before.equals(other.before)) return false;
177+
if (!after.equals(other.after)) return false;
150178
return position == other.position;
151179
}
152180

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

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,20 @@
2020
package com.github.difflib.patch;
2121

2222
import com.github.difflib.algorithm.Change;
23-
import static com.github.difflib.patch.DeltaType.DELETE;
24-
import static com.github.difflib.patch.DeltaType.INSERT;
23+
2524
import java.util.ArrayList;
2625
import java.util.Collections;
27-
import static java.util.Comparator.comparing;
2826
import java.util.List;
2927
import java.util.ListIterator;
3028

29+
import static java.util.Comparator.comparing;
30+
3131
/**
3232
* Describes the patch holding all deltas between the original and revised texts.
3333
*
34+
* T The type of the compared elements in the 'lines'.
3435
* @author <a href="dm.naumenko@gmail.com">Dmitry Naumenko</a>
35-
* @param T The type of the compared elements in the 'lines'.
36+
* @author <a href="ch.sontag@gmail.com">Christopher Sontag</a>
3637
*/
3738
public final class Patch<T> {
3839

@@ -41,11 +42,11 @@ public final class Patch<T> {
4142
public Patch() {
4243
this(10);
4344
}
44-
45+
4546
public Patch(int estimatedPatchSize) {
46-
deltas = new ArrayList<>(estimatedPatchSize);
47+
deltas = new ArrayList<>(estimatedPatchSize);
4748
}
48-
49+
4950
/**
5051
* Apply this patch to the given target
5152
*
@@ -87,26 +88,34 @@ public void addDelta(Delta<T> delta) {
8788
deltas.add(delta);
8889
}
8990

90-
/**
91-
* Get the list of computed deltas
92-
*
93-
* @return the deltas
94-
*/
95-
public List<Delta<T>> getDeltas() {
96-
Collections.sort(deltas, comparing(d -> d.getOriginal().getPosition()));
97-
return deltas;
98-
}
99-
100-
@Override
101-
public String toString() {
102-
return "Patch{" + "deltas=" + deltas + '}';
103-
}
104-
105-
public static <T> Patch<T> generate(List<T> original, List<T> revised, List<Change> changes) {
91+
public static <T> Patch<T> generate(List<T> original, List<T> revised, List<Change> changes, int surroundingLines) {
10692
Patch<T> patch = new Patch<>(changes.size());
10793
for (Change change : changes) {
94+
10895
Chunk<T> orgChunk = new Chunk<>(change.startOriginal, new ArrayList<>(original.subList(change.startOriginal, change.endOriginal)));
96+
if (change.startOriginal - surroundingLines >= 0) {
97+
orgChunk.setBefore(new ArrayList<>(original.subList(change.startOriginal - surroundingLines, change.startOriginal)));
98+
} else {
99+
orgChunk.setBefore(new ArrayList<>(original.subList(0, change.startOriginal)));
100+
}
101+
if (change.endOriginal + surroundingLines <= original.size()) {
102+
orgChunk.setAfter(new ArrayList<>(original.subList(change.endOriginal, change.endOriginal + surroundingLines)));
103+
} else {
104+
orgChunk.setAfter(new ArrayList<>(original.subList(change.endOriginal, original.size())));
105+
}
106+
109107
Chunk<T> revChunk = new Chunk<>(change.startRevised, new ArrayList<>(revised.subList(change.startRevised, change.endRevised)));
108+
if (change.startRevised - surroundingLines >= 0) {
109+
revChunk.setBefore(new ArrayList<>(revised.subList(change.startRevised - surroundingLines, change.startRevised)));
110+
} else {
111+
revChunk.setBefore(new ArrayList<>(revised.subList(0, change.startRevised)));
112+
}
113+
if (change.endRevised + surroundingLines <= revised.size()) {
114+
revChunk.setAfter(new ArrayList<>(revised.subList(change.endRevised, change.endRevised + surroundingLines)));
115+
} else {
116+
revChunk.setAfter(new ArrayList<>(revised.subList(change.endRevised, revised.size())));
117+
}
118+
110119
switch (change.deltaType) {
111120
case DELETE:
112121
patch.addDelta(new DeleteDelta<>(orgChunk, revChunk));
@@ -121,4 +130,19 @@ public static <T> Patch<T> generate(List<T> original, List<T> revised, List<Chan
121130
}
122131
return patch;
123132
}
133+
134+
@Override
135+
public String toString() {
136+
return "Patch{" + "deltas=" + deltas + '}';
137+
}
138+
139+
/**
140+
* Get the list of computed deltas
141+
*
142+
* @return the deltas
143+
*/
144+
public List<Delta<T>> getDeltas() {
145+
Collections.sort(deltas, comparing(d -> d.getOriginal().getPosition()));
146+
return deltas;
147+
}
124148
}

0 commit comments

Comments
 (0)