Bruce Brouwer created LANG-1062:
-----------------------------------

             Summary: Use Java 8 features to improve EqualsBuilder
                 Key: LANG-1062
                 URL: https://issues.apache.org/jira/browse/LANG-1062
             Project: Commons Lang
          Issue Type: Improvement
          Components: lang.builder.*
    Affects Versions: 3.3.2
            Reporter: Bruce Brouwer


Remove the need for boilerplate code still necessary with EqualsBuilder. 
Instead of checking for instanceOf or equality checks, it would be nice to be 
able to write this:
{code}
public boolean equals(final Object obj) {
  return EqualsBuilder.test(this, obj).append(this.id, o -> o.id).isEquals();
}
{code}

I think this could generally work with a subclass of EqualsBuilder with a 
generic type, like this:
{code}
public class LambdaEqualsBuilder<T> extends EqualsBuilder {
    private final T obj;

    public LambdaEqualsBuilder(final T _this, final Object obj) {
        if (_this != obj && !_this.getClass().isInstance(obj)) {
            appendSuper(false);
            this.obj = null;
        } else {
            this.obj = (T) obj;
        }
    }

    public <P> LambdaEqualsBuilder<T> append(final P a, final Function<T, P> b) 
{
        if (isEquals()) {
            return (LambdaEqualsBuilder<T>) append(a, b.apply(obj));
        }
        return this;
    }

    // This might actually go in EqualsBuilder itself
    public static <E> LambdaEqualsBuilder<E> test(final E _this, final Object 
_that) {
        return new LambdaEqualsBuilder<E>(_this, _that);
    }
}
{code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to