com.flexive.shared
Class FxDiff

java.lang.Object
  extended by com.flexive.shared.FxDiff

public class FxDiff
extends java.lang.Object

Compares two collections, returning a list of the additions, changes, and deletions between them. A 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") collection 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" 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.

Author:
Jeff Pace (jpace at incava dot org) - original author, Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)

Nested Class Summary
static class FxDiff.Difference
          Represents a difference, as used in FxDiff.
 
Field Summary
protected  java.lang.Object[] a
          The source array, AKA the "from" values.
protected  java.lang.Object[] b
          The target array, AKA the "to" values.
protected  java.util.List<FxDiff.Difference> diffs
          The list of differences, as Difference instances.
 
Constructor Summary
FxDiff(java.util.Collection a, java.util.Collection b)
          Constructs the Diff object for the two collections, using the default comparison mechanism between the objects, such as equals and compareTo.
FxDiff(java.util.Collection a, java.util.Collection b, java.util.Comparator<java.lang.Object> comp)
          Constructs the Diff object for the two collections, using the given comparator.
FxDiff(java.lang.Object[] a, java.lang.Object[] b)
          Constructs the Diff object for the two arrays, using the default comparison mechanism between the objects, such as equals and compareTo.
FxDiff(java.lang.Object[] a, java.lang.Object[] b, java.util.Comparator<java.lang.Object> comp)
          Constructs the Diff object for the two arrays, using the given comparator.
 
Method Summary
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<FxDiff.Difference> diff()
          Runs diff and returns the results.
protected  boolean equals(java.lang.Object x, java.lang.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 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 static java.lang.Integer[] toArray(java.util.TreeMap<java.lang.Integer,java.lang.Integer> 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 methods finishedA, finishedB, onANotB, and onBNotA.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

a

protected java.lang.Object[] a
The source array, AKA the "from" values.


b

protected java.lang.Object[] b
The target array, AKA the "to" values.


diffs

protected java.util.List<FxDiff.Difference> diffs
The list of differences, as Difference instances.

Constructor Detail

FxDiff

public FxDiff(java.lang.Object[] a,
              java.lang.Object[] b,
              java.util.Comparator<java.lang.Object> comp)
Constructs the Diff object for the two arrays, using the given comparator.

Parameters:
a - array A
b - array B
comp - comparator

FxDiff

public FxDiff(java.lang.Object[] a,
              java.lang.Object[] b)
Constructs the Diff object for the two arrays, using the default comparison mechanism between the objects, such as equals and compareTo.

Parameters:
a - array A
b - array B

FxDiff

public FxDiff(java.util.Collection a,
              java.util.Collection b,
              java.util.Comparator<java.lang.Object> comp)
Constructs the Diff object for the two collections, using the given comparator.

Parameters:
a - collection A
b - collection B
comp - comparator

FxDiff

public FxDiff(java.util.Collection a,
              java.util.Collection b)
Constructs the Diff object for the two collections, using the default comparison mechanism between the objects, such as equals and compareTo.

Parameters:
a - collection A
b - collection B
Method Detail

diff

public java.util.List<FxDiff.Difference> diff()
Runs diff and returns the results.

Returns:
list of differences

traverseSequences

protected void traverseSequences()
Traverses the sequences, seeking the longest common subsequences, invoking the methods finishedA, finishedB, onANotB, and onBNotA.


callFinishedA

protected boolean callFinishedA()
Override and return true in order to have finishedA invoked at the last element in the a array.

Returns:
return true in order to have finishedA invoked at the last element in the a array

callFinishedB

protected boolean callFinishedB()
Override and return true in order to have finishedB invoked at the last element in the b array.

Returns:
return true in order to have finishedA invoked at the last element in the a array

finishedA

protected void finishedA(int lastA)
Invoked at the last element in a, if callFinishedA returns true.

Parameters:
lastA - last line in A

finishedB

protected void finishedB(int lastB)
Invoked at the last element in b, if callFinishedB returns true.

Parameters:
lastB - last line in B

onANotB

protected void onANotB(int ai,
                       int bi)
Invoked for elements in a and not in b.

Parameters:
ai - line in A
bi - line in B

onBNotA

protected void onBNotA(int ai,
                       int bi)
Invoked for elements in b and not in a.

Parameters:
ai - line in A
bi - line in B

onMatch

protected void onMatch(int ai,
                       int bi)
Invoked for elements matching in a and b.

Parameters:
ai - line in A
bi - line in B

equals

protected boolean equals(java.lang.Object x,
                         java.lang.Object y)
Compares the two objects, using the comparator provided with the constructor, if any.

Parameters:
x - X
y - Y
Returns:
equality

getLongestCommonSubsequences

public java.lang.Integer[] getLongestCommonSubsequences()
Returns an array of the longest common subsequences.

Returns:
array of the longest common subsequences

toArray

protected static java.lang.Integer[] toArray(java.util.TreeMap<java.lang.Integer,java.lang.Integer> map)
Converts the map (indexed by java.lang.Integers) into an array.

Parameters:
map - map the convert
Returns:
array

isNonzero

protected static boolean isNonzero(java.lang.Integer i)
Returns whether the integer is not zero (including if it is not null).

Parameters:
i - value to check
Returns:
non zero

isGreaterThan

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.

Parameters:
index - index in threshold
val - value to compare
Returns:
if greater

isLessThan

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.

Parameters:
index - index in threshold
val - value to compare
Returns:
if less than

getLastValue

protected java.lang.Integer getLastValue()
Returns the value for the greatest key in the map.

Returns:
last value

append

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.

Parameters:
value - the value to append

insert

protected java.lang.Integer insert(java.lang.Integer j,
                                   java.lang.Integer k)
Inserts the given values into the threshold map.

Parameters:
j - j
k - k
Returns:
k