T - type of the left and right object.public class DiffBuilder<T> extends Object implements Builder<DiffResult<T>>
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<Person> diff(Person obj) {
// No need for null check, as NullPointerException correct if obj is null
return DiffBuilder.<Person>builder()
.setLeft(this)
.setRight(obj)
.setStyle(ToStringStyle.SHORT_PREFIX_STYLE)
.build()
.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).
See ReflectionDiffBuilder for a reflection based version of this class.
Diffable,
Diff,
DiffResult,
ToStringStyle,
ReflectionDiffBuilder| Modifier and Type | Class and Description |
|---|---|
static class |
DiffBuilder.Builder<T>
Constructs a new instance.
|
| Constructor and Description |
|---|
DiffBuilder(T left,
T right,
ToStringStyle style)
Deprecated.
Use
DiffBuilder.Builder. |
DiffBuilder(T left,
T right,
ToStringStyle style,
boolean testObjectsEquals)
Deprecated.
Use
DiffBuilder.Builder. |
| Modifier and Type | Method and Description |
|---|---|
DiffBuilder<T> |
append(String fieldName,
boolean[] lhs,
boolean[] rhs)
Tests if two
boolean[]s are equal. |
DiffBuilder<T> |
append(String fieldName,
boolean lhs,
boolean rhs)
Tests if two
booleans are equal. |
DiffBuilder<T> |
append(String fieldName,
byte[] lhs,
byte[] rhs)
Tests if two
byte[]s are equal. |
DiffBuilder<T> |
append(String fieldName,
byte lhs,
byte rhs)
Tests if two
bytes are equal. |
DiffBuilder<T> |
append(String fieldName,
char[] lhs,
char[] rhs)
Tests if two
char[]s are equal. |
DiffBuilder<T> |
append(String fieldName,
char lhs,
char rhs)
Tests if two
chars are equal. |
DiffBuilder<T> |
append(String fieldName,
DiffResult<?> diffResult)
Appends diffs from another
DiffResult. |
DiffBuilder<T> |
append(String fieldName,
double[] lhs,
double[] rhs)
Tests if two
double[]s are equal. |
DiffBuilder<T> |
append(String fieldName,
double lhs,
double rhs)
Tests if two
doubles are equal. |
DiffBuilder<T> |
append(String fieldName,
float[] lhs,
float[] rhs)
Tests if two
float[]s are equal. |
DiffBuilder<T> |
append(String fieldName,
float lhs,
float rhs)
Test if two
floats are equal. |
DiffBuilder<T> |
append(String fieldName,
int[] lhs,
int[] rhs)
Tests if two
int[]s are equal. |
DiffBuilder<T> |
append(String fieldName,
int lhs,
int rhs)
Tests if two
ints are equal. |
DiffBuilder<T> |
append(String fieldName,
long[] lhs,
long[] rhs)
Tests if two
long[]s are equal. |
DiffBuilder<T> |
append(String fieldName,
long lhs,
long rhs)
Tests if two
longs are equal. |
DiffBuilder<T> |
append(String fieldName,
Object[] lhs,
Object[] rhs)
Tests if two
Object[]s are equal. |
DiffBuilder<T> |
append(String fieldName,
Object lhs,
Object rhs)
Tests if two
Objectss are equal. |
DiffBuilder<T> |
append(String fieldName,
short[] lhs,
short[] rhs)
Tests if two
short[]s are equal. |
DiffBuilder<T> |
append(String fieldName,
short lhs,
short rhs)
Tests if two
shorts are equal. |
DiffResult<T> |
build()
Builds a
DiffResult based on the differences appended to this builder. |
static <T> DiffBuilder.Builder<T> |
builder()
Constructs a new
DiffBuilder.Builder. |
@Deprecated public DiffBuilder(T left, T right, ToStringStyle style)
DiffBuilder.Builder.
If lhs == rhs or lhs.equals(rhs) then the builder will not evaluate any calls to append(...) and will return an empty
DiffResult when build() is executed.
This delegates to DiffBuilder(Object, Object, ToStringStyle, boolean) with the testTriviallyEqual flag enabled.
left - this objectright - the object to diff againststyle - the style to use when outputting the objects, null uses the defaultNullPointerException - if lhs or rhs is null@Deprecated public DiffBuilder(T left, T right, ToStringStyle style, boolean testObjectsEquals)
DiffBuilder.Builder.
If lhs == rhs or lhs.equals(rhs) then the builder will not evaluate any calls to append(...) and will return an empty
DiffResult when build() is executed.
left - this objectright - the object to diff againststyle - the style to use when outputting the objects, null uses the defaulttestObjectsEquals - 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 field Diff if the trivially equal test is enabled and returns true. The result of this test is never changed
throughout the life of this DiffBuilder.NullPointerException - if lhs or rhs is nullpublic static <T> DiffBuilder.Builder<T> builder()
DiffBuilder.Builder.T - type of the left and right object.DiffBuilder.Builder.public DiffBuilder<T> append(String fieldName, boolean lhs, boolean rhs)
booleans are equal.fieldName - the field namelhs - the left-hand side booleanrhs - the right-hand side booleanthis instance.NullPointerException - if field name is nullpublic DiffBuilder<T> append(String fieldName, boolean[] lhs, boolean[] rhs)
boolean[]s are equal.fieldName - the field namelhs - the left-hand side boolean[]rhs - the right-hand side boolean[]this instance.NullPointerException - if field name is nullpublic DiffBuilder<T> append(String fieldName, byte lhs, byte rhs)
bytes are equal.fieldName - the field namelhs - the left-hand side byterhs - the right-hand side bytethis instance.NullPointerException - if field name is nullpublic DiffBuilder<T> append(String fieldName, byte[] lhs, byte[] rhs)
byte[]s are equal.fieldName - the field namelhs - the left-hand side byte[]rhs - the right-hand side byte[]this instance.NullPointerException - if field name is nullpublic DiffBuilder<T> append(String fieldName, char lhs, char rhs)
chars are equal.fieldName - the field namelhs - the left-hand side charrhs - the right-hand side charthis instance.NullPointerException - if field name is nullpublic DiffBuilder<T> append(String fieldName, char[] lhs, char[] rhs)
char[]s are equal.fieldName - the field namelhs - the left-hand side char[]rhs - the right-hand side char[]this instance.NullPointerException - if field name is nullpublic DiffBuilder<T> append(String fieldName, DiffResult<?> diffResult)
DiffResult.
Useful this method 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();
}
}
fieldName - the field namediffResult - the DiffResult to appendthis instance.NullPointerException - if field name is null or diffResult is nullpublic DiffBuilder<T> append(String fieldName, double lhs, double rhs)
doubles are equal.fieldName - the field namelhs - the left-hand side doublerhs - the right-hand side doublethis instance.NullPointerException - if field name is nullpublic DiffBuilder<T> append(String fieldName, double[] lhs, double[] rhs)
double[]s are equal.fieldName - the field namelhs - the left-hand side double[]rhs - the right-hand side double[]this instance.NullPointerException - if field name is nullpublic DiffBuilder<T> append(String fieldName, float lhs, float rhs)
floats are equal.fieldName - the field namelhs - the left-hand side floatrhs - the right-hand side floatthis instance.NullPointerException - if field name is nullpublic DiffBuilder<T> append(String fieldName, float[] lhs, float[] rhs)
float[]s are equal.fieldName - the field namelhs - the left-hand side float[]rhs - the right-hand side float[]this instance.NullPointerException - if field name is nullpublic DiffBuilder<T> append(String fieldName, int lhs, int rhs)
ints are equal.fieldName - the field namelhs - the left-hand side intrhs - the right-hand side intthis instance.NullPointerException - if field name is nullpublic DiffBuilder<T> append(String fieldName, int[] lhs, int[] rhs)
int[]s are equal.fieldName - the field namelhs - the left-hand side int[]rhs - the right-hand side int[]this instance.NullPointerException - if field name is nullpublic DiffBuilder<T> append(String fieldName, long lhs, long rhs)
longs are equal.fieldName - the field namelhs - the left-hand side longrhs - the right-hand side longthis instance.NullPointerException - if field name is nullpublic DiffBuilder<T> append(String fieldName, long[] lhs, long[] rhs)
long[]s are equal.fieldName - the field namelhs - the left-hand side long[]rhs - the right-hand side long[]this instance.NullPointerException - if field name is nullpublic DiffBuilder<T> append(String fieldName, Object lhs, Object rhs)
Objectss are equal.fieldName - the field namelhs - the left-hand side Objectrhs - the right-hand side Objectthis instance.NullPointerException - if field name is nullpublic DiffBuilder<T> append(String fieldName, Object[] lhs, Object[] rhs)
Object[]s are equal.fieldName - the field namelhs - the left-hand side Object[]rhs - the right-hand side Object[]this instance.NullPointerException - if field name is nullpublic DiffBuilder<T> append(String fieldName, short lhs, short rhs)
shorts are equal.fieldName - the field namelhs - the left-hand side shortrhs - the right-hand side shortthis instance.NullPointerException - if field name is nullpublic DiffBuilder<T> append(String fieldName, short[] lhs, short[] rhs)
short[]s are equal.fieldName - the field namelhs - the left-hand side short[]rhs - the right-hand side short[]this instance.NullPointerException - if field name is nullpublic DiffResult<T> build()
DiffResult based on the differences appended to this builder.build in interface Builder<DiffResult<T>>DiffResult containing the differences between the two objects.Copyright © 2001–2025 The Apache Software Foundation. All rights reserved.