com.borland.primetime.util
Class Diff

java.lang.Object
  |
  +--com.borland.primetime.util.Diff

public class Diff
extends java.lang.Object

The Diff class is capable of diffing two text files and producing an array of DiffEntries which represent the differences between the two files. The differences can also be expressed as an edit script with add and delete instructions similar to the "--rcs" output of the Unix diff.exe The diff algorithm is based on: An O(NP) Sequence Comparison Algorithm Sun Wu, Udi Manber, Gene Myers, Webb Miller August 1989 To use this file, first instantiate a new Diff object. A Diff object is able to do diff's between BufferedReaders Files, and String arrays, and the result of the last diff is cached in an array of DiffEntry objects. To get the result of a diff, get an iterator that can give the DiffEntry objects one by one, or call toEditScript to get an edit script. As a convenience, this class can turn an edit script into a cached array of DiffEntry objects, as long as the edit script is in the format that diff.exe uses for the --rcs output.


Inner Class Summary
 class Diff.DiffEntry
          A DiffEntry is used to describe a region of lines that are changed from an ORIGINAL file to a NEW file.
 
Field Summary
protected  Diff.DiffEntry[] delta
           
protected  int numEntries
           
 
Constructor Summary
Diff()
          The default constructor just sets up an empty object.
 
Method Summary
 void addAddition(int startLine, java.lang.String[] lines)
          Manually add to the cached array of DiffEntry objects.
 void addChange(int startLine, java.lang.String[] lines)
          Manually add to the cached array of DiffEntry objects A change means 'lines will substitute an equal number of existing lines starting at startLine.
 void addDeletion(int startLine, int count)
          Manually add to the cached array of DiffEntry objects.
 boolean diff(java.io.BufferedReader reader1, java.io.BufferedReader reader2)
          Make a diff between the two buffered inputs and cache the resulting array of DiffEntry objects.
 boolean diff(java.io.File file1, java.io.File file2)
          Make a diff between two files and cache the resulting array of DiffEntry objects.
 boolean diff(java.lang.String[] lines1, java.lang.String[] lines2)
          Make a diff between two arrays of strings and cache the resulting array of DiffEntry objects.
 boolean editScriptToDiff(java.util.Iterator diffFile)
          Given a difference between 2 files, expressed as an edit script produced by diff.exe with the --rcs option, fill this Diff object with DifEntries that are equivalent to the edit script.
 java.util.Iterator iterator()
          Return a new iterator that iterates through the diffEntries in a FIFO order.
 void reset()
          Empty the current cache of DiffEntry objects, which will make the object ready for a fresh diff.
 java.util.Iterator reverseIterator()
          Return a new iterator that iterates through the diffEntries in a FILO order.
 int size()
          Gets the number of valid DiffEntry objects currently cached in our object.
 java.lang.String[] toEditScript()
          Produce an "diff.exe --rcs" compatible edit script from the last diff executed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

delta

protected Diff.DiffEntry[] delta

numEntries

protected int numEntries
Constructor Detail

Diff

public Diff()
The default constructor just sets up an empty object.
Method Detail

size

public int size()
Gets the number of valid DiffEntry objects currently cached in our object.
Returns:
the number of valid DiffEntry objects

reset

public void reset()
Empty the current cache of DiffEntry objects, which will make the object ready for a fresh diff. You normally only call this if the DiffEntry objects are added manually through addAddition, addChange, and addDeletion. All the "diff" calls will automatically reset the object.

diff

public boolean diff(java.io.BufferedReader reader1,
                    java.io.BufferedReader reader2)
Make a diff between the two buffered inputs and cache the resulting array of DiffEntry objects.
Parameters:
reader1 - the first reader
reader2 - the second reader
Returns:
true if the diff succeeded, false otherwise.

diff

public boolean diff(java.lang.String[] lines1,
                    java.lang.String[] lines2)
Make a diff between two arrays of strings and cache the resulting array of DiffEntry objects.
Parameters:
lines1 - the first array of strings
lines2 - the second array of strings
Returns:
true if the diff succeeded, false otherwise.

diff

public boolean diff(java.io.File file1,
                    java.io.File file2)
Make a diff between two files and cache the resulting array of DiffEntry objects.
Parameters:
file1 - the first file
file2 - the second file
Returns:
true if the diff succeeded, false otherwise.

addAddition

public void addAddition(int startLine,
                        java.lang.String[] lines)
Manually add to the cached array of DiffEntry objects. An addition means add the lines stored in the 'lines' array after 'startLine'.
Parameters:
startLine - The line after which we add lines
lines - The lines we will add

addDeletion

public void addDeletion(int startLine,
                        int count)
Manually add to the cached array of DiffEntry objects. A deletion means we delete 'count' lines starting with 'startLine'.
Parameters:
startLine - the first line to be deleted
count - the number of lines to delete

addChange

public void addChange(int startLine,
                      java.lang.String[] lines)
Manually add to the cached array of DiffEntry objects A change means 'lines will substitute an equal number of existing lines starting at startLine.
Parameters:
startLine - the first line to be changed.
lines - the new lines

editScriptToDiff

public boolean editScriptToDiff(java.util.Iterator diffFile)
Given a difference between 2 files, expressed as an edit script produced by diff.exe with the --rcs option, fill this Diff object with DifEntries that are equivalent to the edit script. For convenience we expect an iterator that will give us the lines of the edit script one by one.
Parameters:
diffFile - The difference between 2 files expressed as an iterator over an arrayList of Strings.
Returns:
true if we turned the diff successfully into an array of DiffEntry objects, false otherwise.

toEditScript

public java.lang.String[] toEditScript()
Produce an "diff.exe --rcs" compatible edit script from the last diff executed. The edit script only contains add and delete lines similar to the following lines: a1 2 newline 1 newline 2 d5 3 Explanation of the above script: add two lines after line 1 (the two lines follow this entry) delete 3 lines starting at line 5
Returns:
the array of strings making up the edit script.

iterator

public java.util.Iterator iterator()
Return a new iterator that iterates through the diffEntries in a FIFO order.
Returns:
A forward iterator producing diffEntry objects.

reverseIterator

public java.util.Iterator reverseIterator()
Return a new iterator that iterates through the diffEntries in a FILO order.
Returns:
A reverse iterator producing diffEntry objects.