<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Beautiful casting gone bad</title>
	<atom:link href="http://www.javablogging.com/beautiful-casting-gone-bad/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javablogging.com/beautiful-casting-gone-bad/</link>
	<description>Tracking surprises, features and bugs</description>
	<lastBuildDate>Fri, 12 Mar 2010 19:17:44 -0600</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Nanda Firdausi</title>
		<link>http://www.javablogging.com/beautiful-casting-gone-bad/comment-page-1/#comment-1467</link>
		<dc:creator>Nanda Firdausi</dc:creator>
		<pubDate>Wed, 06 Jan 2010 20:50:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.javablogging.com/?p=834#comment-1467</guid>
		<description>I agree with Jaran, and I think I&#039;ve better improvement that avoid the problems you mention for the other cases in: http://satukubik.com/2010/01/06/java-tips-using-generic-correctly/</description>
		<content:encoded><![CDATA[<p>I agree with Jaran, and I think I&#8217;ve better improvement that avoid the problems you mention for the other cases in: <a href="http://satukubik.com/2010/01/06/java-tips-using-generic-correctly/" rel="nofollow">http://satukubik.com/2010/01/06/java-tips-using-generic-correctly/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Java Tips: Using generic correctly &#124; satukubik</title>
		<link>http://www.javablogging.com/beautiful-casting-gone-bad/comment-page-1/#comment-1466</link>
		<dc:creator>Java Tips: Using generic correctly &#124; satukubik</dc:creator>
		<pubDate>Wed, 06 Jan 2010 20:33:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.javablogging.com/?p=834#comment-1466</guid>
		<description>[...] wrong with that? PPOW show the wrong cases to [...]</description>
		<content:encoded><![CDATA[<p>[...] wrong with that? PPOW show the wrong cases to [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jaran Nilsen</title>
		<link>http://www.javablogging.com/beautiful-casting-gone-bad/comment-page-1/#comment-887</link>
		<dc:creator>Jaran Nilsen</dc:creator>
		<pubDate>Thu, 05 Nov 2009 12:28:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.javablogging.com/?p=834#comment-887</guid>
		<description>Hi, and thanks for the well covered response to &lt;a href=&quot;http://codemunchies.com/2009/10/cast-away-with-java-generics/&quot; rel=&quot;nofollow&quot;&gt;my blog entry over at Codemunchies.com&lt;/a&gt;. 

I still have to say I disagree with several of your points though. Especially when it comes to type checking and whether you get the warning at compile time or not. 

The core issue here is when you work with type unsafe structures such as:

&lt;code&gt;
Map someMap = new HashMap();
&lt;/code&gt;

This is the case for a HttpSession which I have used as example in my blog entry.

It does not matter if you cast any structure put into that map using my solution with a static cast-method, or using your adapter pattern. In both cases you will not detect any incorrect cast before runtime, and with the adapter you are simply moving the location where the cast exception will be thrown into the adapter.

Consider this case:

&lt;code&gt;
final Map someMap = new HashMap();
        final List&lt;Map&gt; listOfMapsOfIntegers = newArrayList();
        someMap.put(&quot;listOfMapsOfIntegers&quot;, listOfMapsOfIntegers);
&lt;/code&gt;

Now, consider another part of you code trying to access this &quot;list of maps of Integers&quot;, but it is being cast to the incorrect type, like this: 

&lt;code&gt;
final List&lt;Map&lt;String, Set&gt;&gt; myIncorrectlyCastMap = (List&lt;Map&lt;String, Set&gt;&gt;) someMap
                .get(&quot;listOfMapsOfIntegers&quot;);
&lt;/code&gt;

or this:

&lt;code&gt;
final List&lt;Map&lt;String, Set&gt;&gt; myIncorrectlyCastMap = cast(someMap
                .get(&quot;listOfMapsOfIntegers&quot;));
&lt;/code&gt;

Either way, you will not receive an error until that code is actually executed, which only causes the cast-method to increase code readability, in my opinion. 

Introducing the adapter pattern and having methods for accessing every single object that has been put into the map adds a significant amount of unnecessary code to the project.

It&#039;s an interesting discussion though, and again thanks for taking your time to respond to my proposed solution :)

Jaran</description>
		<content:encoded><![CDATA[<p>Hi, and thanks for the well covered response to <a href="http://codemunchies.com/2009/10/cast-away-with-java-generics/" rel="nofollow">my blog entry over at Codemunchies.com</a>. </p>
<p>I still have to say I disagree with several of your points though. Especially when it comes to type checking and whether you get the warning at compile time or not. </p>
<p>The core issue here is when you work with type unsafe structures such as:</p>
<p><code><br />
Map someMap = new HashMap();<br />
</code></p>
<p>This is the case for a HttpSession which I have used as example in my blog entry.</p>
<p>It does not matter if you cast any structure put into that map using my solution with a static cast-method, or using your adapter pattern. In both cases you will not detect any incorrect cast before runtime, and with the adapter you are simply moving the location where the cast exception will be thrown into the adapter.</p>
<p>Consider this case:</p>
<p><code><br />
final Map someMap = new HashMap();<br />
        final List&lt;Map&gt; listOfMapsOfIntegers = newArrayList();<br />
        someMap.put("listOfMapsOfIntegers", listOfMapsOfIntegers);<br />
</code></p>
<p>Now, consider another part of you code trying to access this &#8220;list of maps of Integers&#8221;, but it is being cast to the incorrect type, like this: </p>
<p><code><br />
final List&lt;Map&lt;String, Set&gt;&gt; myIncorrectlyCastMap = (List&lt;Map&lt;String, Set&gt;&gt;) someMap<br />
                .get("listOfMapsOfIntegers");<br />
</code></p>
<p>or this:</p>
<p><code><br />
final List&lt;Map&lt;String, Set&gt;&gt; myIncorrectlyCastMap = cast(someMap<br />
                .get("listOfMapsOfIntegers"));<br />
</code></p>
<p>Either way, you will not receive an error until that code is actually executed, which only causes the cast-method to increase code readability, in my opinion. </p>
<p>Introducing the adapter pattern and having methods for accessing every single object that has been put into the map adds a significant amount of unnecessary code to the project.</p>
<p>It&#8217;s an interesting discussion though, and again thanks for taking your time to respond to my proposed solution <img src='http://www.javablogging.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Jaran</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paul W. Homer</title>
		<link>http://www.javablogging.com/beautiful-casting-gone-bad/comment-page-1/#comment-872</link>
		<dc:creator>Paul W. Homer</dc:creator>
		<pubDate>Tue, 03 Nov 2009 21:07:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.javablogging.com/?p=834#comment-872</guid>
		<description>In your last example, a map holding a bunch of sets is a fairly complex type. I&#039;ve always found it nicer to create real objects out of any consistently used structures within the code, so that: 

Phonebook book = sessionAdapter.getPhonebook();

where Phonebook is an object (whose underlying behavior is a map of sets). 

As for the cast, in some cases I&#039;ve found it really useful to get loosely typed behavior in a strongly typed language, but you&#039;re right, using that cast trick opens up too way many holes. For me, I created a whole lot of static functions such as Value.String(name) that force any arguments into the type OR return null (but never exceptions). Then there is a string called &quot;name&quot; in the system, or not (and if there is an integer, it is explicitly converted to a String because the code knows how to do that). Thus the code is loosely typed, but still very deterministic (and not going to blow up with strange exceptions when running).

Paul.</description>
		<content:encoded><![CDATA[<p>In your last example, a map holding a bunch of sets is a fairly complex type. I&#8217;ve always found it nicer to create real objects out of any consistently used structures within the code, so that: </p>
<p>Phonebook book = sessionAdapter.getPhonebook();</p>
<p>where Phonebook is an object (whose underlying behavior is a map of sets). </p>
<p>As for the cast, in some cases I&#8217;ve found it really useful to get loosely typed behavior in a strongly typed language, but you&#8217;re right, using that cast trick opens up too way many holes. For me, I created a whole lot of static functions such as Value.String(name) that force any arguments into the type OR return null (but never exceptions). Then there is a string called &#8220;name&#8221; in the system, or not (and if there is an integer, it is explicitly converted to a String because the code knows how to do that). Thus the code is loosely typed, but still very deterministic (and not going to blow up with strange exceptions when running).</p>
<p>Paul.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
