You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Matt Benson (JIRA)" <ji...@apache.org> on 2012/09/24 22:38:08 UTC

[jira] [Comment Edited] (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:comment-tabpanel&focusedCommentId=13462087#comment-13462087 ] 

Matt Benson edited comment on LANG-637 at 9/25/12 7:38 AM:
-----------------------------------------------------------

Again, from the usage standpoint I just envisioned that if I were getting diffs I might for some reason want to know at a code level precisely _in what manner_ two objects differed.  You are of course correct that ultimately the retrieval mechanism could really only be object-based, though we could do some generics tricks in that regard.  What about something like:

{code}
public abstract class Diff<T> extends oacl.tuple.Pair<T, T> {
  private final Type type;
  private final String fieldName;

  protected Diff(String fieldName) {
    this.type = ObjectUtils.defaultIfNull(TypeUtils.getTypeArguments(getClass(), Diff.class).get(Diff.class.getTypeParameters()[0]), Object.class);
  }

  public final Type getType() {
    return type;
  }

  public final String getFieldName() {
    return fieldName;
  }
}
{code}

{code}
public class DiffBuilder implements Builder<List<Diff>> {
  private final List<Diff> diffs = new ArrayList<Diff>();

  public List<Diff> build() {
    return Collections.unmodifiableList(new ArrayList<Diff>(diffs));
  }

  public DiffBuilder append(final String fieldName, final boolean lhs, final boolean rhs) {
    if ( /* different */ ) {
      diffs.add(new Diff<Boolean>(fieldName) {
        public Boolean getLeft() {
          return Boolean.valueOf(lhs);
        }
        public Boolean getRight() {
          return Boolean.valueOf(rhs);
        }
      });
    }
    return this;
  }
}
{code}

The above example would avoid _storing_ your primitives as Objects, for whatever small additional storage costs that would have incurred.

$0.02,
Matt
                
      was (Author: mbenson):
    Again, from the usage standpoint I just envisioned that if I were getting diffs I might for some reason want to know at a code level precisely _in what manner_ two objects differed.  You are of course correct that ultimately the retrieval mechanism could really only be object-based, though we could do some generics tricks in that regard.  What about something like:

{code}
public abstract class Diff<T> extends oacl.tuple.Pair<T, T> {
  private final Type type;
  private final String fieldName;

  protected Diff(String fieldName) {
    this.type = ObjectUtils.defaultIfNull(TypeUtils.getTypeArguments(getClass(), Diff.class).get(Diff.class.getTypeParameters()[0]), Object.class);
  }

  public final Type getType() {
    return type;
  }

  public final String getFieldName() {
    return fieldName;
  }
}
{code}

{code}
public class DiffBuilder implements Builder<List<Diff>> {
  private final List<Diff> diffs = new ArrayList<Diff>();

  public List<Diff> build() {
    return Collections.unmodifiableList(new ArrayList<Diff>(diffs));
  }

  public DiffBuilder append(final String fieldName, final boolean lhs, final boolean rhs) {
    if ( /* different */ ) {
      diffs.add(new Diff<Boolean>(fieldName) {
        public Boolean getLeft() {
          return Boolean.valueOf(lhs);
        }
        public Boolean getRight() {
          return Boolean.valueOf(rhs);
        }
      });
    }
  }
}
{code}

The above example would avoid _storing_ your primitives as Objects, for whatever small additional storage costs that would have incurred.

$0.02,
Matt
                  
> 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