1818import difflib .myers .Equalizer ;
1919import difflib .myers .MyersDiff ;
2020
21+ import java .io .File ;
22+ import java .io .IOException ;
2123import java .util .ArrayList ;
2224import java .util .List ;
2325import java .util .regex .Matcher ;
2426import java .util .regex .Pattern ;
2527
28+ import javax .annotation .Nonnull ;
29+ import javax .annotation .Nullable ;
30+
31+ import com .google .common .base .Charsets ;
32+ import com .google .common .io .Files ;
33+
2634/**
2735 * Implements the difference and patching engine
2836 *
@@ -36,6 +44,21 @@ public class DiffUtils {
3644 private static Pattern unifiedDiffChunkRe = Pattern
3745 .compile ("^@@\\ s+-(?:(\\ d+)(?:,(\\ d+))?)\\ s+\\ +(?:(\\ d+)(?:,(\\ d+))?)\\ s+@@$" );
3846
47+ @ Nonnull
48+ public Patch <String > diff (@ Nonnull File original , @ Nonnull File revised ) throws IOException {
49+ return diff (Files .readLines (original , Charsets .UTF_8 ), Files .readLines (revised , Charsets .UTF_8 ));
50+ }
51+
52+ @ Nonnull
53+ public Patch <String > diff (@ Nonnull File original , @ Nonnull File revised , @ Nonnull DiffAlgorithm <String > algorithm ) throws IOException {
54+ return diff (Files .readLines (original , Charsets .UTF_8 ), Files .readLines (revised , Charsets .UTF_8 ), algorithm );
55+ }
56+
57+ @ Nonnull
58+ public Patch <String > diff (@ Nonnull File original , @ Nonnull File revised , @ Nullable Equalizer <String > equalizer ) throws IOException {
59+ return diff (Files .readLines (original , Charsets .UTF_8 ), Files .readLines (revised , Charsets .UTF_8 ), equalizer );
60+ }
61+
3962 /**
4063 * Computes the difference between the original and revised list of elements
4164 * with default diff algorithm
@@ -47,6 +70,7 @@ public class DiffUtils {
4770 * @return The patch describing the difference between the original and
4871 * revised sequences. Never {@code null}.
4972 */
73+ @ Nonnull
5074 public static <T > Patch <T > diff (List <T > original , List <T > revised ) {
5175 return DiffUtils .diff (original , revised , new MyersDiff <T >());
5276 }
@@ -67,6 +91,7 @@ public static <T> Patch<T> diff(List<T> original, List<T> revised) {
6791 * @return The patch describing the difference between the original and
6892 * revised sequences. Never {@code null}.
6993 */
94+ @ Nonnull
7095 public static <T > Patch <T > diff (List <T > original , List <T > revised ,
7196 Equalizer <T > equalizer ) {
7297 if (equalizer != null ) {
@@ -89,6 +114,7 @@ public static <T> Patch<T> diff(List<T> original, List<T> revised,
89114 * @return The patch describing the difference between the original and
90115 * revised sequences. Never {@code null}.
91116 */
117+ @ Nonnull
92118 public static <T > Patch <T > diff (List <T > original , List <T > revised ,
93119 DiffAlgorithm <T > algorithm ) {
94120 if (original == null ) {
@@ -114,6 +140,7 @@ public static <T> Patch<T> diff(List<T> original, List<T> revised,
114140 * @throws PatchFailedException
115141 * if can't apply patch
116142 */
143+ @ Nonnull
117144 public static <T > List <T > patch (List <T > original , Patch <T > patch )
118145 throws PatchFailedException {
119146 return patch .applyTo (original );
0 commit comments