Class DiffResult

  • All Implemented Interfaces:
    java.lang.Iterable<Diff<?>>

    public class DiffResult
    extends java.lang.Object
    implements java.lang.Iterable<Diff<?>>

    A DiffResult contains a collection of the differences between two Diffable objects. Typically these differences are displayed using toString() method, which returns a string describing the fields that differ between the objects.

    Use a DiffBuilder to build a DiffResult comparing two objects.

    Since:
    3.3
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String DIFFERS_STRING  
      private java.util.List<Diff<?>> diffs  
      private java.lang.Object lhs  
      static java.lang.String OBJECTS_SAME_STRING
      The String returned when the objects have no differences: ""
      private java.lang.Object rhs  
      private ToStringStyle style  
    • Constructor Summary

      Constructors 
      Constructor Description
      DiffResult​(java.lang.Object lhs, java.lang.Object rhs, java.util.List<Diff<?>> diffs, ToStringStyle style)
      Creates a DiffResult containing the differences between two objects.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.List<Diff<?>> getDiffs()
      Returns an unmodifiable list of Diffs.
      int getNumberOfDiffs()
      Returns the number of differences between the two objects.
      ToStringStyle getToStringStyle()
      Returns the style used by the toString() method.
      java.util.Iterator<Diff<?>> iterator()
      Returns an iterator over the Diff objects contained in this list.
      java.lang.String toString()
      Builds a String description of the differences contained within this DiffResult.
      java.lang.String toString​(ToStringStyle style)
      Builds a String description of the differences contained within this DiffResult, using the supplied ToStringStyle.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Field Detail

      • OBJECTS_SAME_STRING

        public static final java.lang.String OBJECTS_SAME_STRING

        The String returned when the objects have no differences: ""

        See Also:
        Constant Field Values
      • diffs

        private final java.util.List<Diff<?>> diffs
      • lhs

        private final java.lang.Object lhs
      • rhs

        private final java.lang.Object rhs
    • Constructor Detail

      • DiffResult

        DiffResult​(java.lang.Object lhs,
                   java.lang.Object rhs,
                   java.util.List<Diff<?>> diffs,
                   ToStringStyle style)

        Creates a DiffResult containing the differences between two objects.

        Parameters:
        lhs - the left hand object
        rhs - the right hand object
        diffs - the list of differences, may be empty
        style - the style to use for the toString() method. May be null, in which case ToStringStyle.DEFAULT_STYLE is used
        Throws:
        java.lang.IllegalArgumentException - if lhs, rhs or diffs is null
    • Method Detail

      • getDiffs

        public java.util.List<Diff<?>> getDiffs()

        Returns an unmodifiable list of Diffs. The list may be empty if there were no differences between the objects.

        Returns:
        an unmodifiable list of Diffs
      • getNumberOfDiffs

        public int getNumberOfDiffs()

        Returns the number of differences between the two objects.

        Returns:
        the number of differences
      • getToStringStyle

        public ToStringStyle getToStringStyle()

        Returns the style used by the toString() method.

        Returns:
        the style
      • toString

        public java.lang.String toString()

        Builds a String description of the differences contained within this DiffResult. A ToStringBuilder is used for each object and the style of the output is governed by the ToStringStyle passed to the constructor.

        If there are no differences stored in this list, the method will return OBJECTS_SAME_STRING. Otherwise, using the example given in Diffable and ToStringStyle.SHORT_PREFIX_STYLE, an output might be:

         Person[name=John Doe,age=32] differs from Person[name=Joe Bloggs,age=26]
         

        This indicates that the objects differ in name and age, but not in smoking status.

        To use a different ToStringStyle for an instance of this class, use toString(ToStringStyle).

        Overrides:
        toString in class java.lang.Object
        Returns:
        a String description of the differences.
      • toString

        public java.lang.String toString​(ToStringStyle style)

        Builds a String description of the differences contained within this DiffResult, using the supplied ToStringStyle.

        Parameters:
        style - the ToStringStyle to use when outputting the objects
        Returns:
        a String description of the differences.
      • iterator

        public java.util.Iterator<Diff<?>> iterator()

        Returns an iterator over the Diff objects contained in this list.

        Specified by:
        iterator in interface java.lang.Iterable<Diff<?>>
        Returns:
        the iterator