Class ReflectionDiffBuilder

java.lang.Object
org.apache.commons.lang3.builder.ReflectionDiffBuilder
All Implemented Interfaces:
Builder<DiffResult>

public class ReflectionDiffBuilder extends Object implements Builder<DiffResult>

Assists in implementing Diffable.diff(Object) methods.

All non-static, non-transient fields (including inherited fields) of the objects to diff are discovered using reflection and compared for differences.

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 ReflectionDiffBuilder(this, obj, ToStringStyle.SHORT_PREFIX_STYLE)
       .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.6
See Also:
  • Constructor Details

    • ReflectionDiffBuilder

      public ReflectionDiffBuilder(T lhs, T rhs, ToStringStyle style)

      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.

      Type Parameters:
      T - type of the objects to diff
      Parameters:
      lhs - this object
      rhs - the object to diff against
      style - the style will use when outputting the objects, null uses the default
      Throws:
      IllegalArgumentException - if lhs or rhs is null
  • Method Details

    • build

      public DiffResult build()
      Description copied from interface: Builder
      Returns a reference to the object being constructed or result being calculated by the builder.
      Specified by:
      build in interface Builder<DiffResult>
      Returns:
      the object constructed or result calculated by the builder.