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

[jira] Commented: (LANG-449) Implement equals, hashCode and toString replacement

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

Henri Yandell commented on LANG-449:
------------------------------------

Sounds very interesting. Something to look at for 3.0 I think.

Committership occurs as a function of an individual showing they are committed. Definitely helps to be writing new features like this in my opinion.

> Implement equals, hashCode and toString replacement
> ---------------------------------------------------
>
>                 Key: LANG-449
>                 URL: https://issues.apache.org/jira/browse/LANG-449
>             Project: Commons Lang
>          Issue Type: Improvement
>            Reporter: Vincenzo Vitale
>            Priority: Minor
>
> In my company (TomTom), we have internally developed a replacement of the equals, hashcode and toString methods.
> The base idea is to use the annotation @BusinessObject at the class level and @BusinessField at field level. Then what developers normally do is to override the three methods delegating to the utility methods:
> @Override
> public boolean equals(Object obj) { return BeanUtils.equals(this, obj); }
> @Override
> public int hashCode() { return BeanUtils.hashCode(this); }
> @Override
> public String toString() { return BeanUtils.toString(this); }
> And i.e. the method signature of equals is:
> /**
>     * Compare two @BusinessObject beans comparing only the {@link BusinessField}
>     * annotated fields.
>     *
>     * @param firstBean First bean to compare.
>     * @param secondBean Second bean to compare.
>     * @return The equals result.
>     * @throws IllegalArgumentException If one of the beans compared is not an
>     * instance of a {@link BusinessObject} annotated class.
>       */
>       public static boolean equals(Object firstBean, Object secondBean);
> In the last versions of EqualsBuilder now there is the new method reflectionEquals... but there is no a way to specify what to include in the comparison. With our two annotations we are able to let developers exactly define what need to be included in a Business comparison, as i.e. normally required by persistence framework like hibernate.
> The current implementation can also handle more complex case, comparing correctly totally different kind of objects.
> For example if all my business logic cares only about the color, I can define:
> @BusinessObject
> public class Cat{
> }
> public class ColouredCat extends Cat{ @BusinessField private String color; getter/setter }
> @BusinessObject
> public class SunSet{ @BusinessField private String color="red"; getter/setter }
> and then compare any instance of ColouredCat with a Sunset instance, finding out that the redColouredCat is (for my business logic) equal to a default instance of a Sunset. And also more tricky cases are handled (different BusinessFields, no BusinessObject annotation and so on).
> We intensively use Hibernate and the utility demonstrated to work fine with CGLIB proxies
> We always thought about the possibility to create a new Open Source project but then it was decided it would be better adding the feature to an already well know open source project...
> If you are interested I can send you more details. How can we (me more one other developer) eventually became committers?
> Thanks in advance,
> Vicio.
> P.s.: an utility method to automatically populate the BusinessFields of a BusinessObject is also implemented.
> P.s.2: In the meanwhile I started creating a new OpenSource project here: http://code.google.com/p/simplestuff/ 

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.