Package ca.cgjennings.algo
Class Diff<E>
- java.lang.Object
-
- ca.cgjennings.algo.Diff<E>
-
- Type Parameters:
E
- the type of the elements that may be inserted, deleted, or changed; for example, in a text file each element might be aString
representing a single line
public class Diff<E> extends java.lang.Object
A generic implementation of the diff algorithm. This algorithm describes the changes needed to convert one sequence of objects into another. It is most commonly used to determine the changes between two text files by comparing their lines.The algorithm is used in combination with a
DiffListener
. The listener's methods will be called in the order of the original sequence and will describe a combined version of both the original and changed sequences, with insertions and deletions at the appropriate points in order to describe the actions that would covert the original sequence to the new sequence in the minimal number of steps.- Since:
- 2.0
- Author:
- Chris Jennings
-
-
Constructor Summary
Constructors Constructor Description Diff()
Create a newDiff
instance that uses a default listener that is useful for diagnostic purposes.Diff(DiffListener<E> listener)
A diff engine that posts differences to the specified listener.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equal(E a, E b, int originalIndex, int changedIndex)
The method used to compare two entries for equality.void
findChanges(E[] original, E[] changed)
Determine the difference between the original and changed element sequences, posting them to the listener supplied at construction.
-
-
-
Constructor Detail
-
Diff
public Diff()
Create a newDiff
instance that uses a default listener that is useful for diagnostic purposes. For each element in the combined collection, this listener prints to the output stream one of the following followed by the string value of the element in question:- "> "
- this element must be inserted
- "X "
- this element must be deleted
- " "
- this element is unchanged
-
Diff
public Diff(DiffListener<E> listener)
A diff engine that posts differences to the specified listener.- Parameters:
listener
- a listener to be notified of the sequence of changes required to modify the original version of an entity to the changed version
-
-
Method Detail
-
findChanges
public void findChanges(E[] original, E[] changed)
Determine the difference between the original and changed element sequences, posting them to the listener supplied at construction.- Parameters:
original
- the original sequence of elementschanged
- the changed sequence of elements
-
equal
public boolean equal(E a, E b, int originalIndex, int changedIndex)
The method used to compare two entries for equality. The default implementation is equivalent to:if( a == null ) { return b == null; } else { return a.equals( b ); }
- Parameters:
a
- an item from the original collection being comparedb
- an item from the changed collection being comparedoriginalIndex
- the index ofa
in the original sequencechangedIndex
- the index ofb
in the changed sequence- Returns:
true
if the objects should be considered equal for the purposes of the diff operation
-
-