You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Duncan Jones (JIRA)" <ji...@apache.org> on 2012/09/23 20:37:07 UTC

[jira] [Updated] (LANG-637) There should be a DifferenceBuilder with a ReflectionDifferenceBuilder implementation

     [ https://issues.apache.org/jira/browse/LANG-637?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Duncan Jones updated LANG-637:
------------------------------

    Attachment: DifferenceBuilderTest.java
                Differentiable.java
                DifferenceBuilder.java

I've begun work on an implementation of this feature, attached are some partial files. I would love some thoughts on whether the approach is sensible. Note that a reflection based builder would be required, but is not yet sketched out.

I have defined a new interface, {{Differentiable<T>}}, that is similar to {{Comparable<T>}}. The {{DifferenceBuilder}} will test user-chosen fields for equality (much like {{EqualsBuilder}}) and will append any differing fields an internal {{ToStringBuilder}} (one for each object). The {{build()}} method will either return an empty string if the tested fields were equal, or a string of the form:

{noformat}[LHS Object] differs from [RHS Object]{noformat}

(where the objects are printed using the {{ToStringBuilder}} and a user-chosen style).

Expected usage is as follows (taken from JavaDoc):

{code}
public class Person implements Differentiable<Person> {
  String name;
  int age;
  boolean smoker;
  
  ...
  
  public String differentiateFrom(Person obj) {
    // No need for null check, as NullPointerException correct if obj is null
    return new DifferenceBuilder(this, obj, ToStringStyle.SHORT_PREFIX_STYLE)
      .append("name", this.name, obj.name)
      .append("age", this.age, obj.age)
      .append("smoker", this.smoker, obj.smoker)
      .build();
  }
}
{code}

If the design appeals, I'll continue with the leg-work and add all the necessary method overloads and additional constructors etc. I can then post an update for a more detailed review.
                
> There should be a DifferenceBuilder with a ReflectionDifferenceBuilder implementation
> -------------------------------------------------------------------------------------
>
>                 Key: LANG-637
>                 URL: https://issues.apache.org/jira/browse/LANG-637
>             Project: Commons Lang
>          Issue Type: Improvement
>          Components: lang.builder.*
>            Reporter: Eric Lewis
>            Priority: Minor
>             Fix For: 3.x
>
>         Attachments: DifferenceBuilder.java, DifferenceBuilderTest.java, Differentiable.java
>
>
> The ToStringBuilder and ReflectionToStringBuilder are great tools for everyday development.
> We use them to show all the properties in an object, which comes handy especially for testing.
> However, JUnit with its assertEquals() just outputs the toString() of the two compared objects. For complex objects, this becomes unreadable.
> So, it would be great to have a DifferenceBuilder with a ReflectionDifferenceBuilder implementation to be able to get only the differing properties of two objects. The question is whether the two objects would have to be of the same type or not.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira