Class StringDiff


  • public final class StringDiff
    extends java.lang.Object
    Represents the difference between two Strings. The difference is calculated row wise by a DiffAlgorithm and is internally represented as a List of DiffLine.

    Differences in line breaks are not part of the actual diff but will be determined as well.

    Whether other whitespaces are subject to comparison depends on the used DiffAlgorithm.

    A StringDiff can be rendered as a nice human readable String using a DiffRenderer. Rendering behavior can be fine tuned by providing a DiffSettings instance.

    • Method Detail

      • simple

        public static StringDiff simple​(java.lang.String left,
                                        java.lang.String right)
      • using

        public static StringDiff using​(DiffAlgorithm algorithm,
                                       java.util.List<java.lang.String> left,
                                       java.util.List<java.lang.String> right)
      • using

        public static StringDiff using​(DiffAlgorithm algorithm,
                                       java.lang.String left,
                                       java.lang.String right)
      • textDifferences

        public java.util.List<DiffLine> textDifferences()
        Returns the internal list of differences (read-only).
        Returns:
        The list of differences.
      • leftLineSeparator

        public LineSeparator leftLineSeparator()
        Returns the LineSeparator for the left file of the comparison.
        Returns:
        The line separator.
      • rightLineSeparator

        public LineSeparator rightLineSeparator()
        Returns the LineSeparator of the right file of the comparison.
        Returns:
        The line separator.
      • hasDifference

        public boolean hasDifference()
        Whether the compared Strings differ in either line separators or at least one text difference was found. Equivalent to
         hasDifference = hasLineSeparatorDifference() || hasTextDifference();
         
        Returns:
        Whether any difference has been found.
      • hasTextDifference

        public boolean hasTextDifference()
        Whether at least one text difference has been found.
        Returns:
        Whether at least one difference has been found.
      • hasLineSeparatorDifference

        public boolean hasLineSeparatorDifference()
        Whether compared Strings differed in line separators.
        Returns:
        Whether compared Strings differed in line separators.
      • toString

        public java.lang.String toString​(DiffRenderer diffRenderer,
                                         DiffSettings diffSettings)
        Creates a String representation of this diff via the given DiffRenderer using given DiffSettings.
        Parameters:
        diffRenderer - The renderer.
        diffSettings - The settings that will be passed to the renderer.
        Returns:
        The String representation.
      • toString

        public java.lang.String toString​(DiffRenderer diffRenderer)
      • toString

        public java.lang.String toString()
        Creates a unified rendered diff of this StringDiff using default DiffSettings.
        Overrides:
        toString in class java.lang.Object
        Returns:
        A String representation of this diff.