Class DiffResult
- java.lang.Object
-
- org.apache.commons.lang3.builder.DiffResult
-
- All Implemented Interfaces:
java.lang.Iterable<Diff<?>>
public class DiffResult extends java.lang.Object implements java.lang.Iterable<Diff<?>>
A
DiffResultcontains a collection of the differences between twoDiffableobjects. Typically these differences are displayed usingtoString()method, which returns a string describing the fields that differ between the objects.Use a
DiffBuilderto build aDiffResultcomparing two objects.- Since:
- 3.3
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringDIFFERS_STRINGprivate java.util.List<Diff<?>>diffsprivate java.lang.Objectlhsstatic java.lang.StringOBJECTS_SAME_STRINGTheStringreturned when the objects have no differences: ""private java.lang.Objectrhsprivate ToStringStylestyle
-
Constructor Summary
Constructors Constructor Description DiffResult(java.lang.Object lhs, java.lang.Object rhs, java.util.List<Diff<?>> diffs, ToStringStyle style)Creates aDiffResultcontaining 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 ofDiffs.intgetNumberOfDiffs()Returns the number of differences between the two objects.ToStringStylegetToStringStyle()Returns the style used by thetoString()method.java.util.Iterator<Diff<?>>iterator()Returns an iterator over theDiffobjects contained in this list.java.lang.StringtoString()Builds aStringdescription of the differences contained within thisDiffResult.java.lang.StringtoString(ToStringStyle style)Builds aStringdescription of the differences contained within thisDiffResult, using the suppliedToStringStyle.
-
-
-
Field Detail
-
OBJECTS_SAME_STRING
public static final java.lang.String OBJECTS_SAME_STRING
The
Stringreturned when the objects have no differences: ""- See Also:
- Constant Field Values
-
DIFFERS_STRING
private static final java.lang.String DIFFERS_STRING
- 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
-
style
private final ToStringStyle style
-
-
Constructor Detail
-
DiffResult
DiffResult(java.lang.Object lhs, java.lang.Object rhs, java.util.List<Diff<?>> diffs, ToStringStyle style)Creates a
DiffResultcontaining the differences between two objects.- Parameters:
lhs- the left hand objectrhs- the right hand objectdiffs- the list of differences, may be emptystyle- the style to use for thetoString()method. May benull, in which caseToStringStyle.DEFAULT_STYLEis used- Throws:
java.lang.IllegalArgumentException- iflhs,rhsordiffsisnull
-
-
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
Stringdescription of the differences contained within thisDiffResult. AToStringBuilderis used for each object and the style of the output is governed by theToStringStylepassed to the constructor.If there are no differences stored in this list, the method will return
OBJECTS_SAME_STRING. Otherwise, using the example given inDiffableandToStringStyle.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
ToStringStylefor an instance of this class, usetoString(ToStringStyle).- Overrides:
toStringin classjava.lang.Object- Returns:
- a
Stringdescription of the differences.
-
toString
public java.lang.String toString(ToStringStyle style)
Builds a
Stringdescription of the differences contained within thisDiffResult, using the suppliedToStringStyle.- Parameters:
style- theToStringStyleto use when outputting the objects- Returns:
- a
Stringdescription of the differences.
-
-