<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Codemunchies &#187; casting</title>
	<atom:link href="http://codemunchies.com/tag/casting/feed/" rel="self" type="application/rss+xml" />
	<link>http://codemunchies.com</link>
	<description>Satisfy your cravings</description>
	<lastBuildDate>Mon, 19 Sep 2011 19:32:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Cast away with Java generics</title>
		<link>http://codemunchies.com/2009/10/cast-away-with-java-generics/</link>
		<comments>http://codemunchies.com/2009/10/cast-away-with-java-generics/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 10:22:18 +0000</pubDate>
		<dc:creator>Jaran Nilsen</dc:creator>
				<category><![CDATA[Prototyping]]></category>
		<category><![CDATA[casting]]></category>
		<category><![CDATA[generics]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://codemunchies.com/?p=175</guid>
		<description><![CDATA[They say it’s those little things in life - like casting and generics in Java.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcodemunchies.com%2F2009%2F10%2Fcast-away-with-java-generics%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcodemunchies.com%2F2009%2F10%2Fcast-away-with-java-generics%2F&amp;source=codemunchies&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><strong>They say it&#8217;s those little things in life. I believe the same is very much true for your code as well. Here&#8217;s an example with casting and generics in Java.</strong></p>
<p>After a long hard day in the code editor I often find that what makes me happy about my day, and the work I check in to our <a href="http://en.wikipedia.org/wiki/Source_Code_Management">SCM</a>, is not always the additions to an advanced algorithm, or an insanely compact chunk of awesomeness, but it&#8217;s more often a small piece of beautiful code. One of the ugliest lines of code I see in Java, are lines involved with <em>casting</em>.</p>
<p>I recently started doing a bit of coding on a web application a couple of weeks back. Suddenly I found myself writing a lot of casting statements when fetching data from session, which made my code want to crawl out of the editor due to it&#8217;s own ugliness. Here is an example:</p>
<pre class="brush: java; toolbar: false;">Session webappSession = ...

Map&lt;String, Map&lt;String, Collection&lt;Entity&gt;&gt;&gt; myUglyMap = (Map&lt;String, Map&lt;String, Collection&lt;Entity&gt;&gt;&gt;)(webappSession.getAttribute("myUglyMap"));</pre>
<p>Oh boy! I figured there must be some other way to write this. With our static imports fever as of late I came up with a tiny piece of code which made me go home smiling that day. That little thing which made it all so much more beautiful&#8230;</p>
<pre class="brush: java; toolbar: false;">public static &lt;T&gt; T cast(Object o) {

    return (T) o;

}</pre>
<p>Placed in a easily accessible class in your favourite toolbox project, the messy snippet above quickly becomes much better:</p>
<pre class="brush: java; toolbar: false;">import static com.mydomain.CastUtils.cast;

/* snipp */

Session webappSession = ...

Map&lt;String, Map&lt;String, Collection&lt;Entity&gt;&gt;&gt; beautiful = cast(webappSession.getAttribute("beautiful"));</pre>
<div>A small piece of simple code, so full of awesomeness.</div>
<blockquote>
<div><em><a href="http://www.flickr.com/photos/shazwan/409580429/">Article thumbnail</a> by <a href="http://www.flickr.com/photos/shazwan/" rel="cc:attributionURL">http://www.flickr.com/photos/shazwan/</a></em> / <a href="http://creativecommons.org/licenses/by/2.0/" rel="license">CC BY 2.0</a></div>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://codemunchies.com/2009/10/cast-away-with-java-generics/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>

