On this page

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 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.

Parameters:

<T> – type of the left and right object.

See also:
Since:

3.3

Constructors

DiffBuilder(Object, Object, ToStringStyle)

Deprecated. Constructs a builder for the specified objects with the specified style.

DiffBuilder(Object, Object, ToStringStyle, boolean)

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 == 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.

Parameters:
  • left – this object.

  • right – The object to diff against.

  • style – The style to use when outputting the objects, null uses the default.

Throws:

java.lang.NullPointerException – Thrown if lhs or rhs is null.

@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 == 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.

Parameters:
  • left – this object.

  • right – The object to diff against.

  • style – The style to use when outputting the objects, null uses 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 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.

Throws:

java.lang.NullPointerException – Thrown if lhs or rhs is null.

Since:

3.4

Methods

append(String, boolean, boolean)

Tests if two booleans are equal.

append(String, boolean[], boolean[])

Tests if two boolean[]s are equal.

append(String, byte, byte)

Tests if two bytes are equal.

append(String, byte[], byte[])

Tests if two byte[]s are equal.

append(String, char, char)

Tests if two chars are equal.

append(String, char[], char[])

Tests if two char[]s are equal.

append(String, double, double)

Tests if two doubles are equal.

append(String, double[], double[])

Tests if two double[]s are equal.

append(String, float, float)

Test if two floats are equal.

append(String, float[], float[])

Tests if two float[]s are equal.

append(String, int, int)

Tests if two ints are equal.

append(String, int[], int[])

Tests if two int[]s are equal.

append(String, Object, Object)

Tests if two Objectss are equal.

append(String, Object[], Object[])

Tests if two Object[]s are equal.

append(String, long, long)

Tests if two longs are equal.

append(String, long[], long[])

Tests if two long[]s are equal.

append(String, DiffResult)

Appends diffs from another DiffResult.

append(String, short, short)

Tests if two shorts are equal.

append(String, short[], short[])

Tests if two short[]s are equal.

build()

Builds a DiffResult based on the differences appended to this builder.

builder()

Constructs a new Builder.

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:

this instance.

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:

this instance.

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:

this instance.

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:

this instance.

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:

this instance.

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:

this instance.

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:

this instance.

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:

this instance.

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:

this instance.

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:

this instance.

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:

this instance.

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:

this instance.

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.

Parameters:
  • fieldName – The field name.

  • lhs – The left-hand side Object.

  • rhs – The right-hand side Object.

Returns:

this instance.

Throws:

java.lang.NullPointerException – Thrown if field name is null.

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:

this instance.

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:

this instance.

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:

this instance.

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 DiffResult to append.

Returns:

this instance.

Throws:

java.lang.NullPointerException – Thrown if field name is null or diffResult is null.

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:

this instance.

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:

this instance.

Throws:

java.lang.NullPointerException – Thrown if field name is null.

@Override public DiffResult<T> build()[source]

Builds a DiffResult based on the differences appended to this builder.

Returns:

A DiffResult containing 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