Class DiffUtils


  • public class DiffUtils
    extends java.lang.Object
    Copy from https://code.google.com/p/java-diff-utils/.

    Implements the difference and patching engine

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.util.regex.Pattern unifiedDiffChunkRe  
    • Constructor Summary

      Constructors 
      Constructor Description
      DiffUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> Patch<T> diff​(java.util.List<T> original, java.util.List<T> revised)
      Computes the difference between the original and revised list of elements with default diff algorithm
      static <T> Patch<T> diff​(java.util.List<T> original, java.util.List<T> revised, DiffAlgorithm<T> algorithm)
      Computes the difference between the original and revised list of elements with default diff algorithm
      static java.util.List<java.lang.String> generateUnifiedDiff​(java.lang.String original, java.lang.String revised, java.util.List<java.lang.String> originalLines, Patch<java.lang.String> patch, int contextSize)
      generateUnifiedDiff takes a Patch and some other arguments, returning the Unified Diff format text representing the Patch.
      private static java.util.List<java.lang.String> getDeltaText​(Delta<java.lang.String> delta)
      getDeltaText returns the lines to be added to the Unified Diff text from the Delta parameter
      static Patch<java.lang.String> parseUnifiedDiff​(java.util.List<java.lang.String> diff)
      Parse the given text in unified format and creates the list of deltas for it.
      static <T> java.util.List<T> patch​(java.util.List<T> original, Patch<T> patch)
      Patch the original text with given patch
      private static java.util.List<java.lang.String> processDeltas​(java.util.List<java.lang.String> origLines, java.util.List<Delta<java.lang.String>> deltas, int contextSize)
      processDeltas takes a list of Deltas and outputs them together in a single block of Unified-Diff-format text.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • unifiedDiffChunkRe

        private static java.util.regex.Pattern unifiedDiffChunkRe
    • Constructor Detail

      • DiffUtils

        public DiffUtils()
    • Method Detail

      • diff

        public static <T> Patch<T> diff​(java.util.List<T> original,
                                        java.util.List<T> revised)
        Computes the difference between the original and revised list of elements with default diff algorithm
        Parameters:
        original - The original text. Must not be null.
        revised - The revised text. 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​(java.util.List<T> original,
                                        java.util.List<T> revised,
                                        DiffAlgorithm<T> algorithm)
        Computes the difference between the original and revised list of elements with default diff algorithm
        Parameters:
        original - The original text. Must not be null.
        revised - The revised text. Must not be null.
        algorithm - The diff algorithm. Must not be null.
        Returns:
        The patch describing the difference between the original and revised sequences. Never null.
      • patch

        public static <T> java.util.List<T> patch​(java.util.List<T> original,
                                                  Patch<T> patch)
                                           throws java.lang.IllegalStateException
        Patch the original text with given patch
        Parameters:
        original - the original text
        patch - the given patch
        Returns:
        the revised text
        Throws:
        java.lang.IllegalStateException - if can't apply patch
      • parseUnifiedDiff

        public static Patch<java.lang.String> parseUnifiedDiff​(java.util.List<java.lang.String> diff)
        Parse the given text in unified format and creates the list of deltas for it.
        Parameters:
        diff - the text in unified format
        Returns:
        the patch with deltas.
      • generateUnifiedDiff

        public static java.util.List<java.lang.String> generateUnifiedDiff​(java.lang.String original,
                                                                           java.lang.String revised,
                                                                           java.util.List<java.lang.String> originalLines,
                                                                           Patch<java.lang.String> patch,
                                                                           int contextSize)
        generateUnifiedDiff takes a Patch and some other arguments, returning the Unified Diff format text representing the Patch.
        Parameters:
        original - Filename of the original (unrevised file)
        revised - Filename of the revised file
        originalLines - Lines of the original file
        patch - Patch created by the diff() function
        contextSize - number of lines of context output around each difference in the file.
        Returns:
        List of strings representing the Unified Diff representation of the Patch argument.
      • processDeltas

        private static java.util.List<java.lang.String> processDeltas​(java.util.List<java.lang.String> origLines,
                                                                      java.util.List<Delta<java.lang.String>> deltas,
                                                                      int contextSize)
        processDeltas takes a list of Deltas and outputs them together in a single block of Unified-Diff-format text.
        Parameters:
        origLines - the lines of the original file
        deltas - the Deltas to be output as a single block
        contextSize - the number of lines of context to place around block
      • getDeltaText

        private static java.util.List<java.lang.String> getDeltaText​(Delta<java.lang.String> delta)
        getDeltaText returns the lines to be added to the Unified Diff text from the Delta parameter
        Parameters:
        delta - the Delta to output
        Returns:
        list of String lines of code.