|
15 | 15 | */ |
16 | 16 | package com.github.difflib; |
17 | 17 |
|
| 18 | +import com.github.difflib.algorithm.DiffAlgorithmFactory; |
18 | 19 | import com.github.difflib.algorithm.DiffAlgorithmI; |
19 | 20 | import com.github.difflib.algorithm.DiffAlgorithmListener; |
20 | | -import com.github.difflib.algorithm.myers.MyersDiff; |
| 21 | +import com.github.difflib.algorithm.myers.MeyersDiff; |
21 | 22 | import com.github.difflib.patch.AbstractDelta; |
22 | 23 | import com.github.difflib.patch.Patch; |
23 | 24 | import com.github.difflib.patch.PatchFailedException; |
|
34 | 35 | public final class DiffUtils { |
35 | 36 |
|
36 | 37 | /** |
37 | | - * Computes the difference between the original and revised list of elements with default diff |
38 | | - * algorithm |
| 38 | + * This factory generates the DEFAULT_DIFF algorithm for all these routines. |
| 39 | + */ |
| 40 | + static DiffAlgorithmFactory DEFAULT_DIFF = MeyersDiff.factory(); |
| 41 | + |
| 42 | + public static void withDefaultDiffAlgorithmFactory(DiffAlgorithmFactory factory) { |
| 43 | + DEFAULT_DIFF = factory; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Computes the difference between the original and revised list of elements |
| 48 | + * with default diff algorithm |
39 | 49 | * |
40 | 50 | * @param <T> types to be diffed |
41 | 51 | * @param original The original text. Must not be {@code null}. |
42 | 52 | * @param revised The revised text. Must not be {@code null}. |
43 | 53 | * @param progress progress listener |
44 | | - * @return The patch describing the difference between the original and revised sequences. Never |
45 | | - * {@code null}. |
| 54 | + * @return The patch describing the difference between the original and |
| 55 | + * revised sequences. Never {@code null}. |
46 | 56 | */ |
47 | 57 | public static <T> Patch<T> diff(List<T> original, List<T> revised, DiffAlgorithmListener progress) { |
48 | | - return DiffUtils.diff(original, revised, new MyersDiff<>(), progress); |
| 58 | + return DiffUtils.diff(original, revised, DEFAULT_DIFF.create(), progress); |
49 | 59 | } |
50 | 60 |
|
51 | 61 | public static <T> Patch<T> diff(List<T> original, List<T> revised) { |
52 | | - return DiffUtils.diff(original, revised, new MyersDiff<>(), null); |
| 62 | + return DiffUtils.diff(original, revised, DEFAULT_DIFF.create(), null); |
53 | 63 | } |
54 | | - |
| 64 | + |
55 | 65 | public static <T> Patch<T> diff(List<T> original, List<T> revised, boolean includeEqualParts) { |
56 | | - return DiffUtils.diff(original, revised, new MyersDiff<>(), null, includeEqualParts); |
| 66 | + return DiffUtils.diff(original, revised, DEFAULT_DIFF.create(), null, includeEqualParts); |
57 | 67 | } |
58 | 68 |
|
59 | 69 | /** |
@@ -67,45 +77,46 @@ public static Patch<String> diff(String sourceText, String targetText, |
67 | 77 | } |
68 | 78 |
|
69 | 79 | /** |
70 | | - * Computes the difference between the original and revised list of elements with default diff |
71 | | - * algorithm |
| 80 | + * Computes the difference between the original and revised list of elements |
| 81 | + * with default diff algorithm |
72 | 82 | * |
73 | 83 | * @param source The original text. Must not be {@code null}. |
74 | 84 | * @param target The revised text. Must not be {@code null}. |
75 | 85 | * |
76 | | - * @param equalizer the equalizer object to replace the default compare algorithm |
77 | | - * (Object.equals). If {@code null} the default equalizer of the default algorithm is used.. |
78 | | - * @return The patch describing the difference between the original and revised sequences. Never |
79 | | - * {@code null}. |
| 86 | + * @param equalizer the equalizer object to replace the default compare |
| 87 | + * algorithm (Object.equals). If {@code null} the default equalizer of the |
| 88 | + * default algorithm is used.. |
| 89 | + * @return The patch describing the difference between the original and |
| 90 | + * revised sequences. Never {@code null}. |
80 | 91 | */ |
81 | 92 | public static <T> Patch<T> diff(List<T> source, List<T> target, |
82 | 93 | BiPredicate<T, T> equalizer) { |
83 | 94 | if (equalizer != null) { |
84 | 95 | return DiffUtils.diff(source, target, |
85 | | - new MyersDiff<>(equalizer)); |
| 96 | + DEFAULT_DIFF.create(equalizer)); |
86 | 97 | } |
87 | | - return DiffUtils.diff(source, target, new MyersDiff<>()); |
| 98 | + return DiffUtils.diff(source, target, new MeyersDiff<>()); |
88 | 99 | } |
89 | 100 |
|
90 | 101 | public static <T> Patch<T> diff(List<T> original, List<T> revised, |
91 | 102 | DiffAlgorithmI<T> algorithm, DiffAlgorithmListener progress) { |
92 | 103 | return diff(original, revised, algorithm, progress, false); |
93 | 104 | } |
94 | | - |
| 105 | + |
95 | 106 | /** |
96 | | - * Computes the difference between the original and revised list of elements with default diff |
97 | | - * algorithm |
| 107 | + * Computes the difference between the original and revised list of elements |
| 108 | + * with default diff algorithm |
98 | 109 | * |
99 | 110 | * @param original The original text. Must not be {@code null}. |
100 | 111 | * @param revised The revised text. Must not be {@code null}. |
101 | 112 | * @param algorithm The diff algorithm. Must not be {@code null}. |
102 | 113 | * @param progress The diff algorithm listener. |
103 | 114 | * @param includeEqualParts Include equal data parts into the patch. |
104 | | - * @return The patch describing the difference between the original and revised sequences. Never |
105 | | - * {@code null}. |
| 115 | + * @return The patch describing the difference between the original and |
| 116 | + * revised sequences. Never {@code null}. |
106 | 117 | */ |
107 | 118 | public static <T> Patch<T> diff(List<T> original, List<T> revised, |
108 | | - DiffAlgorithmI<T> algorithm, DiffAlgorithmListener progress, |
| 119 | + DiffAlgorithmI<T> algorithm, DiffAlgorithmListener progress, |
109 | 120 | boolean includeEqualParts) { |
110 | 121 | Objects.requireNonNull(original, "original must not be null"); |
111 | 122 | Objects.requireNonNull(revised, "revised must not be null"); |
@@ -115,23 +126,23 @@ public static <T> Patch<T> diff(List<T> original, List<T> revised, |
115 | 126 | } |
116 | 127 |
|
117 | 128 | /** |
118 | | - * Computes the difference between the original and revised list of elements with default diff |
119 | | - * algorithm |
| 129 | + * Computes the difference between the original and revised list of elements |
| 130 | + * with default diff algorithm |
120 | 131 | * |
121 | 132 | * @param original The original text. Must not be {@code null}. |
122 | 133 | * @param revised The revised text. Must not be {@code null}. |
123 | 134 | * @param algorithm The diff algorithm. Must not be {@code null}. |
124 | | - * @return The patch describing the difference between the original and revised sequences. Never |
125 | | - * {@code null}. |
| 135 | + * @return The patch describing the difference between the original and |
| 136 | + * revised sequences. Never {@code null}. |
126 | 137 | */ |
127 | 138 | public static <T> Patch<T> diff(List<T> original, List<T> revised, DiffAlgorithmI<T> algorithm) { |
128 | 139 | return diff(original, revised, algorithm, null); |
129 | 140 | } |
130 | 141 |
|
131 | 142 | /** |
132 | | - * Computes the difference between the given texts inline. This one uses the "trick" to make out |
133 | | - * of texts lists of characters, like DiffRowGenerator does and merges those changes at the end |
134 | | - * together again. |
| 143 | + * Computes the difference between the given texts inline. This one uses the |
| 144 | + * "trick" to make out of texts lists of characters, like DiffRowGenerator |
| 145 | + * does and merges those changes at the end together again. |
135 | 146 | * |
136 | 147 | * @param original |
137 | 148 | * @param revised |
|