2525import com .github .difflib .patch .Delta ;
2626import com .github .difflib .patch .Patch ;
2727import 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 .*;
3330import java .util .function .BiPredicate ;
31+
3432import static java .util .stream .Collectors .joining ;
3533
3634/**
3735 * Implements the difference and patching engine
3836 *
3937 * @author <a href="[email protected] ">Dmitry Naumenko</a> 40- * @version 0.4.1
38+ * @author <a href="[email protected] ">Christopher Sontag</a> 39+ * @version 0.4.2
4140 */
4241public 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 (), "" ));
0 commit comments