Top Description Fields Constructors Methods
com.github.difflib

public final Class DiffUtils

extends Object
Class Inheritance
Imports
com.github.difflib.algorithm.DiffAlgorithmFactory, .DiffAlgorithmI, .DiffAlgorithmListener, com.github.difflib.algorithm.myers.MyersDiff, com.github.difflib.patch.AbstractDelta, .Patch, .PatchFailedException, java.util.ArrayList, .Arrays, .Collections, .List, .Objects, java.util.function.BiPredicate

Utility class to implement the difference and patching engine.

Field Summary

Modifier and TypeField and Description
pack-priv static DiffAlgorithmFactory
DEFAULT_DIFF

This factory generates the DEFAULT_DIFF algorithm for all these routines.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static List<String>
compressLines(List<String> lines, String delimiter)

public static <
a generic representing the type of the elements to be compared.
T
>
Patch<T>

Returns:

The patch describing the difference between the original and revised sequences. Never null.
diff
(List<T>
a List represnting the original sequence of elements. Must not be null.
original
,
List<T>
a List represnting the revised sequence of elements. Must not be null.
revised
,
DiffAlgorithmListener
a DiffAlgorithmListener represnting the progress listener. Can be null.
progress
)

Computes the difference between two sequences of elements using the default diff algorithm.

public static <
a generic representing the type of the elements to be compared.
T
>
Patch<T>

Returns:

The patch describing the difference between the original and revised sequences. Never null.
diff
(List<T>
a List represnting the original sequence of elements. Must not be null.
original
,
List<T>
a List represnting the revised sequence of elements. Must not be null.
revised
)

Computes the difference between two sequences of elements using the default diff algorithm.

public static <
a generic representing the type of the elements to be compared.
T
>
Patch<T>

Returns:

The patch describing the difference between the original and revised sequences. Never null.
diff
(List<T>
a List represnting the original sequence of elements. Must not be null.
original
,
List<T>
a List represnting the revised sequence of elements. Must not be null.
revised
,
boolean
a boolean represnting whether to include equal parts in the resulting patch.
includeEqualParts
)

Computes the difference between two sequences of elements using the default diff algorithm.

public static Patch<String>

Returns:

The patch describing the difference between the original and revised strings. Never null.
diff
(String
a String represnting the original string. Must not be null.
sourceText
,
String
a String represnting the revised string. Must not be null.
targetText
,
DiffAlgorithmListener
a DiffAlgorithmListener represnting the progress listener. Can be null.
progress
)

Computes the difference between two strings using the default diff algorithm.

public static <T> Patch<T>

Returns:

The patch describing the difference between the original and revised sequences. Never null.
diff
(List<T>
a List represnting the original text. Must not be null.
source
,
List<T>
a List represnting the revised text. Must not be null.
target
,
BiPredicate<T, T>
a BiPredicate represnting the equalizer object to replace the default compare algorithm (Object.equals). If null the default equalizer of the default algorithm is used.
equalizer
)

Computes the difference between the original and revised list of elements with default diff algorithm

public static <T> Patch<T>
diff(List<T> original, List<T> revised, DiffAlgorithmI<T> algorithm, DiffAlgorithmListener progress)

public static <T> Patch<T>

Returns:

The patch describing the difference between the original and revised sequences. Never null.
diff
(List<T>
a List represnting the original text. Must not be null.
original
,
List<T>
a List represnting the revised text. Must not be null.
revised
,
DiffAlgorithmI<T>
a DiffAlgorithmI represnting the diff algorithm. Must not be null.
algorithm
,
DiffAlgorithmListener
a DiffAlgorithmListener represnting the diff algorithm listener.
progress
,
boolean
Include equal data parts into the patch.
includeEqualParts
)

Computes the difference between the original and revised list of elements with default diff algorithm

public static <T> Patch<T>

Returns:

The patch describing the difference between the original and revised sequences. Never null.
diff
(List<T>
a List represnting the original text. Must not be null.
original
,
List<T>
a List represnting the revised text. Must not be null.
revised
,
DiffAlgorithmI<T>
a DiffAlgorithmI represnting the diff algorithm. Must not be null.
algorithm
)

Computes the difference between the original and revised list of elements with default diff algorithm

public static Patch<String>

Returns:

The patch describing the difference between the original and revised sequences. Never null.
diffInline
(String
a String represnting the original text. Must not be null.
original
,
String
a String represnting the revised text. Must not be null.
revised
)

Computes the difference between the given texts inline.

public static <T> List<T>

Returns:

the revised list.
patch
(List<T>
a List represnting the original list.
original
,
Patch<T>
a List represnting the patch to apply.
patch
)

Applies the given patch to the original list and returns the revised list.

public static <T> List<T>

Returns:

the original list.
unpatch
(List<T>
a List represnting the revised list.
revised
,
Patch<T>
a Patch represnting the patch to apply.
patch
)

Applies the given patch to the revised list and returns the original list.

public static void
withDefaultDiffAlgorithmFactory(DiffAlgorithmFactory
a DiffAlgorithmFactory represnting the new default diff algorithm factory.
factory
)

Sets the default diff algorithm factory to be used by all diff routines.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DEFAULT_DIFFback to summary
pack-priv static DiffAlgorithmFactory DEFAULT_DIFF

This factory generates the DEFAULT_DIFF algorithm for all these routines.

Constructor Detail

DiffUtilsback to summary
private DiffUtils()

Method Detail

compressLinesback to summary
private static List<String> compressLines(List<String> lines, String delimiter)
diffback to summary
public static <T> Patch<T> diff(List<T> original, List<T> revised, DiffAlgorithmListener progress)

Computes the difference between two sequences of elements using the default diff algorithm.

Parameters
<T>
a generic representing the type of the elements to be compared.
original:List<T>

a List represnting the original sequence of elements. Must not be null.

revised:List<T>

a List represnting the revised sequence of elements. Must not be null.

progress:DiffAlgorithmListener

a DiffAlgorithmListener represnting the progress listener. Can be null.

Returns:Patch<T>

The patch describing the difference between the original and revised sequences. Never null.

diffback to summary
public static <T> Patch<T> diff(List<T> original, List<T> revised)

Computes the difference between two sequences of elements using the default diff algorithm.

Parameters
<T>
a generic representing the type of the elements to be compared.
original:List<T>

a List represnting the original sequence of elements. Must not be null.

revised:List<T>

a List represnting the revised sequence of elements. Must not be null.

Returns:Patch<T>

The patch describing the difference between the original and revised sequences. Never null.

diffback to summary
public static <T> Patch<T> diff(List<T> original, List<T> revised, boolean includeEqualParts)

Computes the difference between two sequences of elements using the default diff algorithm.

Parameters
<T>
a generic representing the type of the elements to be compared.
original:List<T>

a List represnting the original sequence of elements. Must not be null.

revised:List<T>

a List represnting the revised sequence of elements. Must not be null.

includeEqualParts:boolean

a boolean represnting whether to include equal parts in the resulting patch.

Returns:Patch<T>

The patch describing the difference between the original and revised sequences. Never null.

diffback to summary
public static Patch<String> diff(String sourceText, String targetText, DiffAlgorithmListener progress)

Computes the difference between two strings using the default diff algorithm.

Parameters
sourceText:String

a String represnting the original string. Must not be null.

targetText:String

a String represnting the revised string. Must not be null.

progress:DiffAlgorithmListener

a DiffAlgorithmListener represnting the progress listener. Can be null.

Returns:Patch<String>

The patch describing the difference between the original and revised strings. Never null.

diffback to summary
public static <T> Patch<T> diff(List<T> source, List<T> target, BiPredicate<T, T> equalizer)

Computes the difference between the original and revised list of elements with default diff algorithm

Parameters
source:List<T>

a List represnting the original text. Must not be null.

target:List<T>

a List represnting the revised text. Must not be null.

equalizer:BiPredicate<T, T>

a BiPredicate represnting the equalizer object to replace the default compare algorithm (Object.equals). If null the default equalizer of the default algorithm is used.

Returns:Patch<T>

The patch describing the difference between the original and revised sequences. Never null.

diffback to summary
public static <T> Patch<T> diff(List<T> original, List<T> revised, DiffAlgorithmI<T> algorithm, DiffAlgorithmListener progress)
diffback to summary
public static <T> Patch<T> diff(List<T> original, List<T> revised, DiffAlgorithmI<T> algorithm, DiffAlgorithmListener progress, boolean includeEqualParts)

Computes the difference between the original and revised list of elements with default diff algorithm

Parameters
original:List<T>

a List represnting the original text. Must not be null.

revised:List<T>

a List represnting the revised text. Must not be null.

algorithm:DiffAlgorithmI<T>

a DiffAlgorithmI represnting the diff algorithm. Must not be null.

progress:DiffAlgorithmListener

a DiffAlgorithmListener represnting the diff algorithm listener.

includeEqualParts:boolean

Include equal data parts into the patch.

Returns:Patch<T>

The patch describing the difference between the original and revised sequences. Never null.

diffback to summary
public static <T> Patch<T> diff(List<T> original, List<T> revised, DiffAlgorithmI<T> algorithm)

Computes the difference between the original and revised list of elements with default diff algorithm

Parameters
original:List<T>

a List represnting the original text. Must not be null.

revised:List<T>

a List represnting the revised text. Must not be null.

algorithm:DiffAlgorithmI<T>

a DiffAlgorithmI represnting the diff algorithm. Must not be null.

Returns:Patch<T>

The patch describing the difference between the original and revised sequences. Never null.

diffInlineback to summary
public static Patch<String> diffInline(String original, String revised)

Computes the difference between the given texts inline. This one uses the "trick" to make out of texts lists of characters, like DiffRowGenerator does and merges those changes at the end together again.

Parameters
original:String

a String represnting the original text. Must not be null.

revised:String

a String represnting the revised text. Must not be null.

Returns:Patch<String>

The patch describing the difference between the original and revised sequences. Never null.

patchback to summary
public static <T> List<T> patch(List<T> original, Patch<T> patch) throws PatchFailedException

Applies the given patch to the original list and returns the revised list.

Parameters
original:List<T>

a List represnting the original list.

patch:Patch<T>

a List represnting the patch to apply.

Returns:List<T>

the revised list.

Exceptions
PatchFailedException:
if the patch cannot be applied.
unpatchback to summary
public static <T> List<T> unpatch(List<T> revised, Patch<T> patch)

Applies the given patch to the revised list and returns the original list.

Parameters
revised:List<T>

a List represnting the revised list.

patch:Patch<T>

a Patch represnting the patch to apply.

Returns:List<T>

the original list.

Exceptions
PatchFailedException:
if the patch cannot be applied.
withDefaultDiffAlgorithmFactoryback to summary
public static void withDefaultDiffAlgorithmFactory(DiffAlgorithmFactory factory)

Sets the default diff algorithm factory to be used by all diff routines.

Parameters
factory:DiffAlgorithmFactory

a DiffAlgorithmFactory represnting the new default diff algorithm factory.