Class DiffBuilder<T>
- Type Parameters:
T- type of the left and right object.
- All Implemented Interfaces:
Builder<DiffResult<T>>
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
returned DiffResult and influences the style of the
DiffResult.toString() method. This style choice can be overridden by
calling DiffResult.toString(ToStringStyle).
- Since:
- 3.3
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Tprivate final booleanprivate final Tprivate final ToStringStyle -
Constructor Summary
ConstructorsConstructorDescriptionDiffBuilder(T lhs, T rhs, ToStringStyle style) Constructs a builder for the specified objects with the specified style.DiffBuilder(T lhs, T rhs, ToStringStyle style, boolean testTriviallyEqual) Constructs a builder for the specified objects with the specified style. -
Method Summary
Modifier and TypeMethodDescriptionTest if twoboolean[]s are equal.Test if twobooleans are equal.Test if twobyte[]s are equal.Test if twobytes are equal.Test if twochar[]s are equal.Test if twochars are equal.Test if twodouble[]s are equal.Test if twodoubles are equal.Test if twofloat[]s are equal.Test if twofloats are equal.Test if twoint[]s are equal.Test if twoints are equal.Test if twolong[]s are equal.Test if twolongs are equal.Test if twoshort[]s are equal.Test if twoshorts are equal.Test if twoObject[]s are equal.Test if twoObjectss are equal.append(String fieldName, DiffResult<T> diffResult) Append diffs from anotherDiffResult.build()Builds aDiffResultbased on the differences appended to this builder.private voidvalidateFieldNameNotNull(String fieldName)
-
Field Details
-
diffs
-
objectsTriviallyEqual
private final boolean objectsTriviallyEqual -
left
-
right
-
style
-
-
Constructor Details
-
DiffBuilder
Constructs a builder for the specified objects with the specified style.
If
lhs == rhsorlhs.equals(rhs)then the builder will not evaluate any calls toappend(...)and will return an emptyDiffResultwhenbuild()is executed.- Parameters:
lhs-thisobjectrhs- the object to diff againststyle- the style will use when outputting the objects,nulluses 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 fieldDiffif the trivially equal test is enabled and returns true. The result of this test is never changed throughout the life of thisDiffBuilder.- Throws:
IllegalArgumentException- iflhsorrhsisnull- Since:
- 3.4
-
DiffBuilder
Constructs a builder for the specified objects with the specified style.
If
lhs == rhsorlhs.equals(rhs)then the builder will not evaluate any calls toappend(...)and will return an emptyDiffResultwhenbuild()is executed.This delegates to
DiffBuilder(Object, Object, ToStringStyle, boolean)with the testTriviallyEqual flag enabled.- Parameters:
lhs-thisobjectrhs- the object to diff againststyle- the style will use when outputting the objects,nulluses the default- Throws:
IllegalArgumentException- iflhsorrhsisnull
-
-
Method Details
-
append
Test if two
booleans are equal.- Parameters:
fieldName- the field namelhs- the left handbooleanrhs- the right handboolean- Returns:
- this
- Throws:
IllegalArgumentException- if field name isnull
-
append
Test if two
boolean[]s are equal.- Parameters:
fieldName- the field namelhs- the left handboolean[]rhs- the right handboolean[]- Returns:
- this
- Throws:
IllegalArgumentException- if field name isnull
-
append
Test if two
bytes are equal.- Parameters:
fieldName- the field namelhs- the left handbyterhs- the right handbyte- Returns:
- this
- Throws:
IllegalArgumentException- if field name isnull
-
append
Test if two
byte[]s are equal.- Parameters:
fieldName- the field namelhs- the left handbyte[]rhs- the right handbyte[]- Returns:
- this
- Throws:
IllegalArgumentException- if field name isnull
-
append
Test if two
chars are equal.- Parameters:
fieldName- the field namelhs- the left handcharrhs- the right handchar- Returns:
- this
- Throws:
IllegalArgumentException- if field name isnull
-
append
Test if two
char[]s are equal.- Parameters:
fieldName- the field namelhs- the left handchar[]rhs- the right handchar[]- Returns:
- this
- Throws:
IllegalArgumentException- if field name isnull
-
append
Test if two
doubles are equal.- Parameters:
fieldName- the field namelhs- the left handdoublerhs- the right handdouble- Returns:
- this
- Throws:
IllegalArgumentException- if field name isnull
-
append
Test if two
double[]s are equal.- Parameters:
fieldName- the field namelhs- the left handdouble[]rhs- the right handdouble[]- Returns:
- this
- Throws:
IllegalArgumentException- if field name isnull
-
append
Test if two
floats are equal.- Parameters:
fieldName- the field namelhs- the left handfloatrhs- the right handfloat- Returns:
- this
- Throws:
IllegalArgumentException- if field name isnull
-
append
Test if two
float[]s are equal.- Parameters:
fieldName- the field namelhs- the left handfloat[]rhs- the right handfloat[]- Returns:
- this
- Throws:
IllegalArgumentException- if field name isnull
-
append
Test if two
ints are equal.- Parameters:
fieldName- the field namelhs- the left handintrhs- the right handint- Returns:
- this
- Throws:
IllegalArgumentException- if field name isnull
-
append
Test if two
int[]s are equal.- Parameters:
fieldName- the field namelhs- the left handint[]rhs- the right handint[]- Returns:
- this
- Throws:
IllegalArgumentException- if field name isnull
-
append
Test if two
longs are equal.- Parameters:
fieldName- the field namelhs- the left handlongrhs- the right handlong- Returns:
- this
- Throws:
IllegalArgumentException- if field name isnull
-
append
Test if two
long[]s are equal.- Parameters:
fieldName- the field namelhs- the left handlong[]rhs- the right handlong[]- Returns:
- this
- Throws:
IllegalArgumentException- if field name isnull
-
append
Test if two
shorts are equal.- Parameters:
fieldName- the field namelhs- the left handshortrhs- the right handshort- Returns:
- this
- Throws:
IllegalArgumentException- if field name isnull
-
append
Test if two
short[]s are equal.- Parameters:
fieldName- the field namelhs- the left handshort[]rhs- the right handshort[]- Returns:
- this
- Throws:
IllegalArgumentException- if field name isnull
-
append
Test if two
Objectss are equal.- Parameters:
fieldName- the field namelhs- the left handObjectrhs- the right handObject- Returns:
- this
- Throws:
IllegalArgumentException- if field name isnull
-
append
Test if two
Object[]s are equal.- Parameters:
fieldName- the field namelhs- the left handObject[]rhs- the right handObject[]- Returns:
- this
- Throws:
IllegalArgumentException- if field name isnull
-
append
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- theDiffResultto append- Returns:
- this
- Throws:
NullPointerException- if field name isnull- Since:
- 3.5
-
build
Builds a
DiffResultbased on the differences appended to this builder. -
validateFieldNameNotNull
-