DiffBuilder
- public class org.apache.commons.lang3.builder.DiffBuilder<T> implements Builder<DiffResult<T>>[source]
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<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
ToStringStylepassed to the constructor is embedded in the returnedDiffResultand influences the style of theDiffResult.toString()method. This style choice can be overridden by callingDiffResult#toString(ToStringStyle).See
ReflectionDiffBuilderfor a reflection based version of this class.- Parameters:
<T> – type of the left and right object.
- See also:
- Since:
3.3
Constructors
Deprecated. Constructs a builder for the specified objects with the specified style. |
|
Deprecated. Constructs a builder for the specified objects with the specified style. |
- @Deprecated public DiffBuilder(T left, T right, ToStringStyle style)[source]
Deprecated: Use
Builder.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 emptyDiffResultwhen#build()is executed.This delegates to
#DiffBuilder(Object, Object, ToStringStyle, boolean)with the testTriviallyEqual flag enabled.- Parameters:
left –
thisobject.right – The object to diff against.
style – The style to use when outputting the objects,
nulluses the default.
- Throws:
java.lang.NullPointerException – Thrown if
lhsorrhsisnull.
- @Deprecated public DiffBuilder(T left, T right, ToStringStyle style, boolean testObjectsEquals)[source]
Deprecated: Use
Builder.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 emptyDiffResultwhen#build()is executed.- Parameters:
left –
thisobject.right – The object to diff against.
style – The style to use when outputting the objects,
nulluses the default.testObjectsEquals – 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
Diffif 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.NullPointerException – Thrown if
lhsorrhsisnull.- Since:
3.4
Methods
Tests if two |
|
Tests if two |
|
Tests if two |
|
Tests if two |
|
Tests if two |
|
Tests if two |
|
Tests if two |
|
Tests if two |
|
Test if two |
|
Tests if two |
|
Tests if two |
|
Tests if two |
|
Tests if two |
|
Tests if two |
|
Tests if two |
|
Tests if two |
|
Appends diffs from another |
|
Tests if two |
|
Tests if two |
|
Builds a |
|
Constructs a new |
- public DiffBuilder<T> append(String fieldName, boolean lhs, boolean rhs)[source]
Tests if two
booleans are equal.- Parameters:
fieldName – The field name.
lhs – The left-hand side
boolean.rhs – The right-hand side
boolean.
- Returns:
thisinstance.- Throws:
java.lang.NullPointerException – Thrown if field name is
null.
- public DiffBuilder<T> append(String fieldName, boolean[] lhs, boolean[] rhs)[source]
Tests if two
boolean[]s are equal.- Parameters:
fieldName – The field name.
lhs – The left-hand side
boolean[].rhs – The right-hand side
boolean[].
- Returns:
thisinstance.- Throws:
java.lang.NullPointerException – Thrown if field name is
null.
- public DiffBuilder<T> append(String fieldName, byte lhs, byte rhs)[source]
Tests if two
bytes are equal.- Parameters:
fieldName – The field name.
lhs – The left-hand side
byte.rhs – The right-hand side
byte.
- Returns:
thisinstance.- Throws:
java.lang.NullPointerException – Thrown if field name is
null.
- public DiffBuilder<T> append(String fieldName, byte[] lhs, byte[] rhs)[source]
Tests if two
byte[]s are equal.- Parameters:
fieldName – The field name.
lhs – The left-hand side
byte[].rhs – The right-hand side
byte[].
- Returns:
thisinstance.- Throws:
java.lang.NullPointerException – Thrown if field name is
null.
- public DiffBuilder<T> append(String fieldName, char lhs, char rhs)[source]
Tests if two
chars are equal.- Parameters:
fieldName – The field name.
lhs – The left-hand side
char.rhs – The right-hand side
char.
- Returns:
thisinstance.- Throws:
java.lang.NullPointerException – Thrown if field name is
null.
- public DiffBuilder<T> append(String fieldName, char[] lhs, char[] rhs)[source]
Tests if two
char[]s are equal.- Parameters:
fieldName – The field name.
lhs – The left-hand side
char[].rhs – The right-hand side
char[].
- Returns:
thisinstance.- Throws:
java.lang.NullPointerException – Thrown if field name is
null.
- public DiffBuilder<T> append(String fieldName, double lhs, double rhs)[source]
Tests if two
doubles are equal.- Parameters:
fieldName – The field name.
lhs – The left-hand side
double.rhs – The right-hand side
double.
- Returns:
thisinstance.- Throws:
java.lang.NullPointerException – Thrown if field name is
null.
- public DiffBuilder<T> append(String fieldName, double[] lhs, double[] rhs)[source]
Tests if two
double[]s are equal.- Parameters:
fieldName – The field name.
lhs – The left-hand side
double[].rhs – The right-hand side
double[].
- Returns:
thisinstance.- Throws:
java.lang.NullPointerException – Thrown if field name is
null.
- public DiffBuilder<T> append(String fieldName, float lhs, float rhs)[source]
Test if two
floats are equal.- Parameters:
fieldName – The field name.
lhs – The left-hand side
float.rhs – The right-hand side
float.
- Returns:
thisinstance.- Throws:
java.lang.NullPointerException – Thrown if field name is
null.
- public DiffBuilder<T> append(String fieldName, float[] lhs, float[] rhs)[source]
Tests if two
float[]s are equal.- Parameters:
fieldName – The field name.
lhs – The left-hand side
float[].rhs – The right-hand side
float[].
- Returns:
thisinstance.- Throws:
java.lang.NullPointerException – Thrown if field name is
null.
- public DiffBuilder<T> append(String fieldName, int lhs, int rhs)[source]
Tests if two
ints are equal.- Parameters:
fieldName – The field name.
lhs – The left-hand side
int.rhs – The right-hand side
int.
- Returns:
thisinstance.- Throws:
java.lang.NullPointerException – Thrown if field name is
null.
- public DiffBuilder<T> append(String fieldName, int[] lhs, int[] rhs)[source]
Tests if two
int[]s are equal.- Parameters:
fieldName – The field name.
lhs – The left-hand side
int[].rhs – The right-hand side
int[].
- Returns:
thisinstance.- Throws:
java.lang.NullPointerException – Thrown if field name is
null.
- public DiffBuilder<T> append(String fieldName, Object lhs, Object rhs)[source]
Tests if two
Objectss are equal.
- public DiffBuilder<T> append(String fieldName, Object[] lhs, Object[] rhs)[source]
Tests if two
Object[]s are equal.- Parameters:
fieldName – The field name.
lhs – The left-hand side
Object[].rhs – The right-hand side
Object[].
- Returns:
thisinstance.- Throws:
java.lang.NullPointerException – Thrown if field name is
null.
- public DiffBuilder<T> append(String fieldName, long lhs, long rhs)[source]
Tests if two
longs are equal.- Parameters:
fieldName – The field name.
lhs – The left-hand side
long.rhs – The right-hand side
long.
- Returns:
thisinstance.- Throws:
java.lang.NullPointerException – Thrown if field name is
null.
- public DiffBuilder<T> append(String fieldName, long[] lhs, long[] rhs)[source]
Tests if two
long[]s are equal.- Parameters:
fieldName – The field name.
lhs – The left-hand side
long[].rhs – The right-hand side
long[].
- Returns:
thisinstance.- Throws:
java.lang.NullPointerException – Thrown if field name is
null.
- public DiffBuilder<T> append(String fieldName, DiffResult<?> diffResult)[source]
Appends diffs from another
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(); } }
- Parameters:
fieldName – The field name.
diffResult – The
DiffResultto append.
- Returns:
thisinstance.- Throws:
java.lang.NullPointerException – Thrown if field name is
nullor diffResult isnull.- Since:
3.5
- public DiffBuilder<T> append(String fieldName, short lhs, short rhs)[source]
Tests if two
shorts are equal.- Parameters:
fieldName – The field name.
lhs – The left-hand side
short.rhs – The right-hand side
short.
- Returns:
thisinstance.- Throws:
java.lang.NullPointerException – Thrown if field name is
null.
- public DiffBuilder<T> append(String fieldName, short[] lhs, short[] rhs)[source]
Tests if two
short[]s are equal.- Parameters:
fieldName – The field name.
lhs – The left-hand side
short[].rhs – The right-hand side
short[].
- Returns:
thisinstance.- Throws:
java.lang.NullPointerException – Thrown if field name is
null.
- @Override public DiffResult<T> build()[source]
Builds a
DiffResultbased on the differences appended to this builder.- Returns:
A
DiffResultcontaining the differences between the two objects.
- public static <T> DiffBuilder.Builder<T> builder()[source]
Constructs a new
Builder.- Type parameters:
<T> – type of the left and right object.
- Returns:
A new
Builder.- Since:
3.15.0