Class DiffBuilder
- java.lang.Object
-
- org.apache.commons.lang3.builder.DiffBuilder
-
- All Implemented Interfaces:
Builder<DiffResult>
public class DiffBuilder extends java.lang.Object implements Builder<DiffResult>
Assists in implementing
Diffable.diff(Object)
methods.To use this class, write code as follows:
public class Person implements Diffable<Person> { String name; int age; boolean smoker; ... public DiffResult diff(Person obj) { // No need for null check, as NullPointerException correct if obj is null return new DiffBuilder(this, obj, ToStringStyle.SHORT_PREFIX_STYLE) .append("name", this.name, obj.name) .append("age", this.age, obj.age) .append("smoker", this.smoker, obj.smoker) .build(); } }
The
ToStringStyle
passed to the constructor is embedded in the returnedDiffResult
and influences the style of theDiffResult.toString()
method. This style choice can be overridden by callingDiffResult.toString(ToStringStyle)
.- Since:
- 3.3
- See Also:
Diffable
,Diff
,DiffResult
,ToStringStyle
-
-
Constructor Summary
Constructors Constructor Description DiffBuilder(java.lang.Object lhs, java.lang.Object rhs, ToStringStyle style)
Constructs a builder for the specified objects with the specified style.DiffBuilder(java.lang.Object lhs, java.lang.Object rhs, ToStringStyle style, boolean testTriviallyEqual)
Constructs a builder for the specified objects with the specified style.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description DiffBuilder
append(java.lang.String fieldName, boolean[] lhs, boolean[] rhs)
Test if twoboolean[]
s are equal.DiffBuilder
append(java.lang.String fieldName, boolean lhs, boolean rhs)
Test if twoboolean
s are equal.DiffBuilder
append(java.lang.String fieldName, byte[] lhs, byte[] rhs)
Test if twobyte[]
s are equal.DiffBuilder
append(java.lang.String fieldName, byte lhs, byte rhs)
Test if twobyte
s are equal.DiffBuilder
append(java.lang.String fieldName, char[] lhs, char[] rhs)
Test if twochar[]
s are equal.DiffBuilder
append(java.lang.String fieldName, char lhs, char rhs)
Test if twochar
s are equal.DiffBuilder
append(java.lang.String fieldName, double[] lhs, double[] rhs)
Test if twodouble[]
s are equal.DiffBuilder
append(java.lang.String fieldName, double lhs, double rhs)
Test if twodouble
s are equal.DiffBuilder
append(java.lang.String fieldName, float[] lhs, float[] rhs)
Test if twofloat[]
s are equal.DiffBuilder
append(java.lang.String fieldName, float lhs, float rhs)
Test if twofloat
s are equal.DiffBuilder
append(java.lang.String fieldName, int[] lhs, int[] rhs)
Test if twoint[]
s are equal.DiffBuilder
append(java.lang.String fieldName, int lhs, int rhs)
Test if twoint
s are equal.DiffBuilder
append(java.lang.String fieldName, long[] lhs, long[] rhs)
Test if twolong[]
s are equal.DiffBuilder
append(java.lang.String fieldName, long lhs, long rhs)
Test if twolong
s are equal.DiffBuilder
append(java.lang.String fieldName, short[] lhs, short[] rhs)
Test if twoshort[]
s are equal.DiffBuilder
append(java.lang.String fieldName, short lhs, short rhs)
Test if twoshort
s are equal.DiffBuilder
append(java.lang.String fieldName, java.lang.Object[] lhs, java.lang.Object[] rhs)
Test if twoObject[]
s are equal.DiffBuilder
append(java.lang.String fieldName, java.lang.Object lhs, java.lang.Object rhs)
Test if twoObjects
s are equal.DiffBuilder
append(java.lang.String fieldName, DiffResult diffResult)
Append diffs from anotherDiffResult
.DiffResult
build()
Builds aDiffResult
based on the differences appended to this builder.
-
-
-
Constructor Detail
-
DiffBuilder
public DiffBuilder(java.lang.Object lhs, java.lang.Object rhs, ToStringStyle style, boolean testTriviallyEqual)
Constructs a builder for the specified objects with the specified style.
If
lhs == rhs
orlhs.equals(rhs)
then the builder will not evaluate any calls toappend(...)
and will return an emptyDiffResult
whenbuild()
is executed.- Parameters:
lhs
-this
objectrhs
- the object to diff againststyle
- the style will use when outputting the objects,null
uses the defaulttestTriviallyEqual
- If true, this will test if lhs and rhs are the same or equal. All of the append(fieldName, lhs, rhs) methods will abort without creating a fieldDiff
if the trivially equal test is enabled and returns true. The result of this test is never changed throughout the life of thisDiffBuilder
.- Throws:
java.lang.IllegalArgumentException
- iflhs
orrhs
isnull
- Since:
- 3.4
-
DiffBuilder
public DiffBuilder(java.lang.Object lhs, java.lang.Object rhs, ToStringStyle style)
Constructs a builder for the specified objects with the specified style.
If
lhs == rhs
orlhs.equals(rhs)
then the builder will not evaluate any calls toappend(...)
and will return an emptyDiffResult
whenbuild()
is executed.This delegates to
DiffBuilder(Object, Object, ToStringStyle, boolean)
with the testTriviallyEqual flag enabled.- Parameters:
lhs
-this
objectrhs
- the object to diff againststyle
- the style will use when outputting the objects,null
uses the default- Throws:
java.lang.IllegalArgumentException
- iflhs
orrhs
isnull
-
-
Method Detail
-
append
public DiffBuilder append(java.lang.String fieldName, boolean lhs, boolean rhs)
Test if two
boolean
s are equal.- Parameters:
fieldName
- the field namelhs
- the left handboolean
rhs
- the right handboolean
- Returns:
- this
- Throws:
java.lang.IllegalArgumentException
- if field name isnull
-
append
public DiffBuilder append(java.lang.String fieldName, boolean[] lhs, boolean[] rhs)
Test if two
boolean[]
s are equal.- Parameters:
fieldName
- the field namelhs
- the left handboolean[]
rhs
- the right handboolean[]
- Returns:
- this
- Throws:
java.lang.IllegalArgumentException
- if field name isnull
-
append
public DiffBuilder append(java.lang.String fieldName, byte lhs, byte rhs)
Test if two
byte
s are equal.- Parameters:
fieldName
- the field namelhs
- the left handbyte
rhs
- the right handbyte
- Returns:
- this
- Throws:
java.lang.IllegalArgumentException
- if field name isnull
-
append
public DiffBuilder append(java.lang.String fieldName, byte[] lhs, byte[] rhs)
Test if two
byte[]
s are equal.- Parameters:
fieldName
- the field namelhs
- the left handbyte[]
rhs
- the right handbyte[]
- Returns:
- this
- Throws:
java.lang.IllegalArgumentException
- if field name isnull
-
append
public DiffBuilder append(java.lang.String fieldName, char lhs, char rhs)
Test if two
char
s are equal.- Parameters:
fieldName
- the field namelhs
- the left handchar
rhs
- the right handchar
- Returns:
- this
- Throws:
java.lang.IllegalArgumentException
- if field name isnull
-
append
public DiffBuilder append(java.lang.String fieldName, char[] lhs, char[] rhs)
Test if two
char[]
s are equal.- Parameters:
fieldName
- the field namelhs
- the left handchar[]
rhs
- the right handchar[]
- Returns:
- this
- Throws:
java.lang.IllegalArgumentException
- if field name isnull
-
append
public DiffBuilder append(java.lang.String fieldName, double lhs, double rhs)
Test if two
double
s are equal.- Parameters:
fieldName
- the field namelhs
- the left handdouble
rhs
- the right handdouble
- Returns:
- this
- Throws:
java.lang.IllegalArgumentException
- if field name isnull
-
append
public DiffBuilder append(java.lang.String fieldName, double[] lhs, double[] rhs)
Test if two
double[]
s are equal.- Parameters:
fieldName
- the field namelhs
- the left handdouble[]
rhs
- the right handdouble[]
- Returns:
- this
- Throws:
java.lang.IllegalArgumentException
- if field name isnull
-
append
public DiffBuilder append(java.lang.String fieldName, float lhs, float rhs)
Test if two
float
s are equal.- Parameters:
fieldName
- the field namelhs
- the left handfloat
rhs
- the right handfloat
- Returns:
- this
- Throws:
java.lang.IllegalArgumentException
- if field name isnull
-
append
public DiffBuilder append(java.lang.String fieldName, float[] lhs, float[] rhs)
Test if two
float[]
s are equal.- Parameters:
fieldName
- the field namelhs
- the left handfloat[]
rhs
- the right handfloat[]
- Returns:
- this
- Throws:
java.lang.IllegalArgumentException
- if field name isnull
-
append
public DiffBuilder append(java.lang.String fieldName, int lhs, int rhs)
Test if two
int
s are equal.- Parameters:
fieldName
- the field namelhs
- the left handint
rhs
- the right handint
- Returns:
- this
- Throws:
java.lang.IllegalArgumentException
- if field name isnull
-
append
public DiffBuilder append(java.lang.String fieldName, int[] lhs, int[] rhs)
Test if two
int[]
s are equal.- Parameters:
fieldName
- the field namelhs
- the left handint[]
rhs
- the right handint[]
- Returns:
- this
- Throws:
java.lang.IllegalArgumentException
- if field name isnull
-
append
public DiffBuilder append(java.lang.String fieldName, long lhs, long rhs)
Test if two
long
s are equal.- Parameters:
fieldName
- the field namelhs
- the left handlong
rhs
- the right handlong
- Returns:
- this
- Throws:
java.lang.IllegalArgumentException
- if field name isnull
-
append
public DiffBuilder append(java.lang.String fieldName, long[] lhs, long[] rhs)
Test if two
long[]
s are equal.- Parameters:
fieldName
- the field namelhs
- the left handlong[]
rhs
- the right handlong[]
- Returns:
- this
- Throws:
java.lang.IllegalArgumentException
- if field name isnull
-
append
public DiffBuilder append(java.lang.String fieldName, short lhs, short rhs)
Test if two
short
s are equal.- Parameters:
fieldName
- the field namelhs
- the left handshort
rhs
- the right handshort
- Returns:
- this
- Throws:
java.lang.IllegalArgumentException
- if field name isnull
-
append
public DiffBuilder append(java.lang.String fieldName, short[] lhs, short[] rhs)
Test if two
short[]
s are equal.- Parameters:
fieldName
- the field namelhs
- the left handshort[]
rhs
- the right handshort[]
- Returns:
- this
- Throws:
java.lang.IllegalArgumentException
- if field name isnull
-
append
public DiffBuilder append(java.lang.String fieldName, java.lang.Object lhs, java.lang.Object rhs)
Test if two
Objects
s are equal.- Parameters:
fieldName
- the field namelhs
- the left handObject
rhs
- the right handObject
- Returns:
- this
- Throws:
java.lang.IllegalArgumentException
- if field name isnull
-
append
public DiffBuilder append(java.lang.String fieldName, java.lang.Object[] lhs, java.lang.Object[] rhs)
Test if two
Object[]
s are equal.- Parameters:
fieldName
- the field namelhs
- the left handObject[]
rhs
- the right handObject[]
- Returns:
- this
- Throws:
java.lang.IllegalArgumentException
- if field name isnull
-
append
public DiffBuilder append(java.lang.String fieldName, DiffResult diffResult)
Append diffs from another
DiffResult
.This method is useful if you want to compare properties which are themselves Diffable and would like to know which specific part of it is different.
public class Person implements Diffable<Person> { String name; Address address; // implements Diffable<Address> ... public DiffResult diff(Person obj) { return new DiffBuilder(this, obj, ToStringStyle.SHORT_PREFIX_STYLE) .append("name", this.name, obj.name) .append("address", this.address.diff(obj.address)) .build(); } }
- Parameters:
fieldName
- the field namediffResult
- theDiffResult
to append- Returns:
- this
- Throws:
java.lang.IllegalArgumentException
- if field name isnull
- Since:
- 3.5
-
build
public DiffResult build()
Builds a
DiffResult
based on the differences appended to this builder.- Specified by:
build
in interfaceBuilder<DiffResult>
- Returns:
- a
DiffResult
containing the differences between the two objects.
-
-