<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: equals and hashCode via syntactic-sugar (AKA JDK-1.5 features)</title>
	<link>http://jaybose.com/archives/equals-and-hashcode-via-syntactic-sugar-aka-jdk-15-features/</link>
	<description>Yapping about stuff.</description>
	<pubDate>Tue, 07 Sep 2010 12:42:40 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.2</generator>

	<item>
		<title>By: Sony</title>
		<link>http://jaybose.com/archives/equals-and-hashcode-via-syntactic-sugar-aka-jdk-15-features/#comment-423</link>
		<author>Sony</author>
		<pubDate>Thu, 07 Dec 2006 18:57:04 +0000</pubDate>
		<guid>http://jaybose.com/archives/equals-and-hashcode-via-syntactic-sugar-aka-jdk-15-features/#comment-423</guid>
		<description>i meant use a List to implement equals() &#38; hashcode() "instead" of CommonUtil.produceXXX.  

Regarding HashSet Impl - i have other thoughts - maybe for another time.</description>
		<content:encoded><![CDATA[<p>i meant use a List to implement equals() &amp; hashcode() &#8220;instead&#8221; of CommonUtil.produceXXX.  </p>
<p>Regarding HashSet Impl - i have other thoughts - maybe for another time.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jaybose</title>
		<link>http://jaybose.com/archives/equals-and-hashcode-via-syntactic-sugar-aka-jdk-15-features/#comment-421</link>
		<author>jaybose</author>
		<pubDate>Thu, 07 Dec 2006 15:18:31 +0000</pubDate>
		<guid>http://jaybose.com/archives/equals-and-hashcode-via-syntactic-sugar-aka-jdk-15-features/#comment-421</guid>
		<description>Sony, I wish that Sets worked that way. 

I assume storing the hashcode rather than calculating it at request time was based on performance. Not sure.</description>
		<content:encoded><![CDATA[<p>Sony, I wish that Sets worked that way. </p>
<p>I assume storing the hashcode rather than calculating it at request time was based on performance. Not sure.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sony</title>
		<link>http://jaybose.com/archives/equals-and-hashcode-via-syntactic-sugar-aka-jdk-15-features/#comment-419</link>
		<author>Sony</author>
		<pubDate>Wed, 06 Dec 2006 19:46:13 +0000</pubDate>
		<guid>http://jaybose.com/archives/equals-and-hashcode-via-syntactic-sugar-aka-jdk-15-features/#comment-419</guid>
		<description>OR:
Add all your fields to an ArrayList and return list.equals() or list.hashcode(). See contract for java.util.List.</description>
		<content:encoded><![CDATA[<p>OR:<br />
Add all your fields to an ArrayList and return list.equals() or list.hashcode(). See contract for java.util.List.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jaybose</title>
		<link>http://jaybose.com/archives/equals-and-hashcode-via-syntactic-sugar-aka-jdk-15-features/#comment-417</link>
		<author>jaybose</author>
		<pubDate>Mon, 04 Dec 2006 20:57:39 +0000</pubDate>
		<guid>http://jaybose.com/archives/equals-and-hashcode-via-syntactic-sugar-aka-jdk-15-features/#comment-417</guid>
		<description>No, as it turns out (after lengthy discussion w/ Jason C, 3 days i think), that is correct behavior. 

I also looked at the HasSet implementation, and they store the hashcode of the object as you add it to the   collection in a map of hashcodes -&#62; Objects.

The only way to gurantee that your object is found is to provide a immutable hashcode to your objects upon creation. &lt;a href="http://www.jroller.com/page/jcarreira?entry=overcoming_the_hashcode_object_identity" rel="nofollow"&gt;Jason gives more info on that.&lt;/a&gt;</description>
		<content:encoded><![CDATA[<p>No, as it turns out (after lengthy discussion w/ Jason C, 3 days i think), that is correct behavior. </p>
<p>I also looked at the HasSet implementation, and they store the hashcode of the object as you add it to the   collection in a map of hashcodes -&gt; Objects.</p>
<p>The only way to gurantee that your object is found is to provide a immutable hashcode to your objects upon creation. <a href="http://www.jroller.com/page/jcarreira?entry=overcoming_the_hashcode_object_identity" rel="nofollow">Jason gives more info on that.</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian</title>
		<link>http://jaybose.com/archives/equals-and-hashcode-via-syntactic-sugar-aka-jdk-15-features/#comment-416</link>
		<author>Brian</author>
		<pubDate>Mon, 04 Dec 2006 20:48:48 +0000</pubDate>
		<guid>http://jaybose.com/archives/equals-and-hashcode-via-syntactic-sugar-aka-jdk-15-features/#comment-416</guid>
		<description>Ok, you have non-domain objects that need to have equals and hashcode???  Oh please explain!!!

Also that original hashcode() is poop.  Looks like some autogen code that will f-up in a HashSet.  I tired that out in 1.4 and created a set of objects.  I then looped through the list, modifiying one field and then did a check if the set contained the modified object.  In all cases the modified object was not found.</description>
		<content:encoded><![CDATA[<p>Ok, you have non-domain objects that need to have equals and hashcode???  Oh please explain!!!</p>
<p>Also that original hashcode() is poop.  Looks like some autogen code that will f-up in a HashSet.  I tired that out in 1.4 and created a set of objects.  I then looped through the list, modifiying one field and then did a check if the set contained the modified object.  In all cases the modified object was not found.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jaybose</title>
		<link>http://jaybose.com/archives/equals-and-hashcode-via-syntactic-sugar-aka-jdk-15-features/#comment-415</link>
		<author>jaybose</author>
		<pubDate>Mon, 04 Dec 2006 20:28:11 +0000</pubDate>
		<guid>http://jaybose.com/archives/equals-and-hashcode-via-syntactic-sugar-aka-jdk-15-features/#comment-415</guid>
		<description>I agree, those 2 methods should be in all domain objects. However, I have non-domain objects that rely on equals/hashCode functionality. So, similar to having an external HashCodeBuilder or EqualsBuilder, you need this code in a centralized yet accessible module. Hence, the CommonUtil.</description>
		<content:encoded><![CDATA[<p>I agree, those 2 methods should be in all domain objects. However, I have non-domain objects that rely on equals/hashCode functionality. So, similar to having an external HashCodeBuilder or EqualsBuilder, you need this code in a centralized yet accessible module. Hence, the CommonUtil.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian</title>
		<link>http://jaybose.com/archives/equals-and-hashcode-via-syntactic-sugar-aka-jdk-15-features/#comment-414</link>
		<author>Brian</author>
		<pubDate>Mon, 04 Dec 2006 20:18:49 +0000</pubDate>
		<guid>http://jaybose.com/archives/equals-and-hashcode-via-syntactic-sugar-aka-jdk-15-features/#comment-414</guid>
		<description>One of the things I've been contemplating is what works best in a helper class while what should be in a parent class.  Overriding equals() and hashCode() seem to me like something you would want to do for all of your domain objects.  Is there reason you put this in it's own class?</description>
		<content:encoded><![CDATA[<p>One of the things I&#8217;ve been contemplating is what works best in a helper class while what should be in a parent class.  Overriding equals() and hashCode() seem to me like something you would want to do for all of your domain objects.  Is there reason you put this in it&#8217;s own class?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian</title>
		<link>http://jaybose.com/archives/equals-and-hashcode-via-syntactic-sugar-aka-jdk-15-features/#comment-413</link>
		<author>Brian</author>
		<pubDate>Mon, 04 Dec 2006 16:03:48 +0000</pubDate>
		<guid>http://jaybose.com/archives/equals-and-hashcode-via-syntactic-sugar-aka-jdk-15-features/#comment-413</guid>
		<description>&lt;div style="outline: thin solid black; overflow : auto"&gt;&lt;pre&gt;&lt;code&gt;
public static boolean produceEquals(final Object... itemsToCompare) {
       boolean value = true;
        if (isNonEmpty(itemsToCompare)) {
            if (0 == itemsToCompare.length % 2) {
                for (...) {
                    final Object itemA = itemsToCompare[index];
                    final Object itemB = itemsToCompare[index + 1];
                    if (null == itemA ? null != itemB : !itemA.equals(itemB)) {
		value =  false;
		break:
	        }
                }
            } else {
                throw new IllegalArgumentException("produceEquals() expects an itemsToCompare array of even length.");
            }
        }
        return value;
    }
&lt;/code&gt;&lt;/pre&gt;

I like the code to only have one exit in it because it makes the code easier to test and debug.  When you have multiple exit points you are adding complexity and can find youself getting a result you may not have expected.  Above is how I would refactor your produceEquals method.&lt;/div&gt;</description>
		<content:encoded><![CDATA[<div style="outline: thin solid black; overflow : auto">
<pre><code>
public static boolean produceEquals(final Object... itemsToCompare) {
       boolean value = true;
        if (isNonEmpty(itemsToCompare)) {
            if (0 == itemsToCompare.length % 2) {
                for (...) {
                    final Object itemA = itemsToCompare[index];
                    final Object itemB = itemsToCompare[index + 1];
                    if (null == itemA ? null != itemB : !itemA.equals(itemB)) {
		value =  false;
		break:
	        }
                }
            } else {
                throw new IllegalArgumentException("produceEquals() expects an itemsToCompare array of even length.");
            }
        }
        return value;
    }
</code></pre>
<p>I like the code to only have one exit in it because it makes the code easier to test and debug.  When you have multiple exit points you are adding complexity and can find youself getting a result you may not have expected.  Above is how I would refactor your produceEquals method.</p></div>
]]></content:encoded>
	</item>
	<item>
		<title>By: jaybose</title>
		<link>http://jaybose.com/archives/equals-and-hashcode-via-syntactic-sugar-aka-jdk-15-features/#comment-409</link>
		<author>jaybose</author>
		<pubDate>Mon, 04 Dec 2006 15:56:41 +0000</pubDate>
		<guid>http://jaybose.com/archives/equals-and-hashcode-via-syntactic-sugar-aka-jdk-15-features/#comment-409</guid>
		<description>Umm, thx. I removed the extra "return false;" after the exception.</description>
		<content:encoded><![CDATA[<p>Umm, thx. I removed the extra &#8220;return false;&#8221; after the exception.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian</title>
		<link>http://jaybose.com/archives/equals-and-hashcode-via-syntactic-sugar-aka-jdk-15-features/#comment-408</link>
		<author>Brian</author>
		<pubDate>Mon, 04 Dec 2006 15:43:28 +0000</pubDate>
		<guid>http://jaybose.com/archives/equals-and-hashcode-via-syntactic-sugar-aka-jdk-15-features/#comment-408</guid>
		<description>You think you can add some exit points form you equals method?  That code is HOT!!!  LOL...jerky!!!</description>
		<content:encoded><![CDATA[<p>You think you can add some exit points form you equals method?  That code is HOT!!!  LOL&#8230;jerky!!!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
