3030import java .util .function .BiPredicate ;
3131
3232/**
33- *Implements the difference and patching engine
33+ *Utility class to implement the difference and patching engine.
3434 */
3535public final class DiffUtils {
3636
@@ -39,38 +39,63 @@ public final class DiffUtils {
3939 */
4040static DiffAlgorithmFactory DEFAULT_DIFF =MyersDiff .factory ();
4141
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+ */
4247public static void withDefaultDiffAlgorithmFactory (DiffAlgorithmFactory factory ) {
4348DEFAULT_DIFF =factory ;
4449 }
4550
4651/**
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.
4953 *
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}.
5659 */
5760public static <T >Patch <T >diff (List <T >original ,List <T >revised ,DiffAlgorithmListener progress ) {
5861return DiffUtils .diff (original ,revised ,DEFAULT_DIFF .create (),progress );
5962 }
6063
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+ */
6172public static <T >Patch <T >diff (List <T >original ,List <T >revised ) {
6273return DiffUtils .diff (original ,revised ,DEFAULT_DIFF .create (),null );
6374 }
6475
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+ */
6585public static <T >Patch <T >diff (List <T >original ,List <T >revised ,boolean includeEqualParts ) {
6686return DiffUtils .diff (original ,revised ,DEFAULT_DIFF .create (),null ,includeEqualParts );
6787 }
6888
6989/**
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}.
7196 */
7297public static Patch <String >diff (String sourceText ,String targetText ,
73- DiffAlgorithmListener progress ) {
98+ DiffAlgorithmListener progress ) {
7499return DiffUtils .diff (
75100Arrays .asList (sourceText .split ("\n " )),
76101Arrays .asList (targetText .split ("\n " )),progress );
@@ -80,17 +105,16 @@ public static Patch<String> diff(String sourceText, String targetText,
80105 * Computes the difference between the original and revised list of elements
81106 * with default diff algorithm
82107 *
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
87111 * algorithm (Object.equals). If {@code null} the default equalizer of the
88- * default algorithm is used..
112+ * default algorithm is used.
89113 * @return The patch describing the difference between the original and
90114 * revised sequences. Never {@code null}.
91115 */
92116public static <T >Patch <T >diff (List <T >source ,List <T >target ,
93- BiPredicate <T ,T >equalizer ) {
117+ BiPredicate <T ,T >equalizer ) {
94118if (equalizer !=null ) {
95119return DiffUtils .diff (source ,target ,
96120DEFAULT_DIFF .create (equalizer ));
@@ -99,39 +123,40 @@ public static <T> Patch<T> diff(List<T> source, List<T> target,
99123 }
100124
101125public static <T >Patch <T >diff (List <T >original ,List <T >revised ,
102- DiffAlgorithmI <T >algorithm ,DiffAlgorithmListener progress ) {
126+ DiffAlgorithmI <T >algorithm ,DiffAlgorithmListener progress ) {
103127return diff (original ,revised ,algorithm ,progress ,false );
104128 }
105129
106130/**
107131 * Computes the difference between the original and revised list of elements
108132 * with default diff algorithm
109133 *
110- * @param originalThe original text. Must not be {@code null}.
111- * @param revisedThe revised text. Must not be {@code null}.
112- * @param algorithmThe diff algorithm. Must not be {@code null}.
113- * @param progressThe diff algorithm listener.
134+ * @param originala {@link List} represnting the original text. Must not be {@code null}.
135+ * @param reviseda {@link List} represnting the revised text. Must not be {@code null}.
136+ * @param algorithma {@link DiffAlgorithmI} represnting the diff algorithm. Must not be {@code null}.
137+ * @param progressa {@link DiffAlgorithmListener} represnting the diff algorithm listener.
114138 * @param includeEqualParts Include equal data parts into the patch.
115139 * @return The patch describing the difference between the original and
116140 * revised sequences. Never {@code null}.
117141 */
118142public 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 ) {
121145Objects .requireNonNull (original ,"original must not be null" );
122146Objects .requireNonNull (revised ,"revised must not be null" );
123147Objects .requireNonNull (algorithm ,"algorithm must not be null" );
124148
125149return Patch .generate (original ,revised ,algorithm .computeDiff (original ,revised ,progress ),includeEqualParts );
126150 }
127151
152+
128153/**
129154 * Computes the difference between the original and revised list of elements
130155 * with default diff algorithm
131156 *
132- * @param originalThe original text. Must not be {@code null}.
133- * @param revisedThe revised text. Must not be {@code null}.
134- * @param algorithmThe diff algorithm. Must not be {@code null}.
157+ * @param originala {@link List} represnting the original text. Must not be {@code null}.
158+ * @param reviseda {@link List} represnting the revised text. Must not be {@code null}.
159+ * @param algorithma {@link DiffAlgorithmI} represnting the diff algorithm. Must not be {@code null}.
135160 * @return The patch describing the difference between the original and
136161 * revised sequences. Never {@code null}.
137162 */
@@ -144,9 +169,10 @@ public static <T> Patch<T> diff(List<T> original, List<T> revised, DiffAlgorithm
144169 * "trick" to make out of texts lists of characters, like DiffRowGenerator
145170 * does and merges those changes at the end together again.
146171 *
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}.
150176 */
151177public static Patch <String >diffInline (String original ,String revised ) {
152178List <String >origList =new ArrayList <>();
@@ -165,37 +191,38 @@ public static Patch<String> diffInline(String original, String revised) {
165191return patch ;
166192 }
167193
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-
175194/**
176- *Patch the originaltext with given patch
195+ *Applies thegiven patch to the originallist and returns the revised list.
177196 *
178- * @param original the originaltext
179- * @param patchthe given patch
180- * @return the revisedtext
181- * @throws PatchFailedException ifcan't apply patch
197+ * @param originala {@link List} represnting the originallist.
198+ * @param patcha {@link List} represnting the patch to apply.
199+ * @return the revisedlist.
200+ * @throws PatchFailedException ifthe patch cannot be applied.
182201 */
183202public static <T >List <T >patch (List <T >original ,Patch <T >patch )
184203throws PatchFailedException {
185204return patch .applyTo (original );
186205 }
187206
188207/**
189- *Unpatch the revisedtext for a given patch
208+ *Applies thegiven patch to the revisedlist and returns the original list.
190209 *
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.
194214 */
195215public static <T >List <T >unpatch (List <T >revised ,Patch <T >patch ) {
196216return patch .restore (revised );
197217 }
198218
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+
199226private DiffUtils () {
200227 }
201228}