[
https://issues.apache.org/jira/browse/LANG-1062?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14598514#comment-14598514
]
Alan Green commented on LANG-1062:
----------------------------------
A couple of thoughts:
1. The attribute access functions could be applied to both objects in the
equality, so append only needs one argument:
{code}
return EqualsBuilder.test(this, obj).append(o -> o.id).isEquals();
{code}
2. It would make sense to provide a single function that accepts a collection
of attribute access functions:
{code}
return EqualsBuilder.test(this, obj).appendAll(Arrays.asList(o -> o.id, o ->
o.name, o -> o.color)).isEquals();
{code}
3. Since the common case is for hashCode and equality to work over the same
member attributes, they could share this list:
{code}
private static final List<Function<ThisClass, ?>> EQUAL_ATTRS = Arrays.asList(o
-> o.id, o -> o.name, o -> o.color);
public boolean equals(final Object obj) {
return EqualsBuilder.test(this, obj).appendAll(EQUAL_ATTRS).isEquals();
}
public int hashCode() {
return HashCodeBuilder.for(this).appendAll(EQUAL_ATTRS).build();
}
{code}
> 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)