Package adams.core

Class DiffUtils


  • public class DiffUtils
    extends Object
    A helper class for generating diffs between two files, lists of strings.
    Version:
    $Revision$
    Author:
    fracpete (fracpete at waikato dot ac dot nz)
    • Field Detail

      • INDICATOR_CHANGED

        public static final char INDICATOR_CHANGED
        the indicator for "changed".
        See Also:
        Constant Field Values
      • INDICATOR_ADDED

        public static final char INDICATOR_ADDED
        the indicator for "added".
        See Also:
        Constant Field Values
      • INDICATOR_DELETED

        public static final char INDICATOR_DELETED
        the indicator for "deleted".
        See Also:
        Constant Field Values
      • INDICATOR_SAME

        public static final char INDICATOR_SAME
        the indicator for "same".
        See Also:
        Constant Field Values
      • SEPARATOR_UNIFIED

        public static final String SEPARATOR_UNIFIED
        the separator for the unified output.
        See Also:
        Constant Field Values
      • SEPARATOR_SIDEBYSIDE

        public static final String SEPARATOR_SIDEBYSIDE
        the separator for the side-by-side output on the command-line.
        See Also:
        Constant Field Values
      • SIDEBYSIDE_SIZE

        public static final int SIDEBYSIDE_SIZE
        the number of array elements in side-by-side diff.
        See Also:
        Constant Field Values
      • SIDEBYSIDE_FIRST

        public static final int SIDEBYSIDE_FIRST
        the index for the first file in side-by-side diff.
        See Also:
        Constant Field Values
      • SIDEBYSIDE_SECOND

        public static final int SIDEBYSIDE_SECOND
        the index for the second file in side-by-side diff.
        See Also:
        Constant Field Values
      • SIDEBYSIDE_INDICATOR

        public static final int SIDEBYSIDE_INDICATOR
        the index for the indicator list in side-by-side diff.
        See Also:
        Constant Field Values
      • SIDEBYSIDE_STARTPOS

        public static final int SIDEBYSIDE_STARTPOS
        the index for the list of start positions of deltas in side-by-side diff.
        See Also:
        Constant Field Values
      • SIDEBYSIDE_ENDPOS

        public static final int SIDEBYSIDE_ENDPOS
        the index for the list of end positions of deltas in side-by-side diff.
        See Also:
        Constant Field Values
    • Constructor Detail

      • DiffUtils

        public DiffUtils()
    • Method Detail

      • loadFromFile

        protected static List<String> loadFromFile​(File file)
        Loads the file. If file points to a directory, an empty vector is returned instead.
        Parameters:
        file - the file to load
        Returns:
        the content of the file, empty vector if directory
      • isDifferent

        public static boolean isDifferent​(File file1,
                                          File file2)
        Returns whether the two files differ.
        Parameters:
        file1 - the first text file
        file2 - the second text file
        Returns:
        true if different
      • isDifferent

        public static boolean isDifferent​(String[] list1,
                                          String[] list2)
        Returns whether the two lists differ.
        Parameters:
        list1 - the first list
        list2 - the second list
        Returns:
        true if different
      • isDifferent

        public static boolean isDifferent​(List<String> list1,
                                          List<String> list2)
        Returns whether the two lists differ.
        Parameters:
        list1 - the first text file
        list2 - the second text file
        Returns:
        the side-by-side diff (first file: index 0, second file: index 1, indicator: )
      • toString

        protected static String toString​(String ind,
                                         List lines)
        Assembles the lines into a string.
        Parameters:
        ind - the indicator string
        lines - the underlying lines
        Returns:
        the generated string
      • unified

        public static String unified​(File file1,
                                     File file2)
        Generates a unified diff for the two files.
        Parameters:
        file1 - the first text file
        file2 - the second text file
        Returns:
        the unified diff
      • createRange

        protected static String createRange​(difflib.Chunk chunk)
        Creates a range string.
        Parameters:
        chunk - the chunk to create the range string for
        Returns:
        the range string
      • unified

        public static String unified​(List<String> list1,
                                     List<String> list2)
        Generates a unified diff for the two lists.
        Parameters:
        list1 - the first list
        list2 - the second list
        Returns:
        the unified diff
      • sideBySide

        public static DiffUtils.SideBySideDiff sideBySide​(File file1,
                                                          File file2)
        Generates a side-by-side diff for the two files.
        Parameters:
        file1 - the first text file
        file2 - the second text file
        Returns:
        the side-by-side diff
      • sideBySide

        public static DiffUtils.SideBySideDiff sideBySide​(String[] list1,
                                                          String[] list2)
        Generates a side-by-side diff for the two lists.
        Parameters:
        list1 - the first list
        list2 - the second list
        Returns:
        the side-by-side diff
      • addToList

        protected static void addToList​(int from,
                                        int to,
                                        List<String> source,
                                        List dest)
        Adds the specified contents of the source list to the destination list.
        Parameters:
        from - the starting index
        to - the ending index (included)
        source - the source list
        dest - the destination of the data
      • addToList

        protected static void addToList​(int from,
                                        int to,
                                        Object obj,
                                        List dest)
        Adds the object to the destination list.
        Parameters:
        from - the starting index
        to - the ending index (included)
        dest - the destination of the data
      • sideBySide

        public static DiffUtils.SideBySideDiff sideBySide​(List<String> list1,
                                                          List<String> list2)
        Generates a side-by-side diff for the two lists.
        Parameters:
        list1 - the first text file
        list2 - the second text file
        Returns:
        the side-by-side diff
      • main

        public static void main​(String[] args)
                         throws Exception
        Usage: DiffUtils <unified|side-by-side|brief> <file1> <file2>
        Parameters:
        args - the files to compare
        Throws:
        Exception - if comparison fails