Top Description Fields Constructors Methods
org.apache.commons.lang3.builder

public Class DiffResult<T>

extends Object
implements Iterable<Diff<?>>
Class Inheritance
All Implemented Interfaces
java.lang.Iterable
Type Parameters
<T>
type of the left and right object.
Imports
java.util.Collections, .Iterator, .List, .Objects, org.apache.commons.lang3.StringUtils

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

Modifier and TypeField and Description
private final List<Diff<?>>
private final T
public static final String
OBJECTS_SAME_STRING

The String returned when the objects have no differences:

private final T
private final ToStringStyle
private final String

Constructor Summary

AccessConstructor and Description
pack-priv
DiffResult(final T
the left-hand side object
lhs
,
final T
the right-hand side object
rhs
,
final List<Diff<?>>
the list of differences, may be empty
diffList
,
final ToStringStyle
the style to use for the toString() method. May be null, in which case ToStringStyle#DEFAULT_STYLE is used
style
,
final String
Two-argument format string for String#format(String, Object...), for example "%s differs from %s".
toStringFormat
)

Creates a DiffResult containing the differences between two objects.

Method Summary

Modifier and TypeMethod and Description
public List<Diff<?>>

Returns:

an unmodifiable list of Diffs
getDiffs
()

Gets an unmodifiable list of Diffs.

public T

Returns:

the left object of the diff
getLeft
()

Gets the object the right object has been compared to.

public int

Returns:

the number of differences
getNumberOfDiffs
()

Gets the number of differences between the two objects.

public T

Returns:

the right object of the diff
getRight
()

Gets the object the left object has been compared to.

public ToStringStyle

Returns:

the style
getToStringStyle
()

Gets the style used by the toString() method.

public Iterator<Diff<?>>

Returns:

the iterator
iterator
()

Implements java.lang.Iterable.iterator.

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

public String

Returns:

a String description of the differences.
toString
()

Overrides java.lang.Object.toString.

Builds a String description of the differences contained within this DiffResult.

public String

Returns:

a String description of the differences.
toString
(final ToStringStyle
the ToStringStyle to use when outputting the objects
style
)

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

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

diffListback to summary
private final List<Diff<?>> diffList
lhsback to summary
private final T lhs
OBJECTS_SAME_STRINGback to summary
public static final String OBJECTS_SAME_STRING

The String returned when the objects have no differences:

rhsback to summary
private final T rhs
styleback to summary
private final ToStringStyle style
toStringFormatback to summary
private final String toStringFormat

Constructor Detail

DiffResultback to summary
pack-priv DiffResult(final T lhs, final T rhs, final List<Diff<?>> diffList, final ToStringStyle style, final String toStringFormat)

Creates a DiffResult containing the differences between two objects.

Parameters
lhs:T

the left-hand side object

rhs:T

the right-hand side object

diffList:List<Diff<?>>

the list of differences, may be empty

style:ToStringStyle

the style to use for the toString() method. May be null, in which case ToStringStyle#DEFAULT_STYLE is used

toStringFormat:String

Two-argument format string for String#format(String, Object...), for example "%s differs from %s".

Exceptions
NullPointerException:
if lhs, rhs or diffs are null.

Method Detail

getDiffsback to summary
public List<Diff<?>> getDiffs()

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

Returns:List<Diff<?>>

an unmodifiable list of Diffs

getLeftback to summary
public T getLeft()

Gets the object the right object has been compared to.

Returns:T

the left object of the diff

Since
3.10
getNumberOfDiffsback to summary
public int getNumberOfDiffs()

Gets the number of differences between the two objects.

Returns:int

the number of differences

getRightback to summary
public T getRight()

Gets the object the left object has been compared to.

Returns:T

the right object of the diff

Since
3.10
getToStringStyleback to summary
public ToStringStyle getToStringStyle()

Gets the style used by the toString() method.

Returns:ToStringStyle

the style

iteratorback to summary
public Iterator<Diff<?>> iterator()

Implements java.lang.Iterable.iterator.

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

Returns:Iterator<Diff<?>>

the iterator

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.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).

Returns:String

a String description of the differences.

Annotations
@Override
toStringback to summary
public String toString(final ToStringStyle style)

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

Parameters
style:ToStringStyle

the ToStringStyle to use when outputting the objects

Returns:String

a String description of the differences.