<?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/"
	xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>WarpedJavaGuy</title>
	<atom:link href="http://warpedjavaguy.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://warpedjavaguy.wordpress.com</link>
	<description>Observations of everyday Java phenomena</description>
	<lastBuildDate>Tue, 28 Apr 2009 15:10:26 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<image>
		<url>http://www.gravatar.com/blavatar/05f7833c15b4a747643fabb0f037564e?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>WarpedJavaGuy</title>
		<link>http://warpedjavaguy.wordpress.com</link>
	</image>
			<item>
		<title>Implicitly Formatted Exceptions</title>
		<link>http://warpedjavaguy.wordpress.com/2008/09/16/implicitly-formatted-exceptions/</link>
		<comments>http://warpedjavaguy.wordpress.com/2008/09/16/implicitly-formatted-exceptions/#comments</comments>
		<pubDate>Tue, 16 Sep 2008 12:56:31 +0000</pubDate>
		<dc:creator>WarpedJavaGuy</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[exceptions]]></category>

		<guid isPermaLink="false">http://warpedjavaguy.wordpress.com/?p=110</guid>
		<description><![CDATA[
It takes a lot of ugly code to create beautiful exception messages.
&#160;

In Java, we write code that throws and catches exceptions a lot.  The core Java exception classes provide the following two constructors for instantiating exceptions with messages:
public Throwable(String message)
public Throwable(String message, Throwable cause)
Creating nicely formatted and meaningful exception messages using these two constructors [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpedjavaguy.wordpress.com&blog=809469&post=110&subd=warpedjavaguy&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><blockquote><p><em><br />
It takes a lot of ugly code to create beautiful exception messages.</em><br />
&nbsp;<br />
<em></em></p></blockquote>
<p>In Java, we write code that throws and catches exceptions a lot.  The core Java exception classes provide the following two constructors for instantiating exceptions with messages:</p>
<p><code>public Throwable(String message)</code><br />
<code>public Throwable(String message, Throwable cause)</code></p>
<p>Creating nicely formatted and meaningful exception messages using these two constructors is more difficult than it should be and often involves a lot of concatenations of plain text and formatted data.  These concatenated string conglomerations are so tedious to write that they deter us from good message formatting practices (let alone proper grammatical correctness).  When we do put in the extra effort to get our exception messages right, our code suddenly becomes ugly and loses its elegance.</p>
<p>The <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/text/MessageFormat.html">MessageFormat</a> class would help a lot in this regard.  Using it explicitly to create exception messages can make life a little easier, but I often find myself extending exceptions to use it implicitly like this:</p>
<pre>
<pre name="code" class="java">

import java.text.MessageFormat;

public class MyException extends Exception {

public MyException(String pattern, Object... args) {
super(MessageFormat.format(pattern, args));
}

public MyException(Exception cause, String pattern, Object... args) {
super(MessageFormat.format(pattern, args), cause);
}
}
</pre>
</pre>
<p>Wouldn&#8217;t it be nice if all Java exceptions had these additional constructors?</p>
<p><code>public Throwable(String pattern, Object... args)</code><br />
<code>public Throwable(Throwable cause, String pattern, Object... args)</code><br />
<!-- AddThis Bookmark Button BEGIN --><a title="Bookmark using any bookmark manager!" href="http://www.addthis.com/bookmark.php?url=http://warpedjavaguy.wordpress.com/2008/09/16/implicitly-formatted-exceptions/" target="_blank"><img src="http://s9.addthis.com/button1-bm.gif" border="0" alt="AddThis Social Bookmark Button" width="125" height="16" /></a><!-- AddThis Bookmark Button END --> <!-- AddThis Feed Button BEGIN --><a title="Subscribe using any feed reader!" href="http://www.addthis.com/feed.php?pub=warpedjavaguy&amp;h1=http%3A%2F%2Fwarpedjavaguy.wordpress.com%2Ffeed%2F&amp;t1="><img src="http://s9.addthis.com/button1-fd.gif" border="0" alt="AddThis Feed Button" width="125" height="16" /></a><!-- AddThis Feed Button END --></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/warpedjavaguy.wordpress.com/110/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/warpedjavaguy.wordpress.com/110/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/warpedjavaguy.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/warpedjavaguy.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/warpedjavaguy.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/warpedjavaguy.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/warpedjavaguy.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/warpedjavaguy.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/warpedjavaguy.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/warpedjavaguy.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/warpedjavaguy.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/warpedjavaguy.wordpress.com/110/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpedjavaguy.wordpress.com&blog=809469&post=110&subd=warpedjavaguy&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://warpedjavaguy.wordpress.com/2008/09/16/implicitly-formatted-exceptions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b5c8e528c21ff5b742756afdef7a9219?s=96&#38;d=identicon" medium="image">
			<media:title type="html">WarpedJavaGuy</media:title>
		</media:content>

		<media:content url="http://s9.addthis.com/button1-bm.gif" medium="image">
			<media:title type="html">AddThis Social Bookmark Button</media:title>
		</media:content>

		<media:content url="http://s9.addthis.com/button1-fd.gif" medium="image">
			<media:title type="html">AddThis Feed Button</media:title>
		</media:content>
	</item>
		<item>
		<title>Polymorphism by Closure Injection?</title>
		<link>http://warpedjavaguy.wordpress.com/2008/09/04/polymorphism-by-closure-injection/</link>
		<comments>http://warpedjavaguy.wordpress.com/2008/09/04/polymorphism-by-closure-injection/#comments</comments>
		<pubDate>Thu, 04 Sep 2008 13:31:38 +0000</pubDate>
		<dc:creator>WarpedJavaGuy</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[closures]]></category>

		<guid isPermaLink="false">http://warpedjavaguy.wordpress.com/?p=33</guid>
		<description><![CDATA[
Instead of defining many classes to do the same thing differently through polymorphism, you could define just one class and achieve the same through closure injection!
&#160;

I recently downloaded the feature complete BGGA closures prototype and had a bit of a play.  My first experiment was to refactor an existing codebase and replace all anonymous [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpedjavaguy.wordpress.com&blog=809469&post=33&subd=warpedjavaguy&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><blockquote><p><em><br />
Instead of defining many classes to do the same thing differently through polymorphism, you could define just one class and achieve the same through closure injection!<br />
&nbsp;<br />
</em></p></blockquote>
<p>I recently downloaded the <a href="http://gafter.blogspot.com/2008/08/java-closures-prototype-feature.html">feature complete BGGA closures prototype</a> and had a bit of a play.  My first experiment was to refactor an existing codebase and replace all anonymous inner classes with closures.  The ones that extended single abstract method (SAM) interfaces were easy to convert.  The ones that extended multiple method (non-SAM) types were a bit trickier.  In the end though, I managed to successfully convert all of them, and with very little effort.</p>
<p>In converting the non-SAM types, I discovered that the functional equivalent of polymorphism can be achieved by injecting closures into a single concrete implementation having no subclasses.</p>
<p>The codebase I refactored was a small (fictitious) railroad application that provided customers with information about routes.  In particular, it calculated things like the number of routes between two cities, the number of routes that do not exceed a given number of stops, the distances of individual routes, and the like.  It did this by deriving routes on the fly from a given set of legs and dynamically yielding only those that match a given predicate (or condition).</p>
<p>Here is the original predicate code using anonymous inner classes:</p>
<pre name="code" class="java">

public abstract class Predicate&lt;T&gt; {

  public boolean eligible(T item) {
    return yield(item);
  }

  public abstract boolean yield(T item);

  /* Pre-defined predicates follow */

  public static Predicate&lt;Route&gt; routesWithMaxStops(final int maxStops) {
    return new Predicate&lt;Route&gt;() {
      public boolean yield(Route route) {
        return route.getLegs().size() &lt;= maxStops;
      }
    };
  }

  public static Predicate&lt;Route&gt; routesWithStops(final int stops) {
    return new Predicate&lt;Route&gt;() {
      public boolean eligible(Route route) {
        return route.getLegs().size() &lt;= stops;
      }
      public boolean yield(Route route) {
        return route.getLegs().size() == stops;
      }
    };
  }

}
</pre>
<p>In the above code only two predefined predicates are shown for brevity (the actual code has got more).</p>
<p>Note that the predicate has two methods.  Only routes for which both methods return true are yielded.</p>
<ul>
<li><em>eligible</em> &#8211; determines if a route satisfies a boundary condition</li>
<li><em>yield</em> &#8211; determines if a route should be yielded</li>
</ul>
<p>Here is the converted closure equivalent:</p>
<pre name="code" class="java">

public class Predicate&lt;T&gt; {

  private {T =&gt; boolean} eligible;
  private {T =&gt; boolean} yield;

  public Predicate({T =&gt; boolean} yield) {
    this(yield, yield);
  }

  public Predicate({T =&gt; boolean} eligible, {T =&gt; boolean} yield) {
    this.eligible = eligible;
    this.yield = yield;
  }

  public boolean eligible(T item) {
    return eligible.invoke(item);
  }

  public boolean yield(T item) {
    return yield.invoke(item);
  }

  /* Pre-defined predicates follow */

  public static Predicate&lt;Route&gt; routesWithMaxStops(int maxStops) {
    return new Predicate&lt;Route&gt;(
      {Route route =&gt; route.getLegs().size() &lt;= maxStops});
  }

  public static Predicate&lt;Route&gt; routesWithStops(int stops) {
    return new Predicate&lt;Route&gt;(
      {Route route =&gt; route.getLegs().size() &lt;= stops},
      {Route route =&gt; route.getLegs().size() == stops});
  }

}
</pre>
<p>If you look carefully, you will notice that both the original and converted code produce a redundant invocation when the <em>eligible</em> and <em>yield</em> expressions are identical.  Fortunately, this is a flaw that can be very easily corrected in the closure version.</p>
<p>A possible correction is shown below (see lines 5, 17, 18, and 22):</p>
<pre name="code" class="java">

public class Predicate&lt;T&gt; {

  private {T =&gt; boolean} eligible;
  private {T =&gt; boolean} yield;
  private boolean isEligible;

  public Predicate({T =&gt; boolean} yield) {
    this(yield, yield);
  }

  public Predicate({T =&gt; boolean} eligible, {T =&gt; boolean} yield) {
    this.eligible = eligible;
    this.yield = yield;
  }

  public boolean eligible(T item) {
    isEligible = eligible.invoke(item);
    return isEligible;
  }

  public boolean yield(T item) {
    return (isEligible &amp;&amp; eligible == yield) ? true : yield.invoke(item);
  }

  /* Unchanged pre-defined predicates not shown here */

}
</pre>
<p>Applying the same correction to the original closureless version is not nearly as simple.  It would require a more complex solution and could even involve changing the interface.</p>
<p>It is very interesting that polymorphic functionality can be achieved by injecting closures into one concrete class instead of defining multiple subclasses of an abstract class.  Closures and anonymous inner classes, injection and inheritance, deferred execution and late binding.  So many great choices!</p>
<p><!-- AddThis Bookmark Button BEGIN --><a title="Bookmark using any bookmark manager!" href="http://www.addthis.com/bookmark.php?url=http://warpedjavaguy.wordpress.com/2008/09/04/polymorphism-by-closure-injection/" target="_blank"><img src="http://s9.addthis.com/button1-bm.gif" border="0" alt="AddThis Social Bookmark Button" width="125" height="16" /></a><!-- AddThis Bookmark Button END --> <!-- AddThis Feed Button BEGIN --><a title="Subscribe using any feed reader!" href="http://www.addthis.com/feed.php?pub=warpedjavaguy&amp;h1=http%3A%2F%2Fwarpedjavaguy.wordpress.com%2Ffeed%2F&amp;t1="><img src="http://s9.addthis.com/button1-fd.gif" border="0" alt="AddThis Feed Button" width="125" height="16" /></a><!-- AddThis Feed Button END --></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/warpedjavaguy.wordpress.com/33/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/warpedjavaguy.wordpress.com/33/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/warpedjavaguy.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/warpedjavaguy.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/warpedjavaguy.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/warpedjavaguy.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/warpedjavaguy.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/warpedjavaguy.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/warpedjavaguy.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/warpedjavaguy.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/warpedjavaguy.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/warpedjavaguy.wordpress.com/33/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpedjavaguy.wordpress.com&blog=809469&post=33&subd=warpedjavaguy&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://warpedjavaguy.wordpress.com/2008/09/04/polymorphism-by-closure-injection/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b5c8e528c21ff5b742756afdef7a9219?s=96&#38;d=identicon" medium="image">
			<media:title type="html">WarpedJavaGuy</media:title>
		</media:content>

		<media:content url="http://s9.addthis.com/button1-bm.gif" medium="image">
			<media:title type="html">AddThis Social Bookmark Button</media:title>
		</media:content>

		<media:content url="http://s9.addthis.com/button1-fd.gif" medium="image">
			<media:title type="html">AddThis Feed Button</media:title>
		</media:content>
	</item>
		<item>
		<title>Java Closures &#8211; The Fundamental Benefit</title>
		<link>http://warpedjavaguy.wordpress.com/2008/06/25/java-closures-the-fundamental-benefit/</link>
		<comments>http://warpedjavaguy.wordpress.com/2008/06/25/java-closures-the-fundamental-benefit/#comments</comments>
		<pubDate>Wed, 25 Jun 2008 12:32:41 +0000</pubDate>
		<dc:creator>WarpedJavaGuy</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[closures]]></category>

		<guid isPermaLink="false">http://warpedjavaguy.wordpress.com/?p=32</guid>
		<description><![CDATA[
Avoiding redundant code is a basic programming instinct.
&#160;

How many times have you written a piece of code and thought &#8220;I wish I could reuse this block of code&#8221;?  How many times have you refactored existing code to remove redundancies?  How many times have you written the same block of code more than once [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpedjavaguy.wordpress.com&blog=809469&post=32&subd=warpedjavaguy&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><blockquote><p><em><br />
Avoiding redundant code is a basic programming instinct.<br />
&nbsp;<br />
</em></p></blockquote>
<p>How many times have you written a piece of code and thought &#8220;I wish I could reuse this block of code&#8221;?  How many times have you refactored existing code to remove redundancies?  How many times have you written the same block of code more than once before realising that an abstraction exists?  How many times have you extracted such abstractions successfully?   How difficult was it to do and how much effort was involved?  How constrained were you by the current Java language constructs?  These are all questions pertaining to the everyday challenges we face as Java programmers in our struggles to achieve zero code redundancy.</p>
<p>Closures are reusable blocks of code that capture the environment and can be passed around as method arguments for immediate or deferred execution.  Why do we need them in Java?  There are <a href="http://tech.puredanger.com/java7#closures">many reasons for and against</a>, but the fundamental benefit they provide is the facilitation of redundant code avoidance.  The <a href="http://www.javac.info/closures-v05.html">current Java closures specification</a> makes a strong point of this in the first paragraph where it states: &#8220;they allow one to more easily extract the common parts of two almost-identical pieces of code&#8221;.</p>
<p>Closures provide an elegant means of reusing blocks of code and avoiding code duplication without the boilerplate.  This is the fundamental benefit that closures bring to Java.  All other benefits are derived benefits inherited from this fundamental benefit.  In fact, all the needs addressed by the <a href="http://www.javac.info/consensus-closures-jsr.html">Java closures proposal</a> are all derived benefits.</p>
<p>I know I am just stating the obvious and reiterating my point here, but it is just too easy to overlook this one fundamental benefit and be overwhelmed by all others.  Java closures make it easier to eliminate redundant code and avoid it altogether!<br />
<!-- AddThis Bookmark Button BEGIN --><a title="Bookmark using any bookmark manager!" href="http://www.addthis.com/bookmark.php?url=http://warpedjavaguy.wordpress.com/2008/06/25/java-closures-the-fundamental-benefit" target="_blank"><img src="http://s9.addthis.com/button1-bm.gif" border="0" alt="AddThis Social Bookmark Button" width="125" height="16" /></a><!-- AddThis Bookmark Button END --> <!-- AddThis Feed Button BEGIN --><a title="Subscribe using any feed reader!" href="http://www.addthis.com/feed.php?pub=warpedjavaguy&amp;h1=http%3A%2F%2Fwarpedjavaguy.wordpress.com%2Ffeed%2F&amp;t1="><img src="http://s9.addthis.com/button1-fd.gif" border="0" alt="AddThis Feed Button" width="125" height="16" /></a><!-- AddThis Feed Button END --></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/warpedjavaguy.wordpress.com/32/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/warpedjavaguy.wordpress.com/32/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/warpedjavaguy.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/warpedjavaguy.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/warpedjavaguy.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/warpedjavaguy.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/warpedjavaguy.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/warpedjavaguy.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/warpedjavaguy.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/warpedjavaguy.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/warpedjavaguy.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/warpedjavaguy.wordpress.com/32/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpedjavaguy.wordpress.com&blog=809469&post=32&subd=warpedjavaguy&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://warpedjavaguy.wordpress.com/2008/06/25/java-closures-the-fundamental-benefit/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b5c8e528c21ff5b742756afdef7a9219?s=96&#38;d=identicon" medium="image">
			<media:title type="html">WarpedJavaGuy</media:title>
		</media:content>

		<media:content url="http://s9.addthis.com/button1-bm.gif" medium="image">
			<media:title type="html">AddThis Social Bookmark Button</media:title>
		</media:content>

		<media:content url="http://s9.addthis.com/button1-fd.gif" medium="image">
			<media:title type="html">AddThis Feed Button</media:title>
		</media:content>
	</item>
		<item>
		<title>Closure Syntax Wars</title>
		<link>http://warpedjavaguy.wordpress.com/2008/02/28/closure-syntax-wars/</link>
		<comments>http://warpedjavaguy.wordpress.com/2008/02/28/closure-syntax-wars/#comments</comments>
		<pubDate>Thu, 28 Feb 2008 01:14:47 +0000</pubDate>
		<dc:creator>WarpedJavaGuy</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[closures]]></category>

		<guid isPermaLink="false">http://warpedjavaguy.wordpress.com/?p=31</guid>
		<description><![CDATA[
Closures look weird and complicated but so does all other code in general.
&#160;

There seems to be some disagreement amongst the Java community about what the exact syntax for Java closures should be.  The two currently most popular closure proposals are BGGA and FCM and they each use a different syntax.  Neither syntax is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpedjavaguy.wordpress.com&blog=809469&post=31&subd=warpedjavaguy&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><blockquote><p><em><br />
Closures look weird and complicated but so does all other code in general.<br />
&nbsp;<br />
</em></p></blockquote>
<p>There seems to be some disagreement amongst the Java community about what the exact syntax for Java closures should be.  The two currently most popular closure proposals are <a href="http://www.javac.info/">BGGA</a> and <a href="http://docs.google.com/Doc?id=ddhp95vd_0f7mcns">FCM</a> and they each use a different syntax.  Neither syntax is final and looking at the discussions and comments in recent <a href="http://java.net/pub/pq/196">polls</a> and <a href="http://weblogs.java.net/blog/alexwinston/archive/2008/02/closure_syntax_1.html">many</a> <a href="http://viewfromthefringe.blogspot.com/2008/02/alternate-syntax-for-java-closures.html">various</a> <a href="http://www.jroller.com/scolebourne/entry/fcm_prototype_available">blogs</a> it is evident that we all have our own opinions, preferences, and personal favourites (<a href="http://warpedjavaguy.wordpress.com/2008/02/21/pointer-syntax-for-java-closures/">myself included</a>).  And as developers why shouldn&#8217;t we?  We are the ones that will potentially have to write and maintain code that uses closures.  That&#8217;s why we care about the syntax.</p>
<p>The current BGGA proposal uses the &#8216;=&gt;&#8217; syntax.  Although this looks like an arrow that points to a block of code, it can sometimes trick the eye and easily be mistaken for the &#8216;&gt;=&#8217; and &#8216;&lt;=&#8217; conditional operators.  Consider the example below which defines a function that returns a boolean and accepts a parameter of type int and compares it against two variables.</p>
<pre name="code" class="java">

{int =&gt; boolean} f = { int x =&gt; y &lt;= x &amp;&amp; x &lt;= z };
</pre>
<p>Now consider the same example using the current FCM &#8216;#&#8217; syntax.  This style is designed to look and feel like Java method signatures.  It is less confusing and easier to grasp though.</p>
<pre name="code" class="java">

&lt;ol&gt;
  &lt;li&gt;(boolean(int)) f =  #(int x) { y &lt;= x &amp;&amp; x &lt;= z };
</pre>
</li>
</ol>
<p>In my previous post I questioned why we shouldn&#8217;t consider <a href="http://warpedjavaguy.wordpress.com/2008/02/21/pointer-syntax-for-java-closures/">Java Method Pointers</a> (JMP).  The inspiration for this was a familiar but variant form of the C/C++ function pointer syntax.  The same example would look something like this:</p>
<pre name="code" class="java">

boolean* (int) f = (int x) { y &lt;= x &amp;&amp; x &lt;= z };
</pre>
<p>Closures are indeed an alien concept to Java and they sure look alien too.  Throw generics into the mix and they can look even more weird and complicated.Take a look at the following two examples:</p>
<p>Neal Gafter&#8217;s <a href="http://gafter.blogspot.com/2008/02/closures-puzzler-neapolitan-ice-cream.html">closures puzzler</a> which transforms a list of objects of type T to a list of objects of type U.</p>
<pre name="code" class="java">

static  List&lt;U&gt; map(List list, {T=&gt;U} transform) {
  List&lt;U&gt; result = new ArrayList&lt;U&gt;(list.size());
  for (T t : list) {
    result.add(transform.invoke(t));
  }
  return result;
}
</pre>
<p>Stephen Colebourne&#8217;s <a href="http://www.jroller.com/scolebourne/entry/evaluating_bgga_closures">evaluating BGGA example</a> which converts an object of type T to an object of type U.</p>
<pre name="code" class="java">

public  {T =&gt; U} converter({=&gt; T} a, {=&gt; U} b, {T =&gt; U} c) {
  return {T t =&gt; a.invoke().equals(t) ? b.invoke() : c.invoke(t)};
}
</pre>
<p>At the end of the day, we really want closures in Java for doing <a href="http://www.javac.info/consensus-closures-jsr.html">these nice things</a>:</p>
<ul>
<li>Simplifying anonymous class instance creation</li>
<li>Automating resource management/termination (<a href="http://docs.google.com/View?docid=dffxznxr_1nmsqkz">ARM blocks</a>)</li>
<li>Using for-each style loop constructs</li>
<li>Writing less boilerplate code</li>
</ul>
<p>Closures would make it all possible.  A lot of the complicated closure constructs involving generics would be integrated into the API.  Developers would not have to write those.  Java would provide them out of the box.</p>
<p>The more you look at closures and read about them the more familiar and less alien they become.  Just like everything else, they do take a little getting used to.  The question is what closure syntax do we want to get used to?   Will it be good for Java?  One day we may have to decide.  Until then, the syntax war will continue.</p>
<p><!-- AddThis Bookmark Button BEGIN --><a title="Bookmark using any bookmark manager!" href="http://www.addthis.com/bookmark.php?url=http://warpedjavaguy.wordpress.com/2008/02/28/closure-syntax-wars/" target="_blank"><img src="http://s9.addthis.com/button1-bm.gif" border="0" alt="AddThis Social Bookmark Button" width="125" height="16" /></a><!-- AddThis Bookmark Button END --> <!-- AddThis Feed Button BEGIN --><a title="Subscribe using any feed reader!" href="http://www.addthis.com/feed.php?pub=warpedjavaguy&amp;h1=http%3A%2F%2Fwarpedjavaguy.wordpress.com%2Ffeed%2F&amp;t1="><img src="http://s9.addthis.com/button1-fd.gif" border="0" alt="AddThis Feed Button" width="125" height="16" /></a><!-- AddThis Feed Button END --></p>
<p><a class="tr-linkcount" href="http://technorati.com/search/http://warpedjavaguy.wordpress.com/2008/02/28/closure-syntax-wars/"><img style="border:0 none;vertical-align:middle;margin-left:.4em;" src="http://static.technorati.com/static/img/pub/icon-utag-16x13.png?tag=warpedjavaguy" alt=" " />View Reactions</a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/warpedjavaguy.wordpress.com/31/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/warpedjavaguy.wordpress.com/31/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/warpedjavaguy.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/warpedjavaguy.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/warpedjavaguy.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/warpedjavaguy.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/warpedjavaguy.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/warpedjavaguy.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/warpedjavaguy.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/warpedjavaguy.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/warpedjavaguy.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/warpedjavaguy.wordpress.com/31/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpedjavaguy.wordpress.com&blog=809469&post=31&subd=warpedjavaguy&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://warpedjavaguy.wordpress.com/2008/02/28/closure-syntax-wars/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b5c8e528c21ff5b742756afdef7a9219?s=96&#38;d=identicon" medium="image">
			<media:title type="html">WarpedJavaGuy</media:title>
		</media:content>

		<media:content url="http://s9.addthis.com/button1-bm.gif" medium="image">
			<media:title type="html">AddThis Social Bookmark Button</media:title>
		</media:content>

		<media:content url="http://s9.addthis.com/button1-fd.gif" medium="image">
			<media:title type="html">AddThis Feed Button</media:title>
		</media:content>

		<media:content url="http://static.technorati.com/static/img/pub/icon-utag-16x13.png?tag=warpedjavaguy" medium="image">
			<media:title type="html"> </media:title>
		</media:content>
	</item>
		<item>
		<title>Pointer Syntax for Java Closures</title>
		<link>http://warpedjavaguy.wordpress.com/2008/02/21/pointer-syntax-for-java-closures/</link>
		<comments>http://warpedjavaguy.wordpress.com/2008/02/21/pointer-syntax-for-java-closures/#comments</comments>
		<pubDate>Thu, 21 Feb 2008 10:51:30 +0000</pubDate>
		<dc:creator>WarpedJavaGuy</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[closures]]></category>

		<guid isPermaLink="false">http://warpedjavaguy.wordpress.com/?p=30</guid>
		<description><![CDATA[
If Java closures are pointers to methods then we should make them look like pointers to methods.
&#160;

Closures for Java are a hot topic yet again.  The BGGA proposal is currently leading in the polls.  It seems that most people who want closures want full closure support.  I like the BGGA proposal, but [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpedjavaguy.wordpress.com&blog=809469&post=30&subd=warpedjavaguy&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><blockquote><p><em><br />
If Java closures are pointers to methods then we should make them look like pointers to methods.<br />
&nbsp;<br />
</em></p></blockquote>
<p>Closures for Java are a hot topic yet again.  The BGGA proposal is currently leading in the <a href="http://java.net/pub/pq/196">polls</a>.  It seems that most people who want closures want full closure support.  I like the BGGA proposal, but I think the syntax can be improved to look more familiar and less verbose.</p>
<p>I generally tend to think of Java closures in the same way that I think of C/C++ function pointers (or member function pointers).  A lot of programmers like myself transitioned from C++ to Java back in 1999.  Java borrows a lot of the syntax from C++ and it was a natural transition for C++ programmers to move to Java.  Given this, I cant help but question whether or not we should adopt the familiar C++ function pointer syntax for Java closures.  That way the syntax (at least) would not be too alien to us.</p>
<p>Borrowing the examples from Stephen Colebourne&#8217;s weblog entry on <a href="http://www.jroller.com/scolebourne/entry/evaluating_bgga_closures">evaluating BGGA</a>, we have the following syntax comparisons:</p>
<p><a href="http://www.javac.info/">BGGA syntax</a>:</p>
<pre name="code" class="java">

int y = 6;
{int =&gt; boolean} a = {int x =&gt; x &lt;= y};
{int =&gt; boolean} b = {int x =&gt; x &gt;= y};
boolean c = a.invoke(3) &amp;&amp; b.invoke(7);
</pre>
<pre name="code" class="java">

public {T =&gt; U} converter({=&gt; T} a, {=&gt; U} b, {T =&gt; U} c) {
  return {T t =&gt; a.invoke().equals(t) ? b.invoke() : c.invoke(t)};
}
</pre>
<p>Java Method Pointer syntax (simplified C/C++ pointer syntax for Java):</p>
<pre name="code" class="java">

int y = 6;
boolean* (int) a = (int x) {x &lt;= y};
boolean* (int) b = (int x) {x &gt;= y};
boolean c = a.invoke(3) &amp;&amp; b.invoke(7);
</pre>
<pre name="code" class="java">

public  U* (T) converter(T* a, U* b, U* (T) c) {
  return (T t) {a.invoke().equals(t) ? b.invoke() : c.invoke(t)};
}
</pre>
<p>In addition to this, we could also borrow the &amp; syntax and reference existing static and instance methods like this:</p>
<pre name="code" class="java">

boolean* (int) a = class&amp;method(int);
boolean* (int) b = instance&amp;method(int);
boolean* (int) c = this&amp;method(int);
</pre>
<p>If we want closures in Java, why not borrow the C/C++ pointer syntax and think of them as pointers to methods if that&#8217;s what they are?  Most of the Java syntax is borrowed from C++ after all.  I understand that function pointers do not fit well in the OO paradigm but I&#8217;m not talking about pointers to functions here.  I&#8217;m instead talking about pointers to methods in the OO world of Java.</p>
<p><!-- AddThis Bookmark Button BEGIN --><a title="Bookmark using any bookmark manager!" href="http://www.addthis.com/bookmark.php?url=http://warpedjavaguy.wordpress.com/2008/02/21/pointer-syntax-for-java-closures/" target="_blank"><img src="http://s9.addthis.com/button1-bm.gif" border="0" alt="AddThis Social Bookmark Button" width="125" height="16" /></a><!-- AddThis Bookmark Button END --> <!-- AddThis Feed Button BEGIN --><a title="Subscribe using any feed reader!" href="http://www.addthis.com/feed.php?pub=warpedjavaguy&amp;h1=http%3A%2F%2Fwarpedjavaguy.wordpress.com%2Ffeed%2F&amp;t1="><img src="http://s9.addthis.com/button1-fd.gif" border="0" alt="AddThis Feed Button" width="125" height="16" /></a><br />
<!-- AddThis Feed Button END --><br />
<a class="tr-linkcount" href="http://technorati.com/search/http://warpedjavaguy.wordpress.com/2008/02/21/pointer-syntax-for-java-closures/"><img style="border:0 none;vertical-align:middle;margin-left:.4em;" src="http://static.technorati.com/static/img/pub/icon-utag-16x13.png?tag=warpedjavaguy" alt=" " />View Reactions</a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/warpedjavaguy.wordpress.com/30/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/warpedjavaguy.wordpress.com/30/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/warpedjavaguy.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/warpedjavaguy.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/warpedjavaguy.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/warpedjavaguy.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/warpedjavaguy.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/warpedjavaguy.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/warpedjavaguy.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/warpedjavaguy.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/warpedjavaguy.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/warpedjavaguy.wordpress.com/30/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpedjavaguy.wordpress.com&blog=809469&post=30&subd=warpedjavaguy&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://warpedjavaguy.wordpress.com/2008/02/21/pointer-syntax-for-java-closures/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b5c8e528c21ff5b742756afdef7a9219?s=96&#38;d=identicon" medium="image">
			<media:title type="html">WarpedJavaGuy</media:title>
		</media:content>

		<media:content url="http://s9.addthis.com/button1-bm.gif" medium="image">
			<media:title type="html">AddThis Social Bookmark Button</media:title>
		</media:content>

		<media:content url="http://s9.addthis.com/button1-fd.gif" medium="image">
			<media:title type="html">AddThis Feed Button</media:title>
		</media:content>

		<media:content url="http://static.technorati.com/static/img/pub/icon-utag-16x13.png?tag=warpedjavaguy" medium="image">
			<media:title type="html"> </media:title>
		</media:content>
	</item>
		<item>
		<title>The Mind of a Programmer</title>
		<link>http://warpedjavaguy.wordpress.com/2008/01/18/the-mind-of-a-programmer/</link>
		<comments>http://warpedjavaguy.wordpress.com/2008/01/18/the-mind-of-a-programmer/#comments</comments>
		<pubDate>Fri, 18 Jan 2008 00:41:59 +0000</pubDate>
		<dc:creator>WarpedJavaGuy</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[learning]]></category>

		<guid isPermaLink="false">http://warpedjavaguy.wordpress.com/2008/01/18/the-mind-of-a-programmer/</guid>
		<description><![CDATA[In my follow up post I delve even deeper into the odd and even number problem and discover a pattern that is common to both humans and machines.  A natural born programmer already knows that.  I am not one of them.  But I am pleased and grateful to have learned about them here :)  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpedjavaguy.wordpress.com&blog=809469&post=29&subd=warpedjavaguy&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><blockquote><p><em><br />
Good programmers are good at solving problems because they consider all the dimensions.<br />
&nbsp;<br />
</em></p></blockquote>
<p>A short while ago I published a post about <a href="http://warpedjavaguy.wordpress.com/2007/06/20/natural-born-programmers/">natural born programmers</a> and questioned whether or not they exist.  When I was writing it I was thinking about how programmers go about coding solutions to problems that they already know how to solve. I was trying to tap into the mind of a programmer and discover how it thinks.  I demonstrated two approaches to solving the simple problem of determining whether a number is odd or even.  One solution used a &#8217;smart&#8217; human approach and the other a &#8217;simple&#8217; math approach.  It generated some good discussions and triggered <a href="http://www.atomicmpc.com.au/forums.asp?s=2&amp;c=10&amp;t=4311">various</a> <a href="http://technorati.com/search/http://warpedjavaguy.wordpress.com/2007/06/20/natural-born-programmers/">reactions</a>.  The most interesting one was <a href="http://programming.reddit.com/info/203sv/comments/">this one</a>.  It discusses <a href="http://warpedjavaguy.wordpress.com/2007/06/20/natural-born-programmers/#comment-3">my first response</a> to the <a href="http://warpedjavaguy.wordpress.com/2007/06/20/natural-born-programmers/#comment-2">first comment</a> posted by Matt Turner.</p>
<p>The simple math solution that I suggested was this:</p>
<pre name="code" class="java">

// if divisible by 2 then is even else is odd
boolean isEven = number % 2 == 0;
</pre>
<p>The alternative and bitwise solution that Matt suggested was this:</p>
<pre name="code" class="java">

// if last bit is 0 then is even else is odd
boolean isEven = (number &amp; 1) == 0;
</pre>
<p>I knew that the &amp;1 solution was functionally equivalent to the %2 solution but I wasn&#8217;t exactly sure if it was also equally as efficient (or even better).  I had to prove it.  So I knocked up a quick test program to compare the execution times.  When I ran the program I observed that both of them did take the same time to execute.  I also analysed the generated bytecode and found that the %2 solution used the IREM bytecode instruction and that the &amp;1 operation used the IAND instruction.  My bytecode findings attracted <a href="http://warpedjavaguy.wordpress.com/2007/06/20/natural-born-programmers/#comment-9">these</a> <a href="http://warpedjavaguy.wordpress.com/2007/06/20/natural-born-programmers/#comment-127">comments</a> by Michael Speer and Charlie Chan respectively (and <a href="http://warpedjavaguy.wordpress.com/2007/06/20/natural-born-programmers/#comment-21">other anonymous comments</a> too).  Having done a bit of assembler, C, and C++ in my not too old school days, I do have some experience in binary arithmetic and native code optimisation.  So I can appreciate the &amp;1 solution.  But I never expect to have to apply this knowledge directly to Java programming.  I always expect Java to perform all trivial optimisations on my behalf.  I should not have to optimise my Java code at this level.  I should just write code that expresses what I want in the simplest and clearest way possible.</p>
<p>It has often been said by many programmers that there is no one right solution to a problem.  That is definitely the case here.  But there is something more interesting here though.  If you study the &amp;1 solution you will find that it very closely resembles the &#8217;smart&#8217; human optimised approach to solving the problem.  How?  Remember that smart humans can tell whether a number is even or odd just by looking at the last digit.  The &amp;1 solution emulates exactly that, yes indeed!  The difference is that humans look at the right-most digit whereas machines look at the right-most bit.  We have identified a pattern here that is common to both humans and machines.  Interesting!</p>
<p>But wait!  The %2 approach is a more natural and readable expression of the mathematical definition and is also more easily understood by us humans as adopters of the base 10 decimal number system.  It directly expresses at a high level the definition that all even numbers are divisible by two and that all odd numbers are not.  At the lower level it expresses it as an IREM bytecode instruction. At the even lower machine code level it expresses it as native assembly instructions.  We are now in the world of base 2 binary numbers where operations like &#8216;divide by two&#8217; involve shifting all bits one place to the right and carrying the right most bit (the remainder).  Bits can be checked, set, and toggled with bitwise AND, OR, and XOR operations.  At this stage, the %2 instruction is optimised for the machine and more closely resembles the &amp;1 operation.  So at the binary level, one can deduce that the %2 operation is essentially equivalent to &amp;1.</p>
<p>By solving problems and comparing solutions we are able to identify common patterns.  Gifted programmers are naturally good at it!  These are the things we learn and the things that go on in our minds as programmers.</p>
<p><!-- AddThis Bookmark Button BEGIN --><a href="http://www.addthis.com/bookmark.php?url=http://warpedjavaguy.wordpress.com/2008/01/18/the-mind-of-a-programmer/" title="Bookmark using any bookmark manager!" target="_blank"><img src="http://s9.addthis.com/button1-bm.gif" alt="AddThis Social Bookmark Button" border="0" height="16" width="125" /></a><!-- AddThis Bookmark Button END --> <!-- AddThis Feed Button BEGIN --><a href="http://www.addthis.com/feed.php?pub=warpedjavaguy&amp;h1=http%3A%2F%2Fwarpedjavaguy.wordpress.com%2Ffeed%2F&amp;t1=" title="Subscribe using any feed reader!"><img src="http://s9.addthis.com/button1-fd.gif" alt="AddThis Feed Button" border="0" height="16" width="125" /></a><!-- AddThis Feed Button END --></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/warpedjavaguy.wordpress.com/29/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/warpedjavaguy.wordpress.com/29/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/warpedjavaguy.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/warpedjavaguy.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/warpedjavaguy.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/warpedjavaguy.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/warpedjavaguy.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/warpedjavaguy.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/warpedjavaguy.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/warpedjavaguy.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/warpedjavaguy.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/warpedjavaguy.wordpress.com/29/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpedjavaguy.wordpress.com&blog=809469&post=29&subd=warpedjavaguy&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://warpedjavaguy.wordpress.com/2008/01/18/the-mind-of-a-programmer/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b5c8e528c21ff5b742756afdef7a9219?s=96&#38;d=identicon" medium="image">
			<media:title type="html">WarpedJavaGuy</media:title>
		</media:content>

		<media:content url="http://s9.addthis.com/button1-bm.gif" medium="image">
			<media:title type="html">AddThis Social Bookmark Button</media:title>
		</media:content>

		<media:content url="http://s9.addthis.com/button1-fd.gif" medium="image">
			<media:title type="html">AddThis Feed Button</media:title>
		</media:content>
	</item>
		<item>
		<title>Test Driven Rewrites (for Programmers)</title>
		<link>http://warpedjavaguy.wordpress.com/2007/12/20/test-driven-rewrites-for-programmers/</link>
		<comments>http://warpedjavaguy.wordpress.com/2007/12/20/test-driven-rewrites-for-programmers/#comments</comments>
		<pubDate>Thu, 20 Dec 2007 10:54:41 +0000</pubDate>
		<dc:creator>WarpedJavaGuy</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[aop mocks testing]]></category>

		<guid isPermaLink="false">http://warpedjavaguy.wordpress.com/2007/12/20/test-driven-rewrites-for-programmers/</guid>
		<description><![CDATA[
Why write mock objects manually when you can emulate them virtually?
&#160;

In my previous post on Test Driven Rewrites I described at a high level how I used virtual mock objects to deliver a regression test suite for an existing component and how I used that suite to test and develop a rewrite of that component. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpedjavaguy.wordpress.com&blog=809469&post=28&subd=warpedjavaguy&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><blockquote><p><i><br />
Why write mock objects manually when you can emulate them virtually?<br />
&nbsp;<br />
</i></p></blockquote>
<p>In my previous post on <a href="http://warpedjavaguy.wordpress.com/2007/12/11/test-driven-rewrites/">Test Driven Rewrites</a> I described at a high level how I used <a href="http://www.xprogramming.com/xpmag/virtualMockObjects.htm">virtual mock objects</a> to deliver a regression test suite for an existing component and how I used that suite to test and develop a rewrite of that component.  In it you&#8217;ll recall that the existing codebase had no test suite at all and that I had very little knowledge of the business domain.  I had to come up with a quick and easy way to create a test suite from scratch.  Here I present some code that shows how I did it using <a href="http://www.eclipse.org/aspectj/">AspectJ</a>, XML, and <a href="http://www.junit.org/">JUnit</a>.</p>
<p>The first thing I had to do was identify the following points in the codebase:</p>
<ol>
<li>The test point
<ul>
<li>This is the point in the application where the call to the component is made.  It is the call to the component method that will be put under test.</li>
</ul>
</li>
<li>All the mock points
<ul>
<li>These are all the points in the component where calls are made to other components and/or services.  They are the calls to the methods that need to be mocked.</li>
</ul>
</li>
</ol>
<p>Next, a TestCaseGenerator aspect was written to capture the arguments and return values of the test and mock points identified above.  This aspect defined two <a href="http://www.eclipse.org/aspectj/doc/released/progguide/semantics-pointcuts.html">point cuts</a> for matching the test and mock points respectively. &#8220;Around&#8221; <a href="http://www.eclipse.org/aspectj/doc/released/progguide/semantics-advice.html">advice</a> was used on each to capture and persist the arguments and return values of each call to an XML file.  A MethodCall POJO was created to encapsulate the name, arguments, and return values of individual method calls and <a href="http://castor.codehaus.org/xml-framework.html">Castor XML</a> was used to marshal it to XML.  The aspect code is shown below:</p>
<pre name="code" class="java">

public aspect TestCaseGenerator {

    pointcut testPoint() :
        execution (/* test-point method pattern */);

    pointcut mockPoints() :
        call (/* mock-points method pattern */)
        &amp;&amp; withincode(/* test-point method pattern*/);

    // output XML document
    private Document xmldoc;

    Object around() : testPoint() {

        /* instantiate new XML document here */

        // encapsulate invoked method data into POJO
        MethodCall methodCall = new MethodCall();
        methodCall.setMethod(
            thisJoinPoint.getSignature().toString());
        methodCall.setArgs(thisJoinPoint.getArgs());
        Object retValue = proceed();
        methodCall.setRetValue(retValue);

        /* marshal POJO into test-point XML element here */

        /* persist XML to file here */

        // pass back return value
        return retValue;
    }

    Object around() : mockPoints() {

        // encapsulate invoked method data into POJO
        MethodCall methodCall = new MethodCall();
        methodCall.setMethod(
            thisJoinPoint.getSignature().toString());
        methodCall.setArgs(thisJoinPoint.getArgs());
        Object retValue = proceed();
        methodCall.setRetValue(retValue);

        /* marshal POJO into mock-point XML element here */

        // pass back return value
        return retValue;
    }
}
</pre>
<p>This aspect was woven into the existing codebase which was then deployed locally.  So now an XML test case file was automatically generated by the aspect on the file system for every scenario that was executed on the deployed application.  The business identified all the scenarios that needed to be tested and a test case XML file was generated for each one in real time as it was executed on the system by a user.  The basic structure of the XML that was generated is shown below:</p>
<pre name="code" class="xml">

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;&gt;
&lt;test-case&gt;
    &lt;test-point method=&quot;method pattern&quot;&gt;
        &lt;arguments&gt;
            ...
        &lt;/arguments&gt;
        &lt;return-value&gt;
            ...
        &lt;/return-value&gt;
    &lt;/test-point&gt;
    &lt;mock-point method=&quot;method pattern&quot;&gt;
        &lt;arguments&gt;
            ...
        &lt;/arguments&gt;
    &lt;/mock-point&gt;
    &lt;mock-point method=&quot;method pattern&quot;&gt;
        &lt;arguments&gt;
            ...
        &lt;/arguments&gt;
        &lt;return-value&gt;
            ...
        &lt;/return-value&gt;
    &lt;/mock-point&gt;
    ...
&lt;/test-case&gt;
</pre>
<p>Once the data for all the known use case scenarios had been captured in XML files, it was then time to start writing the JUnit test suite that would execute the tests.  This involved creating the following:</p>
<ol>
<li>A MockedTestCase class (extends JUnit TestCase)</li>
<li>A MockedTestSuite class (extends JUnit TestCase)</li>
<li>A VirtualMocker aspect</li>
</ol>
<p>The MockedTestCase class was implemented with a constructor that accepted a given test case XML file.  All the mock point data in the file was loaded into individual MethodCall POJO instances and stored in a Map keyed by the method signature pattern.  The map was bound to a thread local variable to make it thread safe and accessible to the virtual mocker aspect for mocking purposes.  The test point data was used to invoke the component in the test and to assert the returned result.  The Java code is shown below:</p>
<pre name="code" class="java">

public class MockedTestCase extends TestCase {

    public static final
        ThreadLocal&lt;Map&lt;String,MethodCall&gt;&gt; MOCKS =
           new ThreadLocal&lt;Map&lt;String,MethodCall&gt;&gt;();

    private File xmlFile;
    private MethodCall testPoint;

    public MockedTestCase(File xmlFile) {
        super(&quot;testIt&quot;);
        this.xmlFile = xmlFile;
    }

    // overrides super to return test case XML file name
    public String getName() {
        return xmlFile.getName();
    }

    protected void setUp() throws Exception {

        testPoint = new MethodCall();
        /* Unmarshal XML test-point into POJO */

        Map&lt;String,MethodCall&gt; mocks =
            new HashMap&lt;String,MethodCall&gt;();
        /* Unmarshal XML mock-points and load into Map */

        // store loaded mocks in thread local var
        MOCKS.set(mocks);
    }

    public void testIt() throws Exception {

        // invoke the test
        // - pass in test-point args and capture the result
        Object args = testPoint.getArgs();
        Object result = /* invocation goes here */;

        // assert the result
        assertValues(
            &quot;Unexpected result returned by &quot;
                + testPoint.getMethod(),
            testPoint.getRetValue(),
            result);
    }

    public static void assertValues(
        String msg, Object expected, Object actual) {
        /* Compare the two values here and throw assertion
            error if not the same. One generic way of doing
            this might involve marshaling both objects to XML
            and then comparing the two with an XML diff utility. */
    }
}
</pre>
<p>The MockedTestSuite class was implemented to load and run every test case.  This was done by loading all the XML test case files from the file system, instantiating individual MockedTestCase instances for each one, and adding them all to the suite of tests to be executed.</p>
<p>The VirtualMocker aspect was defined to intercept all the mock-points using an &#8220;around&#8221; <a href="http://www.eclipse.org/aspectj/doc/released/progguide/semantics-advice.html">advice</a> which asserted the incoming arguments against the mocked arguments for the call before returning the mocked return value.  Access to all mock point data was provided in a Map contained in the thread local variable as loaded by the currently executing MockedTestCase.  The aspect code is shown below:</p>
<pre name="code" class="java">

public aspect VirtualMocker {

    pointcut mockPoints() :
        call (/* mock-points method pattern */)
        &amp;&amp; withincode(/*test-point method pattern*/);

    Object around() : mockPoints() {

        // retrieve mock data
        Map&lt;String,MethodCall&gt; mocks =
            MockedTestCase.MOCKS.get();
        MethodCall mock = mocks.get(
            thisJoinPoint.getSignature().toString());

        // assert input and return output
        MockedTestCase.assertValues(
            &quot;Unexpected input arguments passed to &quot;
                + mock.getMethod(),
            mock.getArgs(),
            thisJoinPoint.getArgs());
        return mock.getRetValue();
    }
}
</pre>
<p>The VirtualMocker aspect was woven into the existing codebase and the MockedTestSuite was executed over it to perform all the tests.  The same aspect was then woven into the new codebase and used to facilitate the test driven development of the rewrite.</p>
<p>Each XML test case file contained all the mock data that was required to test the same scenario that generated it.  The VirtualMocker aspect used the data to do all the mocking in all the tests.  No mock objects or test data fixtures were manually coded <img src='http://s.wordpress.com/wp-includes/images/smilies/face-smile.png' alt=':)' class='wp-smiley' /><br />
&nbsp;<br />
<!-- AddThis Bookmark Button BEGIN --><a href="http://www.addthis.com/bookmark.php?url=http://warpedjavaguy.wordpress.com/2007/12/20/test-driven-rewrites-for-programmers/" title="Bookmark using any bookmark manager!" target="_blank"><img src="http://s9.addthis.com/button1-bm.gif" alt="AddThis Social Bookmark Button" border="0" height="16" width="125" /></a> <!-- AddThis Feed Button BEGIN --><a href="http://www.addthis.com/feed.php?pub=warpedjavaguy&amp;h1=http%3A%2F%2Fwarpedjavaguy.wordpress.com%2Ffeed%2F&amp;t1=" title="Subscribe using any feed reader!"><img src="http://s9.addthis.com/button1-fd.gif" alt="AddThis Feed Button" border="0" height="16" width="125" /></a><!-- AddThis Feed Button END --></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/warpedjavaguy.wordpress.com/28/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/warpedjavaguy.wordpress.com/28/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/warpedjavaguy.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/warpedjavaguy.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/warpedjavaguy.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/warpedjavaguy.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/warpedjavaguy.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/warpedjavaguy.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/warpedjavaguy.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/warpedjavaguy.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/warpedjavaguy.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/warpedjavaguy.wordpress.com/28/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpedjavaguy.wordpress.com&blog=809469&post=28&subd=warpedjavaguy&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://warpedjavaguy.wordpress.com/2007/12/20/test-driven-rewrites-for-programmers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b5c8e528c21ff5b742756afdef7a9219?s=96&#38;d=identicon" medium="image">
			<media:title type="html">WarpedJavaGuy</media:title>
		</media:content>

		<media:content url="http://s9.addthis.com/button1-bm.gif" medium="image">
			<media:title type="html">AddThis Social Bookmark Button</media:title>
		</media:content>

		<media:content url="http://s9.addthis.com/button1-fd.gif" medium="image">
			<media:title type="html">AddThis Feed Button</media:title>
		</media:content>
	</item>
		<item>
		<title>Test Driven Rewrites</title>
		<link>http://warpedjavaguy.wordpress.com/2007/12/11/test-driven-rewrites/</link>
		<comments>http://warpedjavaguy.wordpress.com/2007/12/11/test-driven-rewrites/#comments</comments>
		<pubDate>Tue, 11 Dec 2007 12:01:27 +0000</pubDate>
		<dc:creator>WarpedJavaGuy</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[aop]]></category>
		<category><![CDATA[mocks]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://warpedjavaguy.wordpress.com/2007/12/11/test-driven-rewrites/</guid>
		<description><![CDATA[
Rewritten applications are best tested in the most laziest ways possible.
&#160;

Rewriting existing applications or components can be a risky exercise.  Consider an existing component that provides critical business functionality to an enterprise.  The component has been tweaked and fine tuned over the years and has evolved to provide the exact functionality required by [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpedjavaguy.wordpress.com&blog=809469&post=27&subd=warpedjavaguy&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><blockquote><p><em><br />
Rewritten applications are best tested in the most laziest ways possible.<br />
&nbsp;<br />
</em></p></blockquote>
<p>Rewriting existing applications or components can be a risky exercise.  Consider an existing component that provides critical business functionality to an enterprise.  The component has been tweaked and fine tuned over the years and has evolved to provide the exact functionality required by the business.  It is a core component that consists of very fine grained and delicate operations and it is used by multiple applications.  Although it functions correctly, a rewrite has been requested after a review identified some major performance and maintainability issues.  The functions were taking too long to execute and it was becoming too difficult to add, modify, or replace any functionality without incurring negative side effects.  The component had become very volatile and susceptible to change.</p>
<p>I was part of a team that was given the task of rewriting such a component.  My role was to come up with an automated regression test suite that would test both the old and the new software.  The first problem I had was that there was no current test suite at all that I could use as a base.  I was not familiar with the software at all and my general understanding of the business and its domain was very poor.  I was a newbie that had just joined the project and I was thrown into the deep end.  I had to quickly find a way to start creating tests that would cover all possible scenarios.  In my mind I was thinking &#8220;I wish I had a test generator&#8221;.  That way I could then get a business person to run through all the scenarios in the application and have the tests automatically generated on the file system (including mock data, assertions, and all).  The same tests could then be used to test the rewrite.  So I set upon creating a test generator.  It wasn&#8217;t going to be easy but I knew that once I had it going that the remaining exercise of running the same tests over the rewritten code base would be a breeze.  It was a challenge that I could not refuse and failure was not an option.</p>
<p>I immediately started researching mocking frameworks and was looking for one that would involve minimal overhead from a coding perspective.  I wanted one that would help generate all the mock objects and data that I would need for all my tests.  The last thing I wanted to do was to have to manually start coding mock objects and data fixtures.  I wanted all the data to be automatically captured and all the tests to be automatically generated.  After about half an hour of researching online, I stumbled upon this beauty:</p>
<blockquote><p> <a href="http://www.xprogramming.com/xpmag/virtualMockObjects.htm">Virtual Mock Objects using AspectJ with JUNIT</a></p>
<p>Using aspect-oriented programming in AspectJ to facilitate isolation of testable units, without hand-crafting Mock Objects or using a specialized Mock Object generation tool.</p></blockquote>
<p>With aspects you can intercept method invocations and access the input parameters and return values both before and after the call.  This was exactly what I needed.  The existing component we were replacing made several calls to a rules engine and other EJB services.  I immediately realised that I could write an aspect to intercept those calls and capture the data going in and the data coming out.  I could then persist this data to an XML file and have all the mock data I need for all my tests.  So I wrote a test generator aspect that captured all the data going in and out of the calls that I needed to mock.  I separated the input and output data of each method call into separate XML elements in the generated file and associated them with the call.  I weaved the aspect into the code and asked a business person to use the application as they normally do and run some scenarios.  For each scenario that they ran an XML test file was automatically generated in real time on the file system.  The existing code was not changed at all.  The generator aspect was simply woven into it.</p>
<p>The test case generator was complete and all test cases were identified by the business and generated in less than two weeks.  We managed to generate some 600+ test cases in that time.  It was now time to start writing the JUnit test runner and virtual mocker aspect using the pattern described in the virtual mock objects article (quoted above).  The mocker aspect was written to intercept every method invocation for which mock data was provided in the generated XML test files.  It asserted the input parameters passed to each mocked call and overrode the method to return the output for that call as captured in the XML test file.  The aspect was weaved into the existing code and a JUnit test runner was written to invoke the component using the captured test input and to assert the returned result against the captured test output.   The aspect handled all the mocked calls in between.  Again, the existing code was not modified in anyway and the virtual mocker aspect was simply woven into it.  As expected all the tests passed when executed over the existing code base.  I now had a complete regression test suite that I could use to test both the existing and newly rewritten component that the rest of the team was busy developing.  I had some time on my hands and helped them complete it.</p>
<p>When the rewrite was ready for testing I weaved the same virtual mocker aspect into the new code and ran the same tests using the same JUnit test runner.  The tests all ran very efficiently and fast and about 60% of them passed first go!  The other 40% that failed were due to various bugs that were introduced into the new code, missing logic, and other miscellaneous anomalies.  It took less than a week to fix the new code and achieve a 100% test pass rate.  That 40% of the rewrite was completely test driven.  The entire exercise was a huge success and the tests were integrated into the automated build process.  The entire test suite was recorded in XML form and it could be used to successfully verify both the old and rewritten component.  The rewritten component continued to evolve and the tests were kept in sync.  Whenever the data model changed the tests were also updated.  XSL stylesheets made it easy to transform and restructure the data contained in all XML test files as required.</p>
<p>When it came to running the tests in isolation though, empty stub implementations of all EJB interfaces had to be used.  Those empty stubs were the only little bit of manual coding we had to do.  But as far as mocking goes, nothing was manually coded.  Using aspects in this way made it possible to test both existing and rewritten code &#8220;as is&#8221; without having to manually write any mock objects or test data fixtures <img src='http://s.wordpress.com/wp-includes/images/smilies/face-smile.png' alt=':)' class='wp-smiley' /><br />
&nbsp;<br />
<!-- AddThis Bookmark Button BEGIN --><a href="http://www.addthis.com/bookmark.php?url=http://warpedjavaguy.wordpress.com/2007/12/11/test-driven-rewrites/" title="Bookmark using any bookmark manager!" target="_blank"><img src="http://s9.addthis.com/button1-bm.gif" alt="AddThis Social Bookmark Button" border="0" height="16" width="125" /></a><!-- AddThis Bookmark Button END --> <!-- AddThis Feed Button BEGIN --><a href="http://www.addthis.com/feed.php?pub=warpedjavaguy&amp;h1=http%3A%2F%2Fwarpedjavaguy.wordpress.com%2Ffeed%2F&amp;t1=" title="Subscribe using any feed reader!"><img src="http://s9.addthis.com/button1-fd.gif" alt="AddThis Feed Button" border="0" height="16" width="125" /></a><!-- AddThis Feed Button END --></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/warpedjavaguy.wordpress.com/27/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/warpedjavaguy.wordpress.com/27/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/warpedjavaguy.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/warpedjavaguy.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/warpedjavaguy.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/warpedjavaguy.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/warpedjavaguy.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/warpedjavaguy.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/warpedjavaguy.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/warpedjavaguy.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/warpedjavaguy.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/warpedjavaguy.wordpress.com/27/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpedjavaguy.wordpress.com&blog=809469&post=27&subd=warpedjavaguy&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://warpedjavaguy.wordpress.com/2007/12/11/test-driven-rewrites/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b5c8e528c21ff5b742756afdef7a9219?s=96&#38;d=identicon" medium="image">
			<media:title type="html">WarpedJavaGuy</media:title>
		</media:content>

		<media:content url="http://s9.addthis.com/button1-bm.gif" medium="image">
			<media:title type="html">AddThis Social Bookmark Button</media:title>
		</media:content>

		<media:content url="http://s9.addthis.com/button1-fd.gif" medium="image">
			<media:title type="html">AddThis Feed Button</media:title>
		</media:content>
	</item>
		<item>
		<title>The Day I Timed Out</title>
		<link>http://warpedjavaguy.wordpress.com/2007/10/17/the-day-i-timed-out/</link>
		<comments>http://warpedjavaguy.wordpress.com/2007/10/17/the-day-i-timed-out/#comments</comments>
		<pubDate>Wed, 17 Oct 2007 12:34:21 +0000</pubDate>
		<dc:creator>WarpedJavaGuy</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[interviews]]></category>

		<guid isPermaLink="false">http://warpedjavaguy.wordpress.com/2007/10/17/the-day-i-timed-out/</guid>
		<description><![CDATA[
There is no more time left for waiting after timing out.
&#160;

I work as a contractor (freelancer) and am always on the lookout for new and interesting job opportunities.  Not long ago I found and applied for a Java developer role that was advertised online.  My current contract was about to expire and so [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpedjavaguy.wordpress.com&blog=809469&post=23&subd=warpedjavaguy&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><blockquote><p><em><br />
There is no more time left for waiting after timing out.<br />
&nbsp;<br />
</em></p></blockquote>
<p>I work as a contractor (freelancer) and am always on the lookout for new and interesting job opportunities.  Not long ago I found and applied for a Java developer role that was advertised online.  My current contract was about to expire and so it was the ideal time to actively start looking around.  There was no real need for me to do so though because my current client had already offered me a renewal.  But I could not resist a new opportunity and I had not yet agreed to accept the renewal.  It was the perfect time to make my next transition.  That&#8217;s what us contractors do.  We have the freedom of choice and we move around.  It&#8217;s the beauty of contracting.</p>
<p>The next morning on my way to work I got a phone call from the recruiter.  It was about 8:30 AM and I had just bought my usual morning coffee.  Normally I would sit down and drink my coffee in peace without any distractions but I made an exception in this case.  The conversation I had was a thirty minute phone interview.  I didn&#8217;t enjoy my coffee the way I normally would have liked to but I did manage to secure a face to face interview for 12:30 PM the same day.  And that&#8217;s always a good thing.</p>
<p>Now, 12:30 PM is usually when I prefer to go out for lunch but I was willing to sacrifice that too.  I promptly arrived for the interview and the receptionist lead me to a waiting room.  It was a tiny room that had no windows in it.  There was one small round table with a form and a pen on it and there were three chairs.  I was asked to sit down and fill in the form and wait for the interviewer. I filled in the form in five minutes and sat there waiting.  Five more minutes passed and I was still sitting there waiting.  Yet another five minutes passed and I was yet still sitting there waiting!  I was there for 15 minutes all alone without anyone to speak to or even a glass of water to drink.  I am not one for demanding respect, but a little bit of hospitality would have been nice.  So there I was sitting there all alone and doing nothing.  By this time both my patience and respect for that recruiter had ran out.  I didn&#8217;t really need to be there and I was not desperate for the job either.  I flipped over the form that I had filled in and wrote the following message on the back of it:</p>
<pre name="code" class="xml">

&lt;timed-out millisecs=&quot;900000&quot;/&gt;
</pre>
<p>I left it on the table and walked straight out.  Yes, that&#8217;s right.  I had timed out and I knew it.</p>
<p>Some two minutes later as I was walking away from the building, my phone rang.  It was the interviewer.  I didn&#8217;t answer it.  I grabbed a quick lunch and went back to my current workplace, signed into my computer, and resumed working.  I soon received an email message that read:</p>
<blockquote><p> I am sorry for the mix-up that occurred today with your interview. Our reception did not notify me of your arrival and by the time I checked to see if you had arrived, you had left. Is there a chance we could re-schedule for tomorrow?</p>
<p>Please accept my apologies for keeping you waiting today.</p></blockquote>
<p>To which I replied:</p>
<blockquote><p> Sorry I left the way I did.  I guess I&#8217;m just a little bit impatient sometimes.   In the meantime I&#8217;ve decided to accept the renewal offered to me by my current client.</p>
<p>I wish you good luck in filling the position.</p></blockquote>
<p>&nbsp;<br />
<!-- AddThis Bookmark Button BEGIN --><a href="http://www.addthis.com/bookmark.php?url=http://warpedjavaguy.wordpress.com/2007/10/17/the-day-i-timed-out/" title="Bookmark using any bookmark manager!" target="_blank"><img src="http://s9.addthis.com/button1-bm.gif" alt="AddThis Social Bookmark Button" border="0" height="16" width="125" /></a><!-- AddThis Bookmark Button END --> <!-- AddThis Feed Button BEGIN --><a href="http://www.addthis.com/feed.php?pub=warpedjavaguy&amp;h1=http%3A%2F%2Fwarpedjavaguy.wordpress.com%2Ffeed%2F&amp;t1=" title="Subscribe using any feed reader!"><img src="http://s9.addthis.com/button1-fd.gif" alt="AddThis Feed Button" border="0" height="16" width="125" /></a><!-- AddThis Feed Button END --></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/warpedjavaguy.wordpress.com/23/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/warpedjavaguy.wordpress.com/23/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/warpedjavaguy.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/warpedjavaguy.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/warpedjavaguy.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/warpedjavaguy.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/warpedjavaguy.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/warpedjavaguy.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/warpedjavaguy.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/warpedjavaguy.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/warpedjavaguy.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/warpedjavaguy.wordpress.com/23/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpedjavaguy.wordpress.com&blog=809469&post=23&subd=warpedjavaguy&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://warpedjavaguy.wordpress.com/2007/10/17/the-day-i-timed-out/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b5c8e528c21ff5b742756afdef7a9219?s=96&#38;d=identicon" medium="image">
			<media:title type="html">WarpedJavaGuy</media:title>
		</media:content>

		<media:content url="http://s9.addthis.com/button1-bm.gif" medium="image">
			<media:title type="html">AddThis Social Bookmark Button</media:title>
		</media:content>

		<media:content url="http://s9.addthis.com/button1-fd.gif" medium="image">
			<media:title type="html">AddThis Feed Button</media:title>
		</media:content>
	</item>
		<item>
		<title>Renegading Java Classics</title>
		<link>http://warpedjavaguy.wordpress.com/2007/10/10/renegading-java-classics/</link>
		<comments>http://warpedjavaguy.wordpress.com/2007/10/10/renegading-java-classics/#comments</comments>
		<pubDate>Wed, 10 Oct 2007 01:33:54 +0000</pubDate>
		<dc:creator>WarpedJavaGuy</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[deprecation]]></category>

		<guid isPermaLink="false">http://warpedjavaguy.wordpress.com/2007/10/10/renegading-java-classics/</guid>
		<description><![CDATA[
Anything that is deprecated and superseded can be considered classic and should be renegaded.
&#160;

The Java API has undergone many changes since the original 1.0 and classic 1.1 and 1.2 versions.  The changes include both enhancements and deprecations.  Any classes, interfaces, and methods that have been marked as deprecated should no longer be used. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpedjavaguy.wordpress.com&blog=809469&post=22&subd=warpedjavaguy&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><blockquote><p><em><br />
Anything that is deprecated and superseded can be considered classic and should be renegaded.<br />
&nbsp;<br />
</em></p></blockquote>
<p>The Java API has undergone many changes since the original 1.0 and <em>classic</em> 1.1 and 1.2 versions.  The changes include both enhancements and deprecations.  Any classes, interfaces, and methods that have been marked as deprecated should no longer be used.  They are supported for backwards compatibility purposes and are likely to never be removed from future Java releases.  It is recommended (but not enforced) that programmers never use them in their programs.  I can understand the rationale behind this.  Backwards compatibility is important and all reasonable measures should be taken to ensure that it is always preserved.  Deprecation provides a means of providing this compatibility.  The problem is that in doing so, it forever supports something that is deprecated without strictly enforcing that it no longer be used.</p>
<p>I would like to propose the idea of introducing a new and specialised type of deprecation called <em>renegation</em> whereby anything that is superseded and unsupported can be physically demoted to &#8216;classic&#8217; status and not be supported in source code but in bytecode only.  A Java compiler that supports this would error when attempting to compile any code that makes any direct use of anything marked renegaded.  Anything that is already marked and compiled as renegaded would be fully supported at runtime but strictly controlled at compile time.  This would ensure that all bytecode remains valid and that any source code that does not have any compile time dependencies on anything marked renegaded still compiles.  In this way, all newly written and compiled code would be clean and free of any renegaded code and will still be able to make use of any new or existing precompiled binaries.  Programmers would then be forced to not write or compile any code that makes direct use of anything that is renegaded.  They would be forced to use the preferred alternatives instead.  Compatibility would still be supported through binaries and any existing code that requires recompilation would have to be updated to ensure that all renegaded code fragments are replaced with the preferred alternatives first.</p>
<p>Renegation should not have any impact on deprecation and should not be applied to everything that is already deprecated.  It should instead only be applied to types that may or may not already be deprecated and are suitable because they are obsolete and superseded by preferred alternatives (like the <a href="http://java.sun.com/javase/6/docs/api/java/awt/List.html#clear()">java.awt.List.clear()</a> method for example).  Furthermore, it would also make it easier to deprecate and avoid using certain &#8216;classic&#8217; types that have not yet been deprecated but should have been (like the <a href="http://java.sun.com/javase/6/docs/api/java/util/Enumeration.html">java.util.Enumeration</a> interface for example).  The Enumeration interface is a primary candidate for renegation, especially when you consider that it is only reactively used and has a special note included in it&#8217;s Javadoc comments stating that <a href="http://java.sun.com/javase/6/docs/api/java/util/Iterator.html">Iterators</a> should be considered in preference.</p>
<blockquote><p> <strong>Enumeration Javadoc comment: </strong></p>
<p>NOTE: The functionality of this interface is duplicated by the Iterator interface. In addition, Iterator adds an optional remove operation, and has shorter method names. New implementations should consider using Iterator in preference to Enumeration.</p></blockquote>
<p>In order to support renegation and preserve deprecation, the existing <a href="http://java.sun.com/javase/6/docs/api/java/lang/Deprecated.html">@Deprecated annotation</a> would need to be extended, or a new core Java annotation type or type modifier would need to be introduced.</p>
<p>Here are some suggestions:</p>
<pre name="code" class="java">

/* Option 1:
Extending the existing @Deprecated annotation type */

@Deprecated (renegation = &quot;classic&quot;)
public interface Enumeration&lt;E&gt; {
...
}

/* Option 2:
Introducing a new @Renegaded annotation type */

@Renegaded (status = &quot;classic&quot;)
public interface Enumeration&lt;E&gt; {
...
}

/* Option 3:
Introducing a new classic type modifier */

public classic interface Enumeration&lt;E&gt; {
...
}
</pre>
<p>Of course, renegation should be supported at the type, method, and attribute levels.  Its use should not just be limited to just the Java API either.  Any code in any codebase should be able to be renegaded if deemed suitable.<br />
&nbsp;<br />
<!-- AddThis Bookmark Button BEGIN --><a href="http://www.addthis.com/bookmark.php?url=http://warpedjavaguy.wordpress.com/2007/10/10/renegading-java-classics/" title="Bookmark using any bookmark manager!" target="_blank"><img src="http://s9.addthis.com/button1-bm.gif" alt="AddThis Social Bookmark Button" border="0" height="16" width="125" /></a><!-- AddThis Bookmark Button END --> <!-- AddThis Feed Button BEGIN --><a href="http://www.addthis.com/feed.php?pub=warpedjavaguy&amp;h1=http%3A%2F%2Fwarpedjavaguy.wordpress.com%2Ffeed%2F&amp;t1=" title="Subscribe using any feed reader!"><img src="http://s9.addthis.com/button1-fd.gif" alt="AddThis Feed Button" border="0" height="16" width="125" /></a><!-- AddThis Feed Button END --></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/warpedjavaguy.wordpress.com/22/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/warpedjavaguy.wordpress.com/22/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/warpedjavaguy.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/warpedjavaguy.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/warpedjavaguy.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/warpedjavaguy.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/warpedjavaguy.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/warpedjavaguy.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/warpedjavaguy.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/warpedjavaguy.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/warpedjavaguy.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/warpedjavaguy.wordpress.com/22/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=warpedjavaguy.wordpress.com&blog=809469&post=22&subd=warpedjavaguy&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://warpedjavaguy.wordpress.com/2007/10/10/renegading-java-classics/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b5c8e528c21ff5b742756afdef7a9219?s=96&#38;d=identicon" medium="image">
			<media:title type="html">WarpedJavaGuy</media:title>
		</media:content>

		<media:content url="http://s9.addthis.com/button1-bm.gif" medium="image">
			<media:title type="html">AddThis Social Bookmark Button</media:title>
		</media:content>

		<media:content url="http://s9.addthis.com/button1-fd.gif" medium="image">
			<media:title type="html">AddThis Feed Button</media:title>
		</media:content>
	</item>
	</channel>
</rss>