<?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>Mark Needham &#187; Java</title>
	<atom:link href="http://www.markhneedham.com/blog/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.markhneedham.com/blog</link>
	<description>Thoughts on Software Development</description>
	<lastBuildDate>Sat, 11 Feb 2012 10:56:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Java: Fooled by java.util.Arrays.asList</title>
		<link>http://www.markhneedham.com/blog/2012/02/11/java-fooled-by-java-util-arrays-aslist/</link>
		<comments>http://www.markhneedham.com/blog/2012/02/11/java-fooled-by-java-util-arrays-aslist/#comments</comments>
		<pubDate>Sat, 11 Feb 2012 10:29:15 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3992</guid>
		<description><![CDATA[I&#8217;ve been playing around with the boilerpipe code base by writing some tests around it to check my understanding but ran into an interesting problem using java.util.Arrays.asList to pass a list into one of the functions. I was testing the BlockProximityFusion class which is used to merge together adjacent text blocks. I started off calling [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been playing around with the <a href="http://code.google.com/p/boilerpipe/">boilerpipe</a> code base by writing some tests around it to check my understanding but ran into an interesting problem using <cite>java.util.Arrays.asList</cite> to pass a list into one of the functions.</p>
<p>I was testing the <cite><a href="https://github.com/mneedham/boilerpipe/blob/master/src/main/de/l3s/boilerpipe/filters/heuristics/BlockProximityFusion.java">BlockProximityFusion</a></cite> class which is used to merge together adjacent text blocks.</p>
<p>I started off calling that class like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> java.<span style="color: #006633;">util</span>.<span style="color: #003399;">Arrays</span>.<span style="color: #006633;">asList</span><span style="color: #339933;">;</span>
&nbsp;
@Test
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> willCallBlockProximityFustion<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>    
    TextDocument document <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TextDocument<span style="color: #009900;">&#40;</span>asList<span style="color: #009900;">&#40;</span>contentBlock<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;some words&quot;</span><span style="color: #009900;">&#41;</span>, contentBlock<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;followed by more words&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    BlockProximityFusion.<span style="color: #006633;">MAX_DISTANCE_1</span>.<span style="color: #006633;">process</span><span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> TextBlock contentBlock<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> words<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    TextBlock textBlock <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TextBlock<span style="color: #009900;">&#40;</span>words, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BitSet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, wordCount<span style="color: #009900;">&#40;</span>words<span style="color: #009900;">&#41;</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    textBlock.<span style="color: #006633;">setIsContent</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">return</span> textBlock<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Which blows up like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">java.<span style="color: #006633;">lang</span>.<span style="color: #003399;">UnsupportedOperationException</span>
	at java.<span style="color: #006633;">util</span>.<span style="color: #003399;">AbstractList</span>.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">AbstractList</span>.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">144</span><span style="color: #009900;">&#41;</span>
	at java.<span style="color: #006633;">util</span>.<span style="color: #003399;">AbstractList</span>$Itr.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">AbstractList</span>.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">360</span><span style="color: #009900;">&#41;</span>
	at de.<span style="color: #006633;">l3s</span>.<span style="color: #006633;">boilerpipe</span>.<span style="color: #006633;">filters</span>.<span style="color: #006633;">heuristics</span>.<span style="color: #006633;">BlockProximityFusion</span>.<span style="color: #006633;">process</span><span style="color: #009900;">&#40;</span>BlockProximityFusion.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">115</span><span style="color: #009900;">&#41;</span>
	at de.<span style="color: #006633;">l3s</span>.<span style="color: #006633;">boilerpipe</span>.<span style="color: #006633;">filters</span>.<span style="color: #006633;">heuristics</span>.<span style="color: #006633;">BlockProximityFusionTest</span>.<span style="color: #006633;">willCallBlockProximityFustion</span><span style="color: #009900;">&#40;</span>BlockProximityFusionTest.<span style="color: #006633;">java</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">63</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>The code around that area is trying to remove an element from an iterator&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>113
114
115
116
117
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">                <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ok<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    prevBlock.<span style="color: #006633;">mergeNext</span><span style="color: #009900;">&#40;</span>block<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    it.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    changes <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span></pre></td></tr></table></div>

<p>&#8230;which was created from the list that we passed into the constructor of <cite>TextDocument</cite>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>98
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Iterator<span style="color: #339933;">&lt;</span>TextBlock<span style="color: #339933;">&gt;</span> it <span style="color: #339933;">=</span> textBlocks.<span style="color: #006633;">listIterator</span><span style="color: #009900;">&#40;</span>offset<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> it</pre></td></tr></table></div>

<p>The <cite>remove</cite> method is not implemented on the list created by &#8216;Arrays.asList&#8217; which is weird since I thought it created an <cite>ArrayList</cite> which does implement <cite>remove</cite>!</p>
<p>I&#8217;ve now learnt that the <cite>ArrayList</cite> created by &#8216;Arrays.asList&#8217; is actually a private inner class of <cite>Arrays</cite> and doesn&#8217;t implement the <cite>remove</cite> method!</p>
<p>Who knew&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2012/02/11/java-fooled-by-java-util-arrays-aslist/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Java/Scala: Runtime.exec hanging/in &#8216;pipe_w&#8217; state</title>
		<link>http://www.markhneedham.com/blog/2011/11/20/javascala-runtime-exec-hangingin-pipe_w-state/</link>
		<comments>http://www.markhneedham.com/blog/2011/11/20/javascala-runtime-exec-hangingin-pipe_w-state/#comments</comments>
		<pubDate>Sun, 20 Nov 2011 20:20:08 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3795</guid>
		<description><![CDATA[On the system that I&#8217;m currently working on we have a data ingestion process which needs to take zip files, unzip them and then import their contents into the database. As a result we delegate from Scala code to the system unzip command like so: def extract &#123; var command = &#34;unzip %s -d %s&#34; [...]]]></description>
			<content:encoded><![CDATA[<p>On the system that I&#8217;m currently working on we have a data ingestion process which needs to take zip files, unzip them and then import their contents into the database.</p>
<p>As a result we delegate from Scala code to the system unzip command like so:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">def</span> extract <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">var</span> command <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;unzip %s -d %s&quot;</span> format<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;/file/to/unzip.zip&quot;</span>, <span style="color: #6666FF;">&quot;/place/to/unzip/to&quot;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">var</span> process<span style="color: #000080;">:</span> Process <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">null</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">try</span> <span style="color: #F78811;">&#123;</span>
    process <span style="color: #000080;">=</span> Runtime.<span style="color: #000000;">getRuntime</span>.<span style="color: #000000;">exec</span><span style="color: #F78811;">&#40;</span>command<span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> exitCode <span style="color: #000080;">=</span> process.<span style="color: #000000;">waitFor</span>
  <span style="color: #F78811;">&#125;</span> <span style="color: #0000ff; font-weight: bold;">catch</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">case</span> e <span style="color: #000080;">:</span> Exception <span style="color: #000080;">=&gt;</span> <span style="color: #008000; font-style: italic;">// do some stuff</span>
  <span style="color: #F78811;">&#125;</span> <span style="color: #0000ff; font-weight: bold;">finally</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #008000; font-style: italic;">// close the stream here</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>We ran into a problem where the unzipping process was hanging and executing &#8216;ps&#8217; showed us that the &#8216;unzip&#8217; process was stuck in the &#8216;pipe_w&#8217; (pipe waiting) state which suggested that it was waiting for some sort of input.</p>
<p>After a bit of googling <a href="http://duncan-cragg.org/blog/">Duncan</a> found <a href="http://vyvaks.wordpress.com/2006/05/27/does-runtimeexec-hangs-in-java/">this blog</a> which explained that we needed to process the output stream from our process otherwise it might end up hanging</p>
<p>a.k.a. <a href="http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Process.html">RTFM</a>:</p>
<blockquote><p>
The Runtime.exec methods may not work well for special processes on certain native platforms, such as native windowing processes, daemon processes, Win16/DOS processes on Microsoft Windows, or shell scripts. </p>
<p>The created subprocess does not have its own terminal or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (Process.getOutputStream(), Process.getInputStream(), Process.getErrorStream()). </p>
<p>The parent process uses these streams to feed input to and get output from the subprocess. </p>
<p>Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.
</p></blockquote>
<p>For most of the zip files we presumably hadn&#8217;t been reaching the limit of the buffer because the list of files being sent to STDOUT by &#8216;unzip&#8217; wasn&#8217;t that high.</p>
<p>In order to get around the problem we needed to gobble up the output stream from unzip like so:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">import</span> org.<span style="color: #000000;">apache</span>.<span style="color: #000000;">commons</span>.<span style="color: #000000;">io</span>.<span style="color: #000000;">IOUtils</span>
<span style="color: #0000ff; font-weight: bold;">def</span> extract <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">var</span> command <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;unzip %s -d %s&quot;</span> format<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;/file/to/unzip.zip&quot;</span>, <span style="color: #6666FF;">&quot;/place/to/unzip/to&quot;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">var</span> process<span style="color: #000080;">:</span> Process <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">null</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">try</span> <span style="color: #F78811;">&#123;</span>
    process <span style="color: #000080;">=</span> Runtime.<span style="color: #000000;">getRuntime</span>.<span style="color: #000000;">exec</span><span style="color: #F78811;">&#40;</span>command<span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> thisVariableIsNeededToSuckDataFromUnzipDoNotRemove <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;Output: &quot;</span> + IOUtils.<span style="color: #000000;">readLines</span><span style="color: #F78811;">&#40;</span>process.<span style="color: #000000;">getInputStream</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> exitCode <span style="color: #000080;">=</span> process.<span style="color: #000000;">waitFor</span>
  <span style="color: #F78811;">&#125;</span> <span style="color: #0000ff; font-weight: bold;">catch</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">case</span> e <span style="color: #000080;">:</span> Exception <span style="color: #000080;">=&gt;</span> <span style="color: #008000; font-style: italic;">// do some stuff</span>
  <span style="color: #F78811;">&#125;</span> <span style="color: #0000ff; font-weight: bold;">finally</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #008000; font-style: italic;">// close the stream here</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>We need to do the same thing with the error stream as well in case &#8216;unzip&#8217; ends up overflowing that buffer as well.</p>
<p>On a couple of blog posts that we came across it was suggested that we should &#8216;gobble up&#8217; the output and error streams on separate threads but we weren&#8217;t sure why exactly that was considered necessary&#8230;</p>
<p>If anyone knows then please let me know in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/11/20/javascala-runtime-exec-hangingin-pipe_w-state/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Java: Faking System.in</title>
		<link>http://www.markhneedham.com/blog/2011/03/24/java-faking-system-in/</link>
		<comments>http://www.markhneedham.com/blog/2011/03/24/java-faking-system-in/#comments</comments>
		<pubDate>Thu, 24 Mar 2011 21:58:31 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3442</guid>
		<description><![CDATA[We ran a refactoring dojo a couple of days ago at ThoughtWorks University and in preparation I wrote some system level tests around the coding problem that we were going to use during the session. It&#8217;s a command line application which is called through the main method of &#8216;Program&#8217; and since there&#8217;s no dependency injection [...]]]></description>
			<content:encoded><![CDATA[<p>We ran a <a href="http://www.markhneedham.com/blog/2011/03/22/thoughtworks-university-a-refactoring-dojo/">refactoring dojo</a> a couple of days ago at ThoughtWorks University and in preparation I wrote some system level tests around <a href="https://github.com/mneedham/biblioteca/blob/master/src/main/Program.java">the coding problem</a> that we were going to use during the session.</p>
<p>It&#8217;s a command line application which is called through the main method of &#8216;Program&#8217; and since there&#8217;s no dependency injection we need to be able to set System.in and System.out in order to do any testing.</p>
<p>My initial thinking was that it should be possible to fake System.in with the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> input <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;1<span style="color: #000099; font-weight: bold;">\n</span>9<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">setIn</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ByteArrayInputStream</span><span style="color: #009900;">&#40;</span>input.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This works fine when I just want to simulate one value being passed to System.in but it doesn&#8217;t work so well if I want to simulate passing more than one value because we had a BufferedReader being created each time we loop.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">...
<span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	...
	<span style="color: #003399;">InputStreamReader</span> inputStream <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">InputStreamReader</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">System</span>.<span style="color: #006633;">in</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003399;">BufferedReader</span> reader <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BufferedReader</span><span style="color: #009900;">&#40;</span>inputStream<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This means that the second time System.in gets read it is empty.</p>
<p><a href="http://jimbarritt.com/non-random/">Jim</a> and I paired on the problem for a bit and came to the conclusion that we&#8217;d need to &#8216;stub&#8217; the &#8216;read&#8217; method of &#8216;InputStream&#8217; if we wanted to be able to control exactly what was being returned by System.in.</p>
<p>We eventually ended up with the following <a href="https://github.com/mneedham/biblioteca/blob/master/src/test/StubbedInputStream.java">StubbedInputStream</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> StubbedInputStream <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">InputStream</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> Queue<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> input<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> StubbedInputStream<span style="color: #009900;">&#40;</span>Queue<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> input<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">input</span> <span style="color: #339933;">=</span> input<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> read<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> bytes, <span style="color: #000066; font-weight: bold;">int</span> i, <span style="color: #000066; font-weight: bold;">int</span> i1<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>input.<span style="color: #006633;">isEmpty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">int</span> byteLocation <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span> b <span style="color: #339933;">:</span> input.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            bytes<span style="color: #009900;">&#91;</span>byteLocation<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> b<span style="color: #339933;">;</span>
            byteLocation<span style="color: #339933;">++;</span>
        <span style="color: #009900;">&#125;</span>
        bytes<span style="color: #009900;">&#91;</span>byteLocation<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> byteLocation <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> InputStreamBuilder stubInputStream<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> InputStreamBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
	...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Which can be constructed using the <a href="https://github.com/mneedham/biblioteca/blob/master/src/test/InputStreamBuilder.java">following DSL</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">System</span>.<span style="color: #006633;">setIn</span><span style="color: #009900;">&#40;</span>stubInputStream<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toReturn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">then</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;9&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">atSomePoint</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The code we wrote is on <a href="https://github.com/mneedham/biblioteca/tree/master/src">github</a> &#8211; I&#8217;m not sure that it covers every possible scenario that you might come up with but it does pass the tests that I&#8217;ve managed to come up with!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/03/24/java-faking-system-in/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Writing a Java function in Clojure</title>
		<link>http://www.markhneedham.com/blog/2009/11/23/writing-a-java-function-in-clojure/</link>
		<comments>http://www.markhneedham.com/blog/2009/11/23/writing-a-java-function-in-clojure/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 10:08:20 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Clojure]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1868</guid>
		<description><![CDATA[A function that we had to write in Java on a project that I worked on recently needed to indicate whether there was a gap in a series of data points or not. If there were gaps at the beginning or end of the sequence then that was fine but gaps in the middle of [...]]]></description>
			<content:encoded><![CDATA[<p>A function that we had to write in Java on a project that I worked on recently needed to indicate whether there was a gap in a series of data points or not.</p>
<p>If there were gaps at the beginning or end of the sequence then that was fine but gaps in the middle of the sequence were not.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">null, 1, 2, 3 =&gt; no gaps
1, 2, 3, null =&gt; no gaps
1, null, 2, 3 =&gt; gaps</pre></div></div>

<p>The Java version looked a bit like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> hasGaps<span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>BigInteger<span style="color: #339933;">&gt;</span> values<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Iterator<span style="color: #339933;">&lt;</span>BigInteger<span style="color: #339933;">&gt;</span> fromHead <span style="color: #339933;">=</span> values.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>fromHead.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> fromHead.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        fromHead.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #003399;">Collections</span>.<span style="color: #006633;">reverse</span><span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    Iterator<span style="color: #339933;">&lt;</span>BigInteger<span style="color: #339933;">&gt;</span> fromTail <span style="color: #339933;">=</span> values.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>fromTail.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> fromTail.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        fromTail.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">return</span> values.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We take the initial list and then remove all the null values from the beginning of it, then reverse the list and remove all the values from the end.</p>
<p>We then check if there&#8217;s a null value and if there is then it would indicate there is indeed a gap in the list.</p>
<p>To write this function in Clojure we can start off by using the &#8216;<a href="http://clojure.org/api#toc237">drop-while</a>&#8216; function to get rid of the trailing nil values.</p>
<p>I started off with this attempt:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn has-gaps? <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>no-nils<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>drop-while #<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #66cc66;">%</span> <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#93;</span>
  no-nils<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Unfortunately that gives us the following error!</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Can't take value of a macro: #'clojure.core/let (NO_SOURCE_FILE:16)</pre></div></div>

<p>It thinks we&#8217;re trying to pass around the &#8216;let&#8217; macro instead of evaluating it &#8211; I forgot to put in the brackets around the &#8216;let&#8217;!</p>
<p>I fixed that with this next version:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn has-gaps? <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>no-nils<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>drop-while <span style="color: #b1b100;">nil</span>? <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#93;</span>
  no-nils<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>But again, no love:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;">java<span style="color: #66cc66;">.</span>lang<span style="color: #66cc66;">.</span>IllegalArgumentException<span style="color: #66cc66;">:</span> <span style="color: #b1b100;">let</span> requires an even number of forms in binding vector <span style="color: #66cc66;">&#40;</span>NO_SOURCE_FILE<span style="color: #66cc66;">:</span><span style="color: #cc66cc;">23</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The way I understand it the &#8216;let&#8217; macro takes in a vector of bindings as its first argument and what I&#8217;ve done here is pass in two vectors instead of one.</p>
<p>In the bindings vector we need to ensure that there are an even number of forms so that each symbol can be bound to an expression.</p>
<p>I fixed this by putting the two vectors defined above into another vector:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn has-gaps? <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>no-nils<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>drop-while <span style="color: #b1b100;">nil</span>? <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
  no-nils<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>We can simplify that further so that we don&#8217;t have nested vectors:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn has-gaps? <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>no-nils <span style="color: #66cc66;">&#40;</span>drop-while <span style="color: #b1b100;">nil</span>? <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
  no-nils<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The next step was to make &#8216;no-nils&#8217; a function so that I could make use of that function when the list was reversed as well:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn has-gaps? <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>no-nils <span style="color: #66cc66;">&#40;</span>fn <span style="color: #66cc66;">&#91;</span>x<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>drop-while <span style="color: #b1b100;">nil</span>? x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>no-nils <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>I then wrote the rest of the function to reverse the list and then <a href="http://www.markhneedham.com/blog/2009/11/21/clojure-checking-for-a-nil-value-in-a-collection/">check the remaining list for nil</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn has-gaps? <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>no-nils<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>fn <span style="color: #66cc66;">&#91;</span>x<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>drop-while <span style="color: #b1b100;">nil</span>? x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
          <span style="color: #66cc66;">&#91;</span>nils-removed<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>fn <span style="color: #66cc66;">&#91;</span>x<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>comp no-nils <span style="color: #b1b100;">reverse</span> no-nils<span style="color: #66cc66;">&#41;</span> x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>some <span style="color: #b1b100;">nil</span>? <span style="color: #66cc66;">&#40;</span>nils-removed <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The &#8216;<a href="http://clojure.org/api#toc151">comp</a>&#8216; function can be used to compose a set of functions which is what I needed.</p>
<p>It seemed like the &#8216;nils-removed&#8217; function wasn&#8217;t really necessary so I inlined that:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn has-gaps? <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>no-nils <span style="color: #66cc66;">&#40;</span>fn <span style="color: #66cc66;">&#91;</span>x<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>drop-while <span style="color: #b1b100;">nil</span>? x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>some <span style="color: #b1b100;">nil</span>? <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>comp no-nils <span style="color: #b1b100;">reverse</span> no-nils<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The function can now be used like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">user=&gt; (has-gaps? '(1 2 3))
nil
user=&gt; (has-gaps? '(nil 1 2 3))
nil
user=&gt; (has-gaps? '(1 2 3 nil))
nil
user=&gt; (has-gaps? '(1 2 nil 3))
true</pre></div></div>

<p>I&#8217;d be intrigued to know if there&#8217;s a better way to do this.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/11/23/writing-a-java-function-in-clojure/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>F# vs C# vs Java: Functional Collection Parameters</title>
		<link>http://www.markhneedham.com/blog/2009/01/19/f-vs-c-vs-java-functional-collection-parameters/</link>
		<comments>http://www.markhneedham.com/blog/2009/01/19/f-vs-c-vs-java-functional-collection-parameters/#comments</comments>
		<pubDate>Mon, 19 Jan 2009 09:24:25 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[F#]]></category>
		<category><![CDATA[functional-programming]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=860</guid>
		<description><![CDATA[I wrote a post about a month ago on using functional collection parameters in C# and over the weekend Fabio and I decided to try and contrast the way you would do this in Java, C# and then F# just for fun. Map Map evaluates a high order function on all the elements in a [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote a post about a month ago on using <a href="http://www.markhneedham.com/blog/2008/12/17/functional-collection-parameters-in-c/">functional collection parameters</a> in C# and over the weekend <a href="http://fabiopereira.me/blog/">Fabio</a> and I decided to try and contrast the way you would do this in Java, C# and then F# just for fun.</p>
<h3>Map</h3>
<p>Map evaluates a high order function on all the elements in a collection and then returns a new collection containing the results of the function evaluation.</p>
<p>Given the numbers 1-5, return the square of each number</p>
<p>Java</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> numbers <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">5</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> number <span style="color: #339933;">:</span> numbers<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>f<span style="color: #009900;">&#40;</span>number<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> f<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> value<span style="color: #339933;">*</span>value<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>C#</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">3</span>, <span style="color: #FF0000;">4</span>, <span style="color: #FF0000;">5</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Select</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x<span style="color: #008000;">*</span>x<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">ForEach</span><span style="color: #008000;">&#40;</span>Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>F#</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">[1..5] |&gt; List.map (fun x -&gt; x*x) |&gt; List.iter (printfn &quot;%d&quot;);;</pre></div></div>

<h3>Filter</h3>
<p>Filter applies a predicate against all of the elements in a collection and then returns a collection of elements which matched the predicate.</p>
<p>Given the numbers 1-5, print out only the numbers greater than 3:</p>
<p>Java</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> numbers <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">5</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> number <span style="color: #339933;">:</span> numbers<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    f<span style="color: #009900;">&#40;</span>number<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> f<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>value <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>C#</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span> <span style="color: #008000;">&#123;</span> <span style="color: #FF0000;">1</span>,<span style="color: #FF0000;">2</span>,<span style="color: #FF0000;">3</span>,<span style="color: #FF0000;">4</span>,<span style="color: #FF0000;">5</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">FindAll</span><span style="color: #008000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">ForEach</span><span style="color: #008000;">&#40;</span>Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>F#</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">[1..5] |&gt; List.filter (fun x -&gt; x &gt; 3) |&gt; List.iter (printfn &quot;%d&quot;);;</pre></div></div>

<h3>Reduce</h3>
<p>Reduce applies a high order function against all the elements in a collection and then returns a single result.</p>
<p>Given a list of numbers 1-5, add them all together and print out the answer</p>
<p>Java</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> sum <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> numbers <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">5</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> number <span style="color: #339933;">:</span> numbers<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    sum <span style="color: #339933;">+=</span> number<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>sum<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>C#</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Console.<span style="color: #006633;">WriteLine</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#125;</span>.<span style="color: #006633;">Aggregate</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #009900;">&#40;</span>accumulator, x<span style="color: #009900;">&#41;</span> <span style="color: #339933;">=&gt;</span> accumulator <span style="color: #339933;">+</span> x<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>F#</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">[1..5] |&gt; List.fold_left (+) 0 |&gt; printfn &quot;%d&quot;;;</pre></div></div>

<h3>In Summary</h3>
<p>I was surprised that we could achieve these results in relatively few lines of Java. The C# and F# versions are still more concise but the Java version isn&#8217;t too bad. The <a href="http://commons.apache.org/collections/api-2.1.1/org/apache/commons/collections/CollectionUtils.html#select(java.util.Collection,%20org.apache.commons.collections.Predicate)">Apache Commons Library has a class</a> which allows you to write these in a functional way but the need to use anonymous methods means it&#8217;s not as clean as what you can achieve in C# and F#.</p>
<p>I think there is still a bit of a mindset switch to make from thinking procedurally about these things to thinking in a way that allows you to make the most of functional programming concepts.</p>
<p>Keeping the code as declarative as possible and reducing the amount of state in our code are the most obvious things I have learned so far from playing with F#.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/01/19/f-vs-c-vs-java-functional-collection-parameters/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Coding Dojo #5: Uno</title>
		<link>http://www.markhneedham.com/blog/2009/01/08/coding-dojo-5-uno/</link>
		<comments>http://www.markhneedham.com/blog/2009/01/08/coding-dojo-5-uno/#comments</comments>
		<pubDate>Thu, 08 Jan 2009 13:41:57 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Coding Dojo]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=830</guid>
		<description><![CDATA[We ran our 5th coding dojo on Thursday night, writing the card game Uno in Java. We didn&#8217;t all know the rules so this video explained it &#8211; surely a parody but you never know! The Format We used the Randori approach again with 6 people participating for the majority of the session. Everyone paired [...]]]></description>
			<content:encoded><![CDATA[<p>We ran our 5th coding dojo on Thursday night, writing the card game <a href="http://en.wikipedia.org/wiki/Uno_(game)">Uno</a> in Java. We didn&#8217;t all know the rules so <a href="http://www.youtube.com/watch?v=biNXzYOxmb8">this video</a> explained it &#8211; surely a parody but you never know!</p>
<h3>The Format</h3>
<p>We used the <a href="http://codingdojo.org/cgi-bin/wiki.pl?RandoriKata">Randori</a> approach again with 6 people participating for the majority of the session. Everyone paired with everyone else at least once and sometimes a couple of times.</p>
<p>We had the pair driving at the front of the room and everyone else further back to stop the tendency of observers to whiteboard stuff.</p>
<h3>What We Learnt</h3>
<ul>
<li><strong>Modeling games is really good for practicing design skills</strong>. Most people had played the game so we had domain experts who could use their knowledge to help drive out the API of the various classes. We didn&#8217;t get to the scoring part of the game in the time available but it was quite cool to see our code with all the terms detailed in Wikipedia&#8217;s entry on the term.</li>
<li>We managed to <strong>drive the design much more effectively</strong> than we have done on previous sessions. The flexibility to move between classes depending on where it made most sense to test from next was finally there and we didn&#8217;t end up with the problem we&#8217;ve had on previous sessions where we ended up with coarsely grained tests and then tried to code the whole application in one go. </li>
<li>It was quite painful for me personally having to manually perform operations on collections in Java rather than having the selection of <a href="http://www.markhneedham.com/blog/2008/12/17/functional-collection-parameters-in-c/">functional operators</a> that are available in C# 3.0.  </li>
<li>It wasn&#8217;t a new learning but I&#8217;ve noticed in my project work that I&#8217;ve become a lot more keen to <strong>keep the steps really small</strong> &#8211; there is a bit of pressure on you to do this in a dojo situation and I think it&#8217;s just continued over from there. Every time I try to be too clever and take a big step something inevitably doesn&#8217;t work and I end up doing the small steps anyway. It&#8217;s also a lot of fun coding in this type of environment and watching how others approach problems and how they pair with each other. If you get a chance to attend a dojo I think it&#8217;d definitely be worthwhile.</li>
</ul>
<h3>Other Dojo Thoughts</h3>
<ul>
<li>Some ideas for future coding dojos that we discussed were:
<ul>
<li>Concurrency &#8211; using the <a href="http://code.google.com/p/retlang/">Retlang</a>/<a href="http://code.google.com/p/jetlang/">Jetlang</a> libraries</li>
<li>Do some stuff with <a href="http://code.google.com/p/webdriver/">Web Driver</a></li>
<li>Modeling games</li>
<li>Taking an open source project and refactoring it</li>
</ul>
</li>
<li>I notice there are a couple of sessions of coding/coding dojos planned for Jason Gorman&#8217;s <a href="http://parlezuml.com/softwarecraftsmanship/sessions/programme.htm">Software Craftsmanship conference</a>. It will be interesting to see how those work out, especially if there are high numbers of participants. We&#8217;ve always had a fairly small number of people involved which I think has helped to keep everyone involved. I&#8217;m not convinced it would be effective with many more participants.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/01/08/coding-dojo-5-uno/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JUnit Theories: First Thoughts</title>
		<link>http://www.markhneedham.com/blog/2008/12/12/junit-theories-first-thoughts/</link>
		<comments>http://www.markhneedham.com/blog/2008/12/12/junit-theories-first-thoughts/#comments</comments>
		<pubDate>Thu, 11 Dec 2008 14:34:17 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[theory]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=730</guid>
		<description><![CDATA[One of my favourite additions to JUnit 4.4 was the @Theory annotation which allows us to write parameterised tests rather than having to recreate the same test multiple times with different data values or creating one test and iterating through our own collection of data values. Previously, as far as I&#8217;m aware, it was only [...]]]></description>
			<content:encoded><![CDATA[<p>One of my favourite additions to <a href="http://www.testingreflections.com/node/view/5736">JUnit 4.4</a> was the @Theory annotation which allows us to write parameterised tests rather than having to recreate the same test multiple times with different data values or creating one test and iterating through our own collection of data values.</p>
<p>Previously, as far as I&#8217;m aware, it was only possible to parameterise tests by using the <a href="http://testng.org/doc/">TestNG</a> library which has some nice ideas around grouping tests but had horrible reporting the last time I used it.</p>
<p>To create parameterisable tests using Theories we need to write some code like the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Test</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.experimental.theories.DataPoints</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.experimental.theories.Theories</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.experimental.theories.Theory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.runner.RunWith</span><span style="color: #339933;">;</span>
&nbsp;
@RunWith<span style="color: #009900;">&#40;</span>Theories.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SomeTest <span style="color: #009900;">&#123;</span>
	@Theory
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testTheNewTheoriesStuff<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// test which involves int value	</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> @DataPoints <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> values <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">5</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The &#8216;testTheNewTheoriesStuff&#8217; Theory is then executed with each of the values defined in the values array decorated with the @DataPoints annotation.</p>
<p>The error message reported for a failure is reasonably good and makes it quite easy to figure out which one of the data points causes the problem. </p>
<p>An example error message for an assertion which failed inside a theory might look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">org.junit.experimental.theories.internal.ParameterizedAssertionError: testTheNewTheoriesStuff(values[1])</pre></div></div>

<p>It&#8217;s 0 indexed so this error message tells us that there was an error when running the theory with the 2nd data point, therefore allowing us to go and work out why that&#8217;s the case and fix it.</p>
<p>This approach is actually particularly useful for testing the scope in which classes we pull from a dependency injection container are available from in our application.</p>
<p>Another potential use for this would be to test the edge cases of our classes &#8211; perhaps this would work best if we can randomise the data it uses.</p>
<p>This seems to be more the approach Microsoft are taking with the the <a href="http://research.microsoft.com/Pex/">Pex</a> framework, a similar idea in the .NET space.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2008/12/12/junit-theories-first-thoughts/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Logging with Pico Container</title>
		<link>http://www.markhneedham.com/blog/2008/11/11/logging-with-pico-container/</link>
		<comments>http://www.markhneedham.com/blog/2008/11/11/logging-with-pico-container/#comments</comments>
		<pubDate>Mon, 10 Nov 2008 14:08:16 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[picocontainer]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=600</guid>
		<description><![CDATA[One thing that we&#8217;ve been working on recently is the logging for our current code base. Nearly all the objects in our system are being created by Pico Container so we decided that writing an interceptor that hooked into Pico Container would be the easiest way to intercept and log any exceptions throw from our [...]]]></description>
			<content:encoded><![CDATA[<p>One thing that we&#8217;ve been working on recently is the logging for our current code base.</p>
<p>Nearly all the objects in our system are being created by <a href="http://picocontainer.org/">Pico Container</a> so we decided that writing an interceptor that hooked into Pico Container would be the easiest way to intercept and log any exceptions throw from our code.</p>
<p>Our initial Googling led us to the <a href="http://picocontainer.org/interception.html">AOP Style Interception</a> page on the Pico website which detailed how we could create a static proxy for a class that we put in the container.</p>
<p>The code to do this was as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">        DefaultPicoContainer pico <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DefaultPicoContainer<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Intercepting<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        pico.<span style="color: #006633;">addComponent</span><span style="color: #009900;">&#40;</span>Interceptable.<span style="color: #000000; font-weight: bold;">class</span>, ConcreteInterceptable.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        Intercepted intercepted <span style="color: #339933;">=</span> pico.<span style="color: #006633;">getComponentAdapter</span><span style="color: #009900;">&#40;</span>Interceptable.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">findAdapterOfType</span><span style="color: #009900;">&#40;</span>Intercepted.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        intercepted.<span style="color: #006633;">addPreInvocation</span><span style="color: #009900;">&#40;</span>Interceptable.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #000000; font-weight: bold;">new</span> InterceptableReporter<span style="color: #009900;">&#40;</span>intercepted.<span style="color: #006633;">getController</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        intercepted.<span style="color: #006633;">addPostInvocation</span><span style="color: #009900;">&#40;</span>Interceptable.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #000000; font-weight: bold;">new</span> InterceptableReporter<span style="color: #009900;">&#40;</span>intercepted.<span style="color: #006633;">getController</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        Interceptable a1 <span style="color: #339933;">=</span> pico.<span style="color: #006633;">getComponent</span><span style="color: #009900;">&#40;</span>Interceptable.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        a1.<span style="color: #006633;">methodThatThrowsException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Interceptable <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">void</span> methodThatThrowsException<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> InterceptableReporter <span style="color: #000000; font-weight: bold;">implements</span> Interceptable <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">private</span> Intercepted.<span style="color: #006633;">Controller</span> controller<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> InterceptableReporter<span style="color: #009900;">&#40;</span>Intercepted.<span style="color: #006633;">Controller</span> controller<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">controller</span> <span style="color: #339933;">=</span> controller<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> methodThatThrowsException<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error happened&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>While this approach works, the problem is that we need to define an individual proxy for every class that we want to intercept. It works as  a strategy if we just need to intercept a few classes but not on a larger scale.</p>
<p>Luckily it is possible to create a <a href="http://lizdouglass.wordpress.com/2008/08/31/small-things-amuse-small%E2%80%A6-hmph-well-anyway%E2%80%A6/">dynamic proxy</a> on the container so that we can intercept all the objects without having to create a static proxy for each one.</p>
<p>The code to do this was as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">        DefaultPicoContainer pico <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DefaultPicoContainer<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> LoggingAwareByDefault<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        pico.<span style="color: #006633;">addComponent</span><span style="color: #009900;">&#40;</span>Interceptable.<span style="color: #000000; font-weight: bold;">class</span>, ConcreteInterceptable.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        Interceptable interceptable <span style="color: #339933;">=</span> pico.<span style="color: #006633;">getComponent</span><span style="color: #009900;">&#40;</span>Interceptable.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        interceptable.<span style="color: #006633;">methodThatThrowsException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.logging.LogFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.picocontainer.Characteristics</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.picocontainer.ComponentAdapter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.picocontainer.ComponentMonitor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.picocontainer.LifecycleStrategy</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.picocontainer.Parameter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.picocontainer.behaviors.AbstractBehaviorFactory</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Properties</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LoggingAwareByDefault <span style="color: #000000; font-weight: bold;">extends</span> AbstractBehaviorFactory <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> DO_NOT_LOG_NAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;support-team-opt-out&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Properties</span> DO_NOT_LOG <span style="color: #339933;">=</span> Characteristics
            .<span style="color: #006633;">immutable</span><span style="color: #009900;">&#40;</span>DO_NOT_LOG_NAME, Characteristics.<span style="color: #000066; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> ComponentAdapter<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> createComponentAdapter<span style="color: #009900;">&#40;</span>ComponentMonitor componentMonitor,
                                                          LifecycleStrategy lifecycleStrategy,
                                                          <span style="color: #003399;">Properties</span> componentProperties,
                                                          <span style="color: #003399;">Object</span> componentKey, Class<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> componentImplementation,
                                                          Parameter... <span style="color: #006633;">parameters</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>removePropertiesIfPresent<span style="color: #009900;">&#40;</span>componentProperties, DO_NOT_LOG<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">createComponentAdapter</span><span style="color: #009900;">&#40;</span>componentMonitor, lifecycleStrategy, componentProperties, componentKey,
                    componentImplementation, parameters<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> LoggingAware<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">createComponentAdapter</span><span style="color: #009900;">&#40;</span>componentMonitor,
                    lifecycleStrategy, componentProperties, componentKey,
                    componentImplementation, parameters<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.logging.Log</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.picocontainer.ComponentAdapter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.picocontainer.ComponentMonitor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.picocontainer.PicoContainer</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.picocontainer.behaviors.HiddenImplementation</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.reflect.InvocationTargetException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.lang.reflect.Method</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LoggingAware<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> <span style="color: #000000; font-weight: bold;">extends</span> HiddenImplementation <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> LoggingAware<span style="color: #009900;">&#40;</span><span style="color: #003399;">ComponentAdapter</span> delegate<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span>delegate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">Object</span> invokeMethod<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> componentInstance, <span style="color: #003399;">Method</span> method, <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args, PicoContainer container<span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Throwable</span> <span style="color: #009900;">&#123;</span>
        ComponentMonitor componentMonitor <span style="color: #339933;">=</span> currentMonitor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            componentMonitor.<span style="color: #006633;">invoking</span><span style="color: #009900;">&#40;</span>container, <span style="color: #000000; font-weight: bold;">this</span>, method, componentInstance<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">long</span> startTime <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">Object</span> object <span style="color: #339933;">=</span> method.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span>componentInstance, args<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            componentMonitor.<span style="color: #006633;">invoked</span><span style="color: #009900;">&#40;</span>container,
                                     <span style="color: #000000; font-weight: bold;">this</span>,
                                     method, componentInstance, <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> startTime<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">return</span> object<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">InvocationTargetException</span> ite<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            componentMonitor.<span style="color: #006633;">invocationFailed</span><span style="color: #009900;">&#40;</span>method, componentInstance, ite<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// log the error</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">throw</span> ite.<span style="color: #006633;">getTargetException</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>From what I recall from looking at the source code I think in order to create a proxy around an object it needs to implement an interface otherwise the proxy will not be created.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2008/11/11/logging-with-pico-container/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hamcrest Matchers &#8211; Make the error message clear</title>
		<link>http://www.markhneedham.com/blog/2008/11/08/hamcrest-matchers-make-the-error-message-clear/</link>
		<comments>http://www.markhneedham.com/blog/2008/11/08/hamcrest-matchers-make-the-error-message-clear/#comments</comments>
		<pubDate>Fri, 07 Nov 2008 16:46:59 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[hamcrest]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=590</guid>
		<description><![CDATA[We have been making good use of Hamcrest matchers on my current project for making assertions, and have moved almost entirely away from the more traditional JUnit assertEquals approach. There are several reasons why I find the Hamcrest matcher approach to be more productive &#8211; it&#8217;s more flexible, more expressive and when an assertion fails [...]]]></description>
			<content:encoded><![CDATA[<p>We have been making good use of <a href="http://code.google.com/p/hamcrest/">Hamcrest</a> matchers on my current project for making assertions, and have moved almost entirely away from the more traditional JUnit assertEquals approach.</p>
<p>There are several reasons why I find the Hamcrest matcher approach to be more productive &#8211; it&#8217;s more flexible, more expressive and when an assertion fails we have a much better idea about why it has failed than if we use a JUnit assertion for example.</p>
<p>This applies especially when we get a test failing as part of the build as compared to running a test from the IDE where the source code is close at hand and non descriptive error messages may not be such a problem.</p>
<p>It therefore makes sense when writing custom Hamcrest matchers to ensure that we do indeed provide a clear error message so that it is obvious how to fix the test.</p>
<p>The convention seems to be that we should first state the static method name of the matcher and then in brackets list the expected arguments.</p>
<p>To give an example from a matcher we wrote yesterday:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hamcrest.Description</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hamcrest.Factory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hamcrest.TypeSafeMatcher</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ContainsAllOf<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> <span style="color: #000000; font-weight: bold;">extends</span> TypeSafeMatcher<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> messages<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> ContainsAllOf<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>... <span style="color: #006633;">messages</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">messages</span> <span style="color: #339933;">=</span> messages<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> describeTo<span style="color: #009900;">&#40;</span>Description description<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        description.<span style="color: #006633;">appendText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;containsAllOf(&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> message <span style="color: #339933;">:</span> messages<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            description.<span style="color: #006633;">appendText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            description.<span style="color: #006633;">appendValue</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        description.<span style="color: #006633;">appendText</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Factory
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> ContainsAllOf containsAllOf<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>... <span style="color: #006633;">messages</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ContainsAllOf<span style="color: #009900;">&#40;</span>messages<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> matchesSafely<span style="color: #009900;">&#40;</span>T t<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> contains<span style="color: #009900;">&#40;</span>t, messages<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> contains<span style="color: #009900;">&#40;</span>T t, <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> messages<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">boolean</span> containsAllMessages <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> message <span style="color: #339933;">:</span> messages<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>t.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> containsAllMessages<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>If we call this in our test with a value that doesn&#8217;t exist:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">assertThat<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mark's cool message&quot;</span>, containsAllOf<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mark&quot;</span>, <span style="color: #0000ff;">&quot;cool&quot;</span>, <span style="color: #0000ff;">&quot;message&quot;</span>, <span style="color: #0000ff;">&quot;notThere&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Running the test results in the following error:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">java.lang.AssertionError: 
Expected: containsAllOf(,&quot;mark&quot;,&quot;cool&quot;,&quot;message&quot;,&quot;notThere&quot;)
     got: &quot;mark's cool message&quot;</pre></div></div>

<p>We can easily see what the problem is and how to go about fixing it, which I feel is the most important thing when it comes to test assertions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2008/11/08/hamcrest-matchers-make-the-error-message-clear/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Keep Java checked exceptions in a bounded context</title>
		<link>http://www.markhneedham.com/blog/2008/10/23/keep-java-checked-exceptions-in-a-bounded-context/</link>
		<comments>http://www.markhneedham.com/blog/2008/10/23/keep-java-checked-exceptions-in-a-bounded-context/#comments</comments>
		<pubDate>Thu, 23 Oct 2008 11:22:26 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[checked-exceptions]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=533</guid>
		<description><![CDATA[One of the features that I dislike in Java compared to C# is checked exceptions. For me an exception is about a situation which is exceptional, and if we know that there is a possibility of it happening and even have that possibility defined in our code then it doesn&#8217;t seem all that exceptional to [...]]]></description>
			<content:encoded><![CDATA[<p>One of the features that I dislike in Java compared to C# is <a href="http://radio.weblogs.com/0122027/stories/2003/04/01/JavasCheckedExceptionsWereAMistake.html">checked exceptions</a>.</p>
<p>For me an exception is about a situation which is <strong>exceptional</strong>, and if we know that there is a possibility of it happening and even have that possibility defined in our code then it doesn&#8217;t seem all that exceptional to me.</p>
<p>Having said that they do at least provide information which you can&#8217;t help but notice about what can go wrong when you make a call to a particular method.</p>
<p>The problem is that often these checked exceptions just get passed on &#8211; i.e. not handled &#8211; until we end up with an exception on the page the user sees which is completely irrelevant to the action they are trying to undertake.</p>
<p>To give an example, we have been using the <a href="http://www.ognl.org/">OGNL</a> library to hydrate some objects for testing using the builder pattern.</p>
<p>We have something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FooBuilder <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> bar<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> FooBuilder setBar<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> bar<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">bar</span> <span style="color: #339933;">=</span> bar<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Foo toFoo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        Foo foo <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setValue<span style="color: #009900;">&#40;</span>foo, <span style="color: #0000ff;">&quot;bar&quot;</span>, bar<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> foo<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> setValue<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> object, <span style="color: #003399;">String</span> propertyName, <span style="color: #003399;">Object</span> propertyValue<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            OgnlWrapper.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span>object, propertyName, propertyValue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>OgnlException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">RuntimeException</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ognl.DefaultMemberAccess</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ognl.MemberAccess</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ognl.Ognl</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ognl.OgnlContext</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">ognl.OgnlException</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> OgnlWrapper <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> setValue<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> object, <span style="color: #003399;">String</span> propertyName, <span style="color: #003399;">Object</span> propertyValue<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> OgnlException <span style="color: #009900;">&#123;</span>
        Ognl.<span style="color: #006633;">setValue</span><span style="color: #009900;">&#40;</span>propertyName, createOgnlContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, object, propertyValue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> OgnlContext createOgnlContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        MemberAccess memberAccess <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DefaultMemberAccess<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        OgnlContext ognlContext <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> OgnlContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        ognlContext.<span style="color: #006633;">setMemberAccess</span><span style="color: #009900;">&#40;</span>memberAccess<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> ognlContext<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We can then build an instance of &#8216;Foo&#8217; like so:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Foo foo <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FooBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setBar</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;barValue&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toFoo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>What is interesting here is not the OGNL library in itself but the checked &#8216;OgnlException&#8217; which the &#8216;Ognl.setValue(&#8230;)&#8217; method defines.</p>
<p>If I am using the FooBuilder I don&#8217;t care how the Foo object is created, all I care is that I get it. Therefore we don&#8217;t want to bubble the implementation details of how we are creating the object upwards.</p>
<p>I only care about the OgnlException if I am calling the OgnlWrapper and therefore that is where the exception should be caught and then rethrown as a Runtime exception.</p>
<p>I like to refer to this area of OgnlWrapper callees as being a <a href="http://domaindrivendesign.org/discussion/messageboardarchive/BoundedContext.html">bounded context</a> &#8211; that exception should only be applicable in that particular area and beyond that it should not exist.</p>
<p>Doing this allows us more flexibility around the way we implement things. If I decide in the future to use a different library instead of OGNL to do the same job I don&#8217;t need to worry that the callees of FooBuilder will all need to be updated. I can just make the change inside FooBuilder and that&#8217;s it!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2008/10/23/keep-java-checked-exceptions-in-a-bounded-context/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

