30
30
import java .util .function .BiPredicate ;
31
31
32
32
/**
33
- * Implements the difference and patching engine
33
+ * Utility class to implement the difference and patching engine.
34
34
*/
35
35
public final class DiffUtils {
36
36
@@ -39,38 +39,63 @@ public final class DiffUtils {
39
39
*/
40
40
static DiffAlgorithmFactory DEFAULT_DIFF = MyersDiff .factory ();
41
41
42
+ /**
43
+ * Sets the default diff algorithm factory to be used by all diff routines.
44
+ *
45
+ * @param factory a {@link DiffAlgorithmFactory} represnting the new default diff algorithm factory.
46
+ */
42
47
public static void withDefaultDiffAlgorithmFactory (DiffAlgorithmFactory factory ) {
43
48
DEFAULT_DIFF = factory ;
44
49
}
45
50
46
51
/**
47
- * Computes the difference between the original and revised list of elements
48
- * with default diff algorithm
52
+ * Computes the difference between two sequences of elements using the default diff algorithm.
49
53
*
50
- * @param <T> types to be diffed
51
- * @param original The original text. Must not be {@code null}.
52
- * @param revised The revised text. Must not be {@code null}.
53
- * @param progress progress listener
54
- * @return The patch describing the difference between the original and
55
- * revised sequences. Never {@code null}.
54
+ * @param <T> a generic representing the type of the elements to be compared.
55
+ * @param original a {@link List} represnting the original sequence of elements. Must not be {@code null}.
56
+ * @param revised a {@link List} represnting the revised sequence of elements. Must not be {@code null}.
57
+ * @param progress a {@link DiffAlgorithmListener} represnting the progress listener. Can be {@code null}.
58
+ * @return The patch describing the difference between the original and revised sequences. Never {@code null}.
56
59
*/
57
60
public static <T > Patch <T > diff (List <T > original , List <T > revised , DiffAlgorithmListener progress ) {
58
61
return DiffUtils .diff (original , revised , DEFAULT_DIFF .create (), progress );
59
62
}
60
63
64
+ /**
65
+ * Computes the difference between two sequences of elements using the default diff algorithm.
66
+ *
67
+ * @param <T> a generic representing the type of the elements to be compared.
68
+ * @param original a {@link List} represnting the original sequence of elements. Must not be {@code null}.
69
+ * @param revised a {@link List} represnting the revised sequence of elements. Must not be {@code null}.
70
+ * @return The patch describing the difference between the original and revised sequences. Never {@code null}.
71
+ */
61
72
public static <T > Patch <T > diff (List <T > original , List <T > revised ) {
62
73
return DiffUtils .diff (original , revised , DEFAULT_DIFF .create (), null );
63
74
}
64
75
76
+ /**
77
+ * Computes the difference between two sequences of elements using the default diff algorithm.
78
+ *
79
+ * @param <T> a generic representing the type of the elements to be compared.
80
+ * @param original a {@link List} represnting the original sequence of elements. Must not be {@code null}.
81
+ * @param revised a {@link List} represnting the revised sequence of elements. Must not be {@code null}.
82
+ * @param includeEqualParts a {@link boolean} represnting whether to include equal parts in the resulting patch.
83
+ * @return The patch describing the difference between the original and revised sequences. Never {@code null}.
84
+ */
65
85
public static <T > Patch <T > diff (List <T > original , List <T > revised , boolean includeEqualParts ) {
66
86
return DiffUtils .diff (original , revised , DEFAULT_DIFF .create (), null , includeEqualParts );
67
87
}
68
88
69
89
/**
70
- * Computes the difference between the original and revised text.
90
+ * Computes the difference between two strings using the default diff algorithm.
91
+ *
92
+ * @param sourceText a {@link String} represnting the original string. Must not be {@code null}.
93
+ * @param targetText a {@link String} represnting the revised string. Must not be {@code null}.
94
+ * @param progress a {@link DiffAlgorithmListener} represnting the progress listener. Can be {@code null}.
95
+ * @return The patch describing the difference between the original and revised strings. Never {@code null}.
71
96
*/
72
97
public static Patch <String > diff (String sourceText , String targetText ,
73
- DiffAlgorithmListener progress ) {
98
+ DiffAlgorithmListener progress ) {
74
99
return DiffUtils .diff (
75
100
Arrays .asList (sourceText .split ("\n " )),
76
101
Arrays .asList (targetText .split ("\n " )), progress );
@@ -80,17 +105,16 @@ public static Patch<String> diff(String sourceText, String targetText,
80
105
* Computes the difference between the original and revised list of elements
81
106
* with default diff algorithm
82
107
*
83
- * @param source The original text. Must not be {@code null}.
84
- * @param target The revised text. Must not be {@code null}.
85
- *
86
- * @param equalizer the equalizer object to replace the default compare
108
+ * @param source a {@link List} represnting the original text. Must not be {@code null}.
109
+ * @param target a {@link List} represnting the revised text. Must not be {@code null}.
110
+ * @param equalizer a {@link BiPredicate} represnting the equalizer object to replace the default compare
87
111
* algorithm (Object.equals). If {@code null} the default equalizer of the
88
- * default algorithm is used..
112
+ * default algorithm is used.
89
113
* @return The patch describing the difference between the original and
90
114
* revised sequences. Never {@code null}.
91
115
*/
92
116
public static <T > Patch <T > diff (List <T > source , List <T > target ,
93
- BiPredicate <T , T > equalizer ) {
117
+ BiPredicate <T , T > equalizer ) {
94
118
if (equalizer != null ) {
95
119
return DiffUtils .diff (source , target ,
96
120
DEFAULT_DIFF .create (equalizer ));
@@ -99,39 +123,40 @@ public static <T> Patch<T> diff(List<T> source, List<T> target,
99
123
}
100
124
101
125
public static <T > Patch <T > diff (List <T > original , List <T > revised ,
102
- DiffAlgorithmI <T > algorithm , DiffAlgorithmListener progress ) {
126
+ DiffAlgorithmI <T > algorithm , DiffAlgorithmListener progress ) {
103
127
return diff (original , revised , algorithm , progress , false );
104
128
}
105
129
106
130
/**
107
131
* Computes the difference between the original and revised list of elements
108
132
* with default diff algorithm
109
133
*
110
- * @param original The original text. Must not be {@code null}.
111
- * @param revised The revised text. Must not be {@code null}.
112
- * @param algorithm The diff algorithm. Must not be {@code null}.
113
- * @param progress The diff algorithm listener.
134
+ * @param original a {@link List} represnting the original text. Must not be {@code null}.
135
+ * @param revised a {@link List} represnting the revised text. Must not be {@code null}.
136
+ * @param algorithm a {@link DiffAlgorithmI} represnting the diff algorithm. Must not be {@code null}.
137
+ * @param progress a {@link DiffAlgorithmListener} represnting the diff algorithm listener.
114
138
* @param includeEqualParts Include equal data parts into the patch.
115
139
* @return The patch describing the difference between the original and
116
140
* revised sequences. Never {@code null}.
117
141
*/
118
142
public static <T > Patch <T > diff (List <T > original , List <T > revised ,
119
- DiffAlgorithmI <T > algorithm , DiffAlgorithmListener progress ,
120
- boolean includeEqualParts ) {
143
+ DiffAlgorithmI <T > algorithm , DiffAlgorithmListener progress ,
144
+ boolean includeEqualParts ) {
121
145
Objects .requireNonNull (original , "original must not be null" );
122
146
Objects .requireNonNull (revised , "revised must not be null" );
123
147
Objects .requireNonNull (algorithm , "algorithm must not be null" );
124
148
125
149
return Patch .generate (original , revised , algorithm .computeDiff (original , revised , progress ), includeEqualParts );
126
150
}
127
151
152
+
128
153
/**
129
154
* Computes the difference between the original and revised list of elements
130
155
* with default diff algorithm
131
156
*
132
- * @param original The original text. Must not be {@code null}.
133
- * @param revised The revised text. Must not be {@code null}.
134
- * @param algorithm The diff algorithm. Must not be {@code null}.
157
+ * @param original a {@link List} represnting the original text. Must not be {@code null}.
158
+ * @param revised a {@link List} represnting the revised text. Must not be {@code null}.
159
+ * @param algorithm a {@link DiffAlgorithmI} represnting the diff algorithm. Must not be {@code null}.
135
160
* @return The patch describing the difference between the original and
136
161
* revised sequences. Never {@code null}.
137
162
*/
@@ -144,9 +169,10 @@ public static <T> Patch<T> diff(List<T> original, List<T> revised, DiffAlgorithm
144
169
* "trick" to make out of texts lists of characters, like DiffRowGenerator
145
170
* does and merges those changes at the end together again.
146
171
*
147
- * @param original
148
- * @param revised
149
- * @return
172
+ * @param original a {@link String} represnting the original text. Must not be {@code null}.
173
+ * @param revised a {@link String} represnting the revised text. Must not be {@code null}.
174
+ * @return The patch describing the difference between the original and
175
+ * revised sequences. Never {@code null}.
150
176
*/
151
177
public static Patch <String > diffInline (String original , String revised ) {
152
178
List <String > origList = new ArrayList <>();
@@ -165,37 +191,38 @@ public static Patch<String> diffInline(String original, String revised) {
165
191
return patch ;
166
192
}
167
193
168
- private static List <String > compressLines (List <String > lines , String delimiter ) {
169
- if (lines .isEmpty ()) {
170
- return Collections .emptyList ();
171
- }
172
- return Collections .singletonList (String .join (delimiter , lines ));
173
- }
174
-
175
194
/**
176
- * Patch the original text with given patch
195
+ * Applies the given patch to the original list and returns the revised list.
177
196
*
178
- * @param original the original text
179
- * @param patch the given patch
180
- * @return the revised text
181
- * @throws PatchFailedException if can't apply patch
197
+ * @param original a {@link List} represnting the original list.
198
+ * @param patch a {@link List} represnting the patch to apply.
199
+ * @return the revised list.
200
+ * @throws PatchFailedException if the patch cannot be applied.
182
201
*/
183
202
public static <T > List <T > patch (List <T > original , Patch <T > patch )
184
203
throws PatchFailedException {
185
204
return patch .applyTo (original );
186
205
}
187
206
188
207
/**
189
- * Unpatch the revised text for a given patch
208
+ * Applies the given patch to the revised list and returns the original list.
190
209
*
191
- * @param revised the revised text
192
- * @param patch the given patch
193
- * @return the original text
210
+ * @param revised a {@link List} represnting the revised list.
211
+ * @param patch a {@link Patch} represnting the patch to apply.
212
+ * @return the original list.
213
+ * @throws PatchFailedException if the patch cannot be applied.
194
214
*/
195
215
public static <T > List <T > unpatch (List <T > revised , Patch <T > patch ) {
196
216
return patch .restore (revised );
197
217
}
198
218
219
+ private static List <String > compressLines (List <String > lines , String delimiter ) {
220
+ if (lines .isEmpty ()) {
221
+ return Collections .emptyList ();
222
+ }
223
+ return Collections .singletonList (String .join (delimiter , lines ));
224
+ }
225
+
199
226
private DiffUtils () {
200
227
}
201
228
}
0 commit comments