Class Diff


  • public class Diff
    extends 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.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected Object[] a
      The source array, AKA the "from" values.
      protected Object[] b
      The target array, AKA the "to" values.
      protected List diffs
      The list of differences, as Difference instances.
    • 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 as equals and compareTo.
      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 as equals and compareTo.
      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 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.
      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 in a, if callFinishedA returns true.
      protected void finishedB​(int lastB)
      Invoked at the last element in b, if callFinishedB 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 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 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 methods finishedA, finishedB, onANotB, and onBNotA.
    • Field Detail

      • a

        protected Object[] a
        The source array, AKA the "from" values.
      • b

        protected Object[] b
        The target array, AKA the "to" values.
      • diffs

        protected List diffs
        The list of differences, as Difference instances.
    • 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 as equals and compareTo.
      • 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 as equals and compareTo.
    • 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 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.
      • callFinishedB

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

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

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

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

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

        protected void onMatch​(int ai,
                               int bi)
        Invoked for elements matching in a and b.
      • 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.
      • insert

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