You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Benedikt Ritter (JIRA)" <ji...@apache.org> on 2014/11/11 09:51:34 UTC

[jira] [Commented] (LANG-1062) Use Java 8 features to improve EqualsBuilder

    [ https://issues.apache.org/jira/browse/LANG-1062?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14206172#comment-14206172 ] 

Benedikt Ritter commented on LANG-1062:
---------------------------------------

Nice idea, but we won't switch to Java 8 before Lang 4.0, so this will have to wait some time :-(

> 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
>             Fix For: 4.0
>
>
> 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)