Package dk.netarkivet.testutils
Class Diff
- java.lang.Object
-
- dk.netarkivet.testutils.Diff
-
public class Diff extends Object
Compares two collections, returning a list of the additions, changes, and deletions between them. AComparator
may be passed as an argument to the constructor, and will thus be used. If not provided, the initial value in thea
("from") collection will be looked at to see if it supports theComparable
interface. If so, itsequals
andcompareTo
methods will be invoked on the instances in the "from" and "to" collections; 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.
-
-
Constructor Summary
Constructors Constructor Description Diff(Object[] a, Object[] b)
Constructs the Diff object for the two arrays, using the default comparison mechanism between the objects, such asequals
andcompareTo
.Diff(Object[] a, Object[] b, Comparator comp)
Constructs the Diff object for the two arrays, using the given comparator.Diff(Collection a, Collection b)
Constructs the Diff object for the two collections, using the default comparison mechanism between the objects, such asequals
andcompareTo
.Diff(Collection a, Collection b, Comparator comp)
Constructs the Diff object for the two collections, using the given comparator.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
append(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 havefinishedA
invoked at the last element in thea
array.protected boolean
callFinishedB()
Override and return true in order to havefinishedB
invoked at the last element in theb
array.List
diff()
Runs diff and returns the results.protected boolean
equals(Object x, Object y)
Compares the two objects, using the comparator provided with the constructor, if any.protected void
finishedA(int lastA)
Invoked at the last element ina
, ifcallFinishedA
returns true.protected void
finishedB(int lastB)
Invoked at the last element inb
, ifcallFinishedB
returns true.protected Integer
getLastValue()
Returns the value for the greatest key in the map.Integer[]
getLongestCommonSubsequences()
Returns an array of the longest common subsequences.protected Integer
insert(Integer j, Integer k)
Inserts the given values into the threshold map.protected boolean
isGreaterThan(Integer index, Integer val)
Returns whether the value in the map for the given index is greater than the given value.protected boolean
isLessThan(Integer index, Integer val)
Returns whether the value in the map for the given index is less than the given value.protected static boolean
isNonzero(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 ina
and not inb
.protected void
onBNotA(int ai, int bi)
Invoked for elements inb
and not ina
.protected void
onMatch(int ai, int bi)
Invoked for elements matching ina
andb
.protected static Integer[]
toArray(TreeMap map)
Converts the map (indexed by java.lang.Integers) into an array.protected void
traverseSequences()
Traverses the sequences, seeking the longest common subsequences, invoking the methodsfinishedA
,finishedB
,onANotB
, andonBNotA
.
-
-
-
Constructor Detail
-
Diff
public Diff(Object[] a, Object[] b, Comparator comp)
Constructs the Diff object for the two arrays, using the given comparator.
-
Diff
public Diff(Object[] a, Object[] b)
Constructs the Diff object for the two arrays, using the default comparison mechanism between the objects, such asequals
andcompareTo
.
-
Diff
public Diff(Collection a, Collection b, Comparator comp)
Constructs the Diff object for the two collections, using the given comparator.
-
Diff
public Diff(Collection a, Collection b)
Constructs the Diff object for the two collections, using the default comparison mechanism between the objects, such asequals
andcompareTo
.
-
-
Method Detail
-
diff
public List diff()
Runs diff and returns the results.
-
traverseSequences
protected void traverseSequences()
Traverses the sequences, seeking the longest common subsequences, invoking the methodsfinishedA
,finishedB
,onANotB
, andonBNotA
.
-
callFinishedA
protected boolean callFinishedA()
Override and return true in order to havefinishedA
invoked at the last element in thea
array.
-
callFinishedB
protected boolean callFinishedB()
Override and return true in order to havefinishedB
invoked at the last element in theb
array.
-
finishedA
protected void finishedA(int lastA)
Invoked at the last element ina
, ifcallFinishedA
returns true.
-
finishedB
protected void finishedB(int lastB)
Invoked at the last element inb
, ifcallFinishedB
returns true.
-
onANotB
protected void onANotB(int ai, int bi)
Invoked for elements ina
and not inb
.
-
onBNotA
protected void onBNotA(int ai, int bi)
Invoked for elements inb
and not ina
.
-
onMatch
protected void onMatch(int ai, int bi)
Invoked for elements matching ina
andb
.
-
equals
protected boolean equals(Object x, Object y)
Compares the two objects, using the comparator provided with the constructor, if any.
-
getLongestCommonSubsequences
public Integer[] getLongestCommonSubsequences()
Returns an array of the longest common subsequences.
-
toArray
protected static Integer[] toArray(TreeMap map)
Converts the map (indexed by java.lang.Integers) into an array.
-
isNonzero
protected static boolean isNonzero(Integer i)
Returns whether the integer is not zero (including if it is not null).
-
isGreaterThan
protected boolean isGreaterThan(Integer index, Integer val)
Returns whether the value in the map for the given index is greater than the given value.
-
isLessThan
protected boolean isLessThan(Integer index, Integer val)
Returns whether the value in the map for the given index is less than the given value.
-
getLastValue
protected Integer getLastValue()
Returns the value for the greatest key in the map.
-
append
protected void append(Integer value)
Adds the given value to the "end" of the threshold map, that is, with the greatest index/key.
-
-