Package adams.core
Class DiffUtils
- java.lang.Object
-
- adams.core.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)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
DiffUtils.Filler
A helper class for the side-by-side diff.static class
DiffUtils.SideBySideDiff
Container object for a side-by-side diff.
-
Field Summary
Fields Modifier and Type Field Description static String
DESTINATION
the destination indicator.static char
INDICATOR_ADDED
the indicator for "added".static char
INDICATOR_CHANGED
the indicator for "changed".static char
INDICATOR_DELETED
the indicator for "deleted".static char
INDICATOR_SAME
the indicator for "same".static String
OPTION_BRIEF
the brief option.static String
OPTION_SIDEBYSIDE
the side-by-side option.static String
OPTION_UNIFIED
the unified option.static String
SEPARATOR_SIDEBYSIDE
the separator for the side-by-side output on the command-line.static String
SEPARATOR_UNIFIED
the separator for the unified output.static int
SIDEBYSIDE_ENDPOS
the index for the list of end positions of deltas in side-by-side diff.static int
SIDEBYSIDE_FIRST
the index for the first file in side-by-side diff.static int
SIDEBYSIDE_INDICATOR
the index for the indicator list in side-by-side diff.static int
SIDEBYSIDE_SECOND
the index for the second file in side-by-side diff.static int
SIDEBYSIDE_SIZE
the number of array elements in side-by-side diff.static int
SIDEBYSIDE_STARTPOS
the index for the list of start positions of deltas in side-by-side diff.static String
SOURCE
the source indicator.
-
Constructor Summary
Constructors Constructor Description DiffUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description protected static void
addToList(int from, int to, Object obj, List dest)
Adds the object to the destination list.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.protected static String
createRange(difflib.Chunk chunk)
Creates a range string.static boolean
isDifferent(File file1, File file2)
Returns whether the two files differ.static boolean
isDifferent(String[] list1, String[] list2)
Returns whether the two lists differ.static boolean
isDifferent(List<String> list1, List<String> list2)
Returns whether the two lists differ.protected static List<String>
loadFromFile(File file)
Loads the file.static void
main(String[] args)
Usage: DiffUtils <unified|side-by-side|brief> <file1> <file2>static DiffUtils.SideBySideDiff
sideBySide(File file1, File file2)
Generates a side-by-side diff for the two files.static DiffUtils.SideBySideDiff
sideBySide(String[] list1, String[] list2)
Generates a side-by-side diff for the two lists.static DiffUtils.SideBySideDiff
sideBySide(List<String> list1, List<String> list2)
Generates a side-by-side diff for the two lists.protected static String
toString(String ind, List lines)
Assembles the lines into a string.static String
unified(File file1, File file2)
Generates a unified diff for the two files.static String
unified(List<String> list1, List<String> list2)
Generates a unified diff for the two lists.
-
-
-
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
-
SOURCE
public static final String SOURCE
the source indicator.- See Also:
- Constant Field Values
-
DESTINATION
public static final String DESTINATION
the destination indicator.- 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
-
OPTION_UNIFIED
public static final String OPTION_UNIFIED
the unified option.- See Also:
- Constant Field Values
-
OPTION_SIDEBYSIDE
public static final String OPTION_SIDEBYSIDE
the side-by-side option.- See Also:
- Constant Field Values
-
OPTION_BRIEF
public static final String OPTION_BRIEF
the brief option.- 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
-
-
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 filefile2
- 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 listlist2
- 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 filelist2
- 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 stringlines
- 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 filefile2
- 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 listlist2
- 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 filefile2
- 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 listlist2
- 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 indexto
- the ending index (included)source
- the source listdest
- 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 indexto
- 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 filelist2
- the second text file- Returns:
- the side-by-side diff
-
-