Class DocumentDiff


  • public class DocumentDiff
    extends java.lang.Object
    The document diff is used to create a diff between 2 documents. It provides 2 possibilites to use the differences: either using the ChangeListener which is called for each line of the document, or using the Hunks that form the differences of this diff.
    • Constructor Summary

      Constructors 
      Constructor Description
      DocumentDiff​(Document left, Document right)
      Create a new diff from the given 2 documents
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Hunk getHunks()
      Returns the linked list of hunks
      ElementsFactory getMergedLeft()
      Returns an element factory that provides the elements of the merged result of this diff where the left document is dominant.
      ElementsFactory getMergedRight()
      Returns an element factory that provides the elements of the merged result of this diff where the right document is dominant.
      int getNumDeltaElements()
      Returns the number of changed elements.
      void showChanges​(ChangeListener listener, int numContextLines)
      Iterate over all changes and invoke the respective methods in the given listener.
      void write​(DiffWriter out, int numContextLines)
      Writes the differences to the given writer in a unified diff format.
      void write​(java.io.Writer out, int numContextLines)
      Same as write(DiffWriter, int) but wraps the given writer with a default diff writer.
      void write​(java.lang.StringBuffer buf, int numContextLines)
      Same as write(Writer, int) but to a string buffer.
      void write​(java.lang.StringBuffer buf, java.lang.String lineSeparator, int numContextLines)
      Same as write(DiffWriter, int) but to a string buffer.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DocumentDiff

        public DocumentDiff​(Document left,
                            Document right)
        Create a new diff from the given 2 documents
        Parameters:
        left - the left document
        right - the right document
    • Method Detail

      • getNumDeltaElements

        public int getNumDeltaElements()
        Returns the number of changed elements. each insertion and each deletion counts as 1 change. if elements were modified the greate change is counted. eg: if you change 'foo' to 'bar' this is actually 1 deletion and 1 insertion but counts as 1 change. if you change 'foo\nbar\n' to 'hello' this counts as 2 changes since this includes 2 deletions.
        Returns:
        the number of changed elements.
      • getHunks

        public Hunk getHunks()
        Returns the linked list of hunks
        Returns:
        the hunks.
      • write

        public void write​(java.lang.StringBuffer buf,
                          int numContextLines)
        Same as write(Writer, int) but to a string buffer.
        Parameters:
        buf - the buffer
        numContextLines - the number of context lines.
      • write

        public void write​(java.lang.StringBuffer buf,
                          java.lang.String lineSeparator,
                          int numContextLines)
        Same as write(DiffWriter, int) but to a string buffer.
        Parameters:
        buf - the buffer
        lineSeparator - the line separator to use
        numContextLines - the number of context lines.
      • write

        public void write​(java.io.Writer out,
                          int numContextLines)
                   throws java.io.IOException
        Same as write(DiffWriter, int) but wraps the given writer with a default diff writer.
        Parameters:
        out - the writer
        numContextLines - the number of context lines.
        Throws:
        java.io.IOException - if an I/O error occurs
      • write

        public void write​(DiffWriter out,
                          int numContextLines)
                   throws java.io.IOException
        Writes the differences to the given writer in a unified diff format. the context lines specify how many unmodified lines should sourround the actual difference.
        Parameters:
        out - the writer
        numContextLines - the number of context lines.
        Throws:
        java.io.IOException - if an I/O error occurs
      • showChanges

        public void showChanges​(ChangeListener listener,
                                int numContextLines)
        Iterate over all changes and invoke the respective methods in the given listener. the context lines specify how many unmodified lines should sourround the respective change.
        Parameters:
        listener - the change listener
        numContextLines - the number of context lines
      • getMergedLeft

        public ElementsFactory getMergedLeft()
        Returns an element factory that provides the elements of the merged result of this diff where the left document is dominant.
        Returns:
        the merged elements.
      • getMergedRight

        public ElementsFactory getMergedRight()
        Returns an element factory that provides the elements of the merged result of this diff where the right document is dominant.
        Returns:
        the merged elements.