Class DiffBuilder<T>
- java.lang.Object
-
- org.apache.commons.lang3.builder.DiffBuilder<T>
-
- Type Parameters:
T- type of the left and right object.
- All Implemented Interfaces:
Builder<DiffResult<T>>
public class DiffBuilder<T> extends java.lang.Object implements Builder<DiffResult<T>>
Assists in implementingDiffable.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.- Since:
- 3.3
- See Also:
Diffable,Diff,DiffResult,ToStringStyle,ReflectionDiffBuilder
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDiffBuilder.Builder<T>Constructs a new instance.
-
Constructor Summary
Constructors Constructor Description DiffBuilder(T left, T right, ToStringStyle style)Deprecated.UseDiffBuilder.Builder.DiffBuilder(T left, T right, ToStringStyle style, boolean testObjectsEquals)Deprecated.UseDiffBuilder.Builder.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description DiffBuilder<T>append(java.lang.String fieldName, boolean[] lhs, boolean[] rhs)Tests if twoboolean[]s are equal.DiffBuilder<T>append(java.lang.String fieldName, boolean lhs, boolean rhs)Tests if twobooleans are equal.DiffBuilder<T>append(java.lang.String fieldName, byte[] lhs, byte[] rhs)Tests if twobyte[]s are equal.DiffBuilder<T>append(java.lang.String fieldName, byte lhs, byte rhs)Tests if twobytes are equal.DiffBuilder<T>append(java.lang.String fieldName, char[] lhs, char[] rhs)Tests if twochar[]s are equal.DiffBuilder<T>append(java.lang.String fieldName, char lhs, char rhs)Tests if twochars are equal.DiffBuilder<T>append(java.lang.String fieldName, double[] lhs, double[] rhs)Tests if twodouble[]s are equal.DiffBuilder<T>append(java.lang.String fieldName, double lhs, double rhs)Tests if twodoubles are equal.DiffBuilder<T>append(java.lang.String fieldName, float[] lhs, float[] rhs)Tests if twofloat[]s are equal.DiffBuilder<T>append(java.lang.String fieldName, float lhs, float rhs)Test if twofloats are equal.DiffBuilder<T>append(java.lang.String fieldName, int[] lhs, int[] rhs)Tests if twoint[]s are equal.DiffBuilder<T>append(java.lang.String fieldName, int lhs, int rhs)Tests if twoints are equal.DiffBuilder<T>append(java.lang.String fieldName, long[] lhs, long[] rhs)Tests if twolong[]s are equal.DiffBuilder<T>append(java.lang.String fieldName, long lhs, long rhs)Tests if twolongs are equal.DiffBuilder<T>append(java.lang.String fieldName, short[] lhs, short[] rhs)Tests if twoshort[]s are equal.DiffBuilder<T>append(java.lang.String fieldName, short lhs, short rhs)Tests if twoshorts are equal.DiffBuilder<T>append(java.lang.String fieldName, java.lang.Object[] lhs, java.lang.Object[] rhs)Tests if twoObject[]s are equal.DiffBuilder<T>append(java.lang.String fieldName, java.lang.Object lhs, java.lang.Object rhs)Tests if twoObjectss are equal.DiffBuilder<T>append(java.lang.String fieldName, DiffResult<?> diffResult)Appends diffs from anotherDiffResult.DiffResult<T>build()Builds aDiffResultbased on the differences appended to this builder.static <T> DiffBuilder.Builder<T>builder()Constructs a newDiffBuilder.Builder.
-
-
-
Constructor Detail
-
DiffBuilder
@Deprecated public DiffBuilder(T left, T right, ToStringStyle style)
Deprecated.UseDiffBuilder.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 emptyDiffResultwhenbuild()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- iflhsorrhsisnull.
-
DiffBuilder
@Deprecated public DiffBuilder(T left, T right, ToStringStyle style, boolean testObjectsEquals)
Deprecated.UseDiffBuilder.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 emptyDiffResultwhenbuild()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 fieldDiffif 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- iflhsorrhsisnull.- Since:
- 3.4
-
-
Method Detail
-
builder
public static <T> DiffBuilder.Builder<T> builder()
Constructs a newDiffBuilder.Builder.- Type Parameters:
T- type of the left and right object.- Returns:
- a new
DiffBuilder.Builder. - Since:
- 3.15.0
-
append
public DiffBuilder<T> append(java.lang.String fieldName, boolean lhs, boolean rhs)
Tests if twobooleans are equal.- Parameters:
fieldName- the field name.lhs- the left-hand sideboolean.rhs- the right-hand sideboolean.- Returns:
thisinstance.- Throws:
java.lang.NullPointerException- if field name isnull.
-
append
public DiffBuilder<T> append(java.lang.String fieldName, boolean[] lhs, boolean[] rhs)
Tests if twoboolean[]s are equal.- Parameters:
fieldName- the field name.lhs- the left-hand sideboolean[].rhs- the right-hand sideboolean[].- Returns:
thisinstance.- Throws:
java.lang.NullPointerException- if field name isnull.
-
append
public DiffBuilder<T> append(java.lang.String fieldName, byte lhs, byte rhs)
Tests if twobytes are equal.- Parameters:
fieldName- the field name.lhs- the left-hand sidebyte.rhs- the right-hand sidebyte.- Returns:
thisinstance.- Throws:
java.lang.NullPointerException- if field name isnull.
-
append
public DiffBuilder<T> append(java.lang.String fieldName, byte[] lhs, byte[] rhs)
Tests if twobyte[]s are equal.- Parameters:
fieldName- the field name.lhs- the left-hand sidebyte[].rhs- the right-hand sidebyte[].- Returns:
thisinstance.- Throws:
java.lang.NullPointerException- if field name isnull.
-
append
public DiffBuilder<T> append(java.lang.String fieldName, char lhs, char rhs)
Tests if twochars are equal.- Parameters:
fieldName- the field name.lhs- the left-hand sidechar.rhs- the right-hand sidechar.- Returns:
thisinstance.- Throws:
java.lang.NullPointerException- if field name isnull.
-
append
public DiffBuilder<T> append(java.lang.String fieldName, char[] lhs, char[] rhs)
Tests if twochar[]s are equal.- Parameters:
fieldName- the field name.lhs- the left-hand sidechar[].rhs- the right-hand sidechar[].- Returns:
thisinstance.- Throws:
java.lang.NullPointerException- if field name isnull.
-
append
public DiffBuilder<T> append(java.lang.String fieldName, DiffResult<?> diffResult)
Appends diffs from anotherDiffResult.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- theDiffResultto append.- Returns:
thisinstance.- Throws:
java.lang.NullPointerException- if field name isnullor diffResult isnull.- Since:
- 3.5
-
append
public DiffBuilder<T> append(java.lang.String fieldName, double lhs, double rhs)
Tests if twodoubles are equal.- Parameters:
fieldName- the field name.lhs- the left-hand sidedouble.rhs- the right-hand sidedouble.- Returns:
thisinstance.- Throws:
java.lang.NullPointerException- if field name isnull.
-
append
public DiffBuilder<T> append(java.lang.String fieldName, double[] lhs, double[] rhs)
Tests if twodouble[]s are equal.- Parameters:
fieldName- the field name.lhs- the left-hand sidedouble[].rhs- the right-hand sidedouble[].- Returns:
thisinstance.- Throws:
java.lang.NullPointerException- if field name isnull.
-
append
public DiffBuilder<T> append(java.lang.String fieldName, float lhs, float rhs)
Test if twofloats are equal.- Parameters:
fieldName- the field name.lhs- the left-hand sidefloat.rhs- the right-hand sidefloat.- Returns:
thisinstance.- Throws:
java.lang.NullPointerException- if field name isnull.
-
append
public DiffBuilder<T> append(java.lang.String fieldName, float[] lhs, float[] rhs)
Tests if twofloat[]s are equal.- Parameters:
fieldName- the field name.lhs- the left-hand sidefloat[].rhs- the right-hand sidefloat[].- Returns:
thisinstance.- Throws:
java.lang.NullPointerException- if field name isnull.
-
append
public DiffBuilder<T> append(java.lang.String fieldName, int lhs, int rhs)
Tests if twoints are equal.- Parameters:
fieldName- the field name.lhs- the left-hand sideint.rhs- the right-hand sideint.- Returns:
thisinstance.- Throws:
java.lang.NullPointerException- if field name isnull.
-
append
public DiffBuilder<T> append(java.lang.String fieldName, int[] lhs, int[] rhs)
Tests if twoint[]s are equal.- Parameters:
fieldName- the field name.lhs- the left-hand sideint[].rhs- the right-hand sideint[].- Returns:
thisinstance.- Throws:
java.lang.NullPointerException- if field name isnull.
-
append
public DiffBuilder<T> append(java.lang.String fieldName, long lhs, long rhs)
Tests if twolongs are equal.- Parameters:
fieldName- the field name.lhs- the left-hand sidelong.rhs- the right-hand sidelong.- Returns:
thisinstance.- Throws:
java.lang.NullPointerException- if field name isnull.
-
append
public DiffBuilder<T> append(java.lang.String fieldName, long[] lhs, long[] rhs)
Tests if twolong[]s are equal.- Parameters:
fieldName- the field name.lhs- the left-hand sidelong[].rhs- the right-hand sidelong[].- Returns:
thisinstance.- Throws:
java.lang.NullPointerException- if field name isnull.
-
append
public DiffBuilder<T> append(java.lang.String fieldName, java.lang.Object lhs, java.lang.Object rhs)
Tests if twoObjectss are equal.- Parameters:
fieldName- the field name.lhs- the left-hand sideObject.rhs- the right-hand sideObject.- Returns:
thisinstance.- Throws:
java.lang.NullPointerException- if field name isnull.
-
append
public DiffBuilder<T> append(java.lang.String fieldName, java.lang.Object[] lhs, java.lang.Object[] rhs)
Tests if twoObject[]s are equal.- Parameters:
fieldName- the field name.lhs- the left-hand sideObject[].rhs- the right-hand sideObject[].- Returns:
thisinstance.- Throws:
java.lang.NullPointerException- if field name isnull.
-
append
public DiffBuilder<T> append(java.lang.String fieldName, short lhs, short rhs)
Tests if twoshorts are equal.- Parameters:
fieldName- the field name.lhs- the left-hand sideshort.rhs- the right-hand sideshort.- Returns:
thisinstance.- Throws:
java.lang.NullPointerException- if field name isnull.
-
append
public DiffBuilder<T> append(java.lang.String fieldName, short[] lhs, short[] rhs)
Tests if twoshort[]s are equal.- Parameters:
fieldName- the field name.lhs- the left-hand sideshort[].rhs- the right-hand sideshort[].- Returns:
thisinstance.- Throws:
java.lang.NullPointerException- if field name isnull.
-
build
public DiffResult<T> build()
Builds aDiffResultbased on the differences appended to this builder.- Specified by:
buildin interfaceBuilder<T>- Returns:
- a
DiffResultcontaining the differences between the two objects.
-
-