25
25
import com .github .difflib .patch .Delta ;
26
26
import com .github .difflib .patch .Patch ;
27
27
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 .*;
33
30
import java .util .function .BiPredicate ;
31
+
34
32
import static java .util .stream .Collectors .joining ;
35
33
36
34
/**
37
35
* Implements the difference and patching engine
38
36
*
39
37
* @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
41
40
*/
42
41
public final class DiffUtils {
43
42
@@ -47,18 +46,19 @@ public final class DiffUtils {
47
46
*
48
47
* @param original The original text. Must not be {@code null}.
49
48
* @param revised The revised text. Must not be {@code null}.
49
+ * @param linesBeforeAfter - Amount of lines for before and after chunk content
50
50
* @return The patch describing the difference between the original and revised sequences. Never
51
51
* {@code null}.
52
52
*/
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 );
55
55
}
56
56
57
57
/**
58
58
* Computes the difference between the original and revised text.
59
59
*/
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 );
62
62
}
63
63
64
64
/**
@@ -70,16 +70,17 @@ public static Patch<String> diff(String originalText, String revisedText) throws
70
70
*
71
71
* @param equalizer the equalizer object to replace the default compare algorithm
72
72
* (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
73
74
* @return The patch describing the difference between the original and revised sequences. Never
74
75
* {@code null}.
75
76
*/
76
77
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 {
78
79
if (equalizer != null ) {
79
80
return DiffUtils .diff (original , revised ,
80
- new MyersDiff <>(equalizer ));
81
+ new MyersDiff <>(equalizer ), linesBeforeAfter );
81
82
}
82
- return DiffUtils .diff (original , revised , new MyersDiff <>());
83
+ return DiffUtils .diff (original , revised , new MyersDiff <>(), linesBeforeAfter );
83
84
}
84
85
85
86
/**
@@ -89,16 +90,17 @@ public static <T> Patch<T> diff(List<T> original, List<T> revised,
89
90
* @param original The original text. Must not be {@code null}.
90
91
* @param revised The revised text. Must not be {@code null}.
91
92
* @param algorithm The diff algorithm. Must not be {@code null}.
93
+ * @param linesBeforeAfter - Amount of lines for before and after chunk content
92
94
* @return The patch describing the difference between the original and revised sequences. Never
93
95
* {@code null}.
94
96
*/
95
97
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 {
97
99
Objects .requireNonNull (original ,"original must not be null" );
98
100
Objects .requireNonNull (revised ,"revised must not be null" );
99
101
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 );
102
104
}
103
105
104
106
/**
@@ -108,9 +110,10 @@ public static <T> Patch<T> diff(List<T> original, List<T> revised,
108
110
*
109
111
* @param original
110
112
* @param revised
113
+ * @param linesBeforeAfter - Amount of lines for before and after chunk content
111
114
* @return
112
115
*/
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 {
114
117
List <String > origList = new ArrayList <>();
115
118
List <String > revList = new ArrayList <>();
116
119
for (Character character : original .toCharArray ()) {
@@ -119,7 +122,7 @@ public static Patch<String> diffInline(String original, String revised) throws D
119
122
for (Character character : revised .toCharArray ()) {
120
123
revList .add (character .toString ());
121
124
}
122
- Patch <String > patch = DiffUtils .diff (origList , revList );
125
+ Patch <String > patch = DiffUtils .diff (origList , revList , linesBeforeAfter );
123
126
for (Delta <String > delta : patch .getDeltas ()) {
124
127
delta .getOriginal ().setLines (compressLines (delta .getOriginal ().getLines (), "" ));
125
128
delta .getRevised ().setLines (compressLines (delta .getRevised ().getLines (), "" ));
0 commit comments