public class Diff<Type>
extends java.lang.Object
Comparator
may be passed as an argument to the
constructor, and will thus be used. If not provided, the initial value in the
a
("from") list will be looked at to see if it supports the
Comparable
interface. If so, its equals
and
compareTo
methods will be invoked on the instances in the "from"
and "to" lists; otherwise, for speed, hash codes from the objects will be
used instead for comparison.
The file FileDiff.java shows an example usage of this class, in an application similar to the Unix "diff" program.
Modifier and Type | Field and Description |
---|---|
protected java.util.List<Type> |
a
The source list, AKA the "from" values.
|
protected java.util.List<Type> |
b
The target list, AKA the "to" values.
|
protected java.util.List<Difference> |
diffs
The list of differences, as
Difference instances. |
Constructor and Description |
---|
Diff(java.util.List<Type> a,
java.util.List<Type> b)
Constructs the Diff object for the two lists, using the default
comparison mechanism between the objects, such as
equals and
compareTo . |
Diff(java.util.List<Type> a,
java.util.List<Type> b,
java.util.Comparator<Type> comp)
Constructs the Diff object for the two lists, using the given comparator.
|
Diff(Type[] a,
Type[] b)
Constructs the Diff object for the two arrays, using the default
comparison mechanism between the objects, such as
equals and
compareTo . |
Diff(Type[] a,
Type[] b,
java.util.Comparator<Type> comp)
Constructs the Diff object for the two arrays, using the given comparator.
|
Modifier and Type | Method and Description |
---|---|
protected void |
append(java.lang.Integer value)
Adds the given value to the "end" of the threshold map, that is, with the
greatest index/key.
|
protected boolean |
callFinishedA()
Override and return true in order to have
finishedA invoked
at the last element in the a array. |
protected boolean |
callFinishedB()
Override and return true in order to have
finishedB invoked
at the last element in the b array. |
java.util.List<Difference> |
diff()
Runs diff and returns the results.
|
protected boolean |
equals(Type x,
Type y)
Compares the two objects, using the comparator provided with the
constructor, if any.
|
protected void |
finishedA(int lastA)
Invoked at the last element in
a , if
callFinishedA returns true. |
protected void |
finishedB(int lastB)
Invoked at the last element in
b , if
callFinishedB returns true. |
protected java.lang.Integer |
getLastValue()
Returns the value for the greatest key in the map.
|
java.lang.Integer[] |
getLongestCommonSubsequences()
Returns an array of the longest common subsequences.
|
protected java.lang.Integer |
insert(java.lang.Integer j,
java.lang.Integer k)
Inserts the given values into the threshold map.
|
protected boolean |
isGreaterThan(java.lang.Integer index,
java.lang.Integer val)
Returns whether the value in the map for the given index is greater than
the given value.
|
protected boolean |
isLessThan(java.lang.Integer index,
java.lang.Integer val)
Returns whether the value in the map for the given index is less than
the given value.
|
protected static boolean |
isNonzero(java.lang.Integer i)
Returns whether the integer is not zero (including if it is not null).
|
protected void |
onANotB(int ai,
int bi)
Invoked for elements in
a and not in b . |
protected void |
onBNotA(int ai,
int bi)
Invoked for elements in
b and not in a . |
protected void |
onMatch(int ai,
int bi)
Invoked for elements matching in
a and b . |
protected void |
traverseSequences()
Traverses the sequences, seeking the longest common subsequences,
invoking the methods
finishedA , finishedB ,
onANotB , and onBNotA . |
protected java.util.List<Type> a
protected java.util.List<Type> b
protected java.util.List<Difference> diffs
Difference
instances.public Diff(Type[] a, Type[] b, java.util.Comparator<Type> comp)
public Diff(Type[] a, Type[] b)
equals
and
compareTo
.public Diff(java.util.List<Type> a, java.util.List<Type> b, java.util.Comparator<Type> comp)
public java.util.List<Difference> diff()
protected void traverseSequences()
finishedA
, finishedB
,
onANotB
, and onBNotA
.protected boolean callFinishedA()
finishedA
invoked
at the last element in the a
array.protected boolean callFinishedB()
finishedB
invoked
at the last element in the b
array.protected void finishedA(int lastA)
a
, if
callFinishedA
returns true.protected void finishedB(int lastB)
b
, if
callFinishedB
returns true.protected void onANotB(int ai, int bi)
a
and not in b
.protected void onBNotA(int ai, int bi)
b
and not in a
.protected void onMatch(int ai, int bi)
a
and b
.protected boolean equals(Type x, Type y)
public java.lang.Integer[] getLongestCommonSubsequences()
protected static boolean isNonzero(java.lang.Integer i)
protected boolean isGreaterThan(java.lang.Integer index, java.lang.Integer val)
protected boolean isLessThan(java.lang.Integer index, java.lang.Integer val)
protected java.lang.Integer getLastValue()
protected void append(java.lang.Integer value)
protected java.lang.Integer insert(java.lang.Integer j, java.lang.Integer k)