Class DiffUtils

java.lang.Object
com.github.difflib.DiffUtils

public final class DiffUtils extends Object
Utility class to implement the difference and patching engine.
  • Field Details

    • DEFAULT_DIFF

      static DiffAlgorithmFactory DEFAULT_DIFF
      This factory generates the DEFAULT_DIFF algorithm for all these routines.
  • Constructor Details

    • DiffUtils

      private DiffUtils()
  • Method Details

    • withDefaultDiffAlgorithmFactory

      public static void withDefaultDiffAlgorithmFactory(DiffAlgorithmFactory factory)
      Sets the default diff algorithm factory to be used by all diff routines.
      Parameters:
      factory - a DiffAlgorithmFactory representing the new default diff algorithm factory.
    • diff

      public static <T> Patch<T> diff(List<? extends T> original, List<? extends T> revised, DiffAlgorithmListener progress)
      Computes the difference between two sequences of elements using the default diff algorithm.
      Type Parameters:
      T - a generic representing the type of the elements to be compared.
      Parameters:
      original - a List representing the original sequence of elements. Must not be null.
      revised - a List representing the revised sequence of elements. Must not be null.
      progress - a DiffAlgorithmListener representing the progress listener. Can be null.
      Returns:
      The patch describing the difference between the original and revised sequences. Never null.
    • diff

      public static <T> Patch<T> diff(List<? extends T> original, List<? extends T> revised)
      Computes the difference between two sequences of elements using the default diff algorithm.
      Type Parameters:
      T - a generic representing the type of the elements to be compared.
      Parameters:
      original - a List representing the original sequence of elements. Must not be null.
      revised - a List representing the revised sequence of elements. Must not be null.
      Returns:
      The patch describing the difference between the original and revised sequences. Never null.
    • diff

      public static <T> Patch<T> diff(List<? extends T> original, List<? extends T> revised, boolean includeEqualParts)
      Computes the difference between two sequences of elements using the default diff algorithm.
      Type Parameters:
      T - a generic representing the type of the elements to be compared.
      Parameters:
      original - a List representing the original sequence of elements. Must not be null.
      revised - a List representing the revised sequence of elements. Must not be null.
      includeEqualParts - a
      invalid reference
      boolean
      representing whether to include equal parts in the resulting patch.
      Returns:
      The patch describing the difference between the original and revised sequences. Never null.
    • diff

      public static Patch<String> diff(String sourceText, String targetText, DiffAlgorithmListener progress)
      Computes the difference between two strings using the default diff algorithm.
      Parameters:
      sourceText - a String representing the original string. Must not be null.
      targetText - a String representing the revised string. Must not be null.
      progress - a DiffAlgorithmListener representing the progress listener. Can be null.
      Returns:
      The patch describing the difference between the original and revised strings. Never null.
    • diff

      public static <T> Patch<T> diff(List<? extends T> source, List<? extends T> target, BiPredicate<? super T, ? super T> equalizer)
      Computes the difference between the original and revised list of elements with default diff algorithm
      Parameters:
      source - a List representing the original text. Must not be null.
      target - a List representing the revised text. Must not be null.
      equalizer - a BiPredicate representing the equalizer object to replace the default compare algorithm (Object.equals). If null the default equalizer of the default algorithm is used.
      Returns:
      The patch describing the difference between the original and revised sequences. Never null.
    • diff

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

      public static <T> Patch<T> diff(List<? extends T> original, List<? extends 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 - a List representing the original text. Must not be null.
      revised - a List representing the revised text. Must not be null.
      algorithm - a DiffAlgorithmI representing the diff algorithm. Must not be null.
      progress - a DiffAlgorithmListener representing the diff algorithm listener.
      includeEqualParts - Include equal data parts into the patch.
      Returns:
      The patch describing the difference between the original and revised sequences. Never null.
    • diff

      public static <T> Patch<T> diff(List<? extends T> original, List<? extends T> revised, DiffAlgorithmI<T> algorithm)
      Computes the difference between the original and revised list of elements with default diff algorithm
      Parameters:
      original - a List representing the original text. Must not be null.
      revised - a List representing the revised text. Must not be null.
      algorithm - a DiffAlgorithmI representing the diff algorithm. Must not be null.
      Returns:
      The patch describing the difference between the original and revised sequences. Never null.
    • diffInline

      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 - a String representing the original text. Must not be null.
      revised - a String representing the revised text. Must not be null.
      Returns:
      The patch describing the difference between the original and revised sequences. Never null.
    • patch

      public static <T> List<T> patch(List<? extends T> original, Patch<T> patch) throws PatchFailedException
      Applies the given patch to the original list and returns the revised list.
      Parameters:
      original - a List representing the original list.
      patch - a List representing the patch to apply.
      Returns:
      the revised list.
      Throws:
      PatchFailedException - if the patch cannot be applied.
    • unpatch

      public static <T> List<T> unpatch(List<? extends T> revised, Patch<T> patch)
      Applies the given patch to the revised list and returns the original list.
      Parameters:
      revised - a List representing the revised list.
      patch - a Patch representing the patch to apply.
      Returns:
      the original list.
      Throws:
      PatchFailedException - if the patch cannot be applied.
    • compressLines

      private static List<String> compressLines(List<String> lines, String delimiter)