<?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>Transient Technology &#187; thread</title>
	<atom:link href="http://martinaharris.com/tag/thread/feed/" rel="self" type="application/rss+xml" />
	<link>http://martinaharris.com</link>
	<description>Next time you look it might be gone</description>
	<lastBuildDate>Tue, 24 Jan 2012 18:14:10 +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>Neat multiset in google collections API</title>
		<link>http://martinaharris.com/2009/10/neat-multiset-in-google-collections-api/</link>
		<comments>http://martinaharris.com/2009/10/neat-multiset-in-google-collections-api/#comments</comments>
		<pubDate>Sat, 31 Oct 2009 00:14:27 +0000</pubDate>
		<dc:creator>Martin Harris</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[software quality]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[collections]]></category>
		<category><![CDATA[equals]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[quality]]></category>
		<category><![CDATA[thread]]></category>

		<guid isPermaLink="false">http://martinaharris.com/?p=113</guid>
		<description><![CDATA[This good thing was so small that I almost did not bother to blog it But the devil is in the detail so here it is anyway. I have this multi threaded application. One of the problems with integration testing &#8230; <a href="http://martinaharris.com/2009/10/neat-multiset-in-google-collections-api/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h3>This good thing was so small that I almost did not bother to blog it</h3>
<p><cite>But the devil is in the detail</cite> so here it is anyway.</p>
<p>I have this multi threaded application.  One of the problems with integration testing it is, you can&#8217;t guarantee the order that messages will be written to a stream.  So during the test I have to go to some trouble to split and order the lines written out.  If I don&#8217;t do this the test fails sometimes. It depends on which thread writes first. The output though is correct, order is not important.  Not exactly rocket science.</p>
<p>Here is the method I was using to massage the string.</p>
<pre class="brush: java; gutter: false; wrap-lines: false">
 public List&lt;String&gt; getSortedLines(String expectedString) {

        String[] splitExpectedResultLines = expectedString.toString().split("\n");
        List&lt;String&gt; resultLines = new ArrayList&lt;String&gt;();
        for (String messageLine : resultLines) {
            resultLines.add(messageLine);
        }

        Collections.sort(resultLines);
        return resultLines;
    }
</pre>
<p>There is nothing particularly wrong with the method.  It takes a string, splits it into lines, loads a List, sorts and returns it.  Thus when its used in an assertion later the expected and actual lists are the same.</p>
<p>With google collections its just three lines!  The HashMultiset is designed to be used in equals methods.  Its equal even if the order of the elements in collections being compared is different.  From the API documentation: <cite>An extension of Collection that may contain duplicate values like a List, yet has order-independent equality like a Set. One typical use for a multiset is to represent a histogram.</cite> </p>
<p>Its possible to use the HashMultiSet.create static factory method, passing in an Iterable.  i.e a Collection.  So from the original code, both the loop and the Collections.sort are no longer required.</p>
<pre class="brush: java; gutter: false; wrap-lines: false; highlight: 6">
   public HashMultiset&lt;String&gt; getOrderAgnosticCollection(String expectedString) {

        String[] splitExpectedResultLines = expectedString.toString().split("\n");
        Iterable&lt;String&gt; resultLines = Arrays.asList(splitExpectedResultLines);
        HashMultiset&lt;String&gt; orderAgnosticResultLines =
                                              HashMultiset.create(resultLines);

        return orderAgnosticResultLines;
    }
</pre>
<p>If there was something that performed the split, but returned an Iterable the method would be perfect!  <img src='http://martinaharris.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<div class="lightsocial_container"><a class="lightsocial_a" href="http://digg.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fneat-multiset-in-google-collections-api%2F&amp;title=Neat+multiset+in+google+collections+API" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/digg.png" alt="Digg This" title="Digg This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.reddit.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fneat-multiset-in-google-collections-api%2F&amp;title=Neat+multiset+in+google+collections+API" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/reddit.png" alt="Reddit This" title="Reddit This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fneat-multiset-in-google-collections-api%2F&amp;title=Neat+multiset+in+google+collections+API" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/stumbleupon.png" alt="Stumble Now!" title="Stumble Now!" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://buzz.yahoo.com/buzz?targetUrl=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fneat-multiset-in-google-collections-api%2F&amp;headline=Neat+multiset+in+google+collections+API" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/yahoo_buzz.png" alt="Buzz This" title="Buzz This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dzone.com/links/add.html?title=Neat+multiset+in+google+collections+API&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fneat-multiset-in-google-collections-api%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dzone.png" alt="Vote on DZone" title="Vote on DZone" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.facebook.com/sharer.php?t=Neat+multiset+in+google+collections+API&amp;u=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fneat-multiset-in-google-collections-api%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/facebook.png" alt="Share on Facebook" title="Share on Facebook" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://delicious.com/save?title=Neat+multiset+in+google+collections+API&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fneat-multiset-in-google-collections-api%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/delicious.png" alt="Bookmark this on Delicious" title="Bookmark this on Delicious" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dotnetkicks.com/kick/?title=Neat+multiset+in+google+collections+API&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fneat-multiset-in-google-collections-api%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dotnetkicks.png" alt="Kick It on DotNetKicks.com" title="Kick It on DotNetKicks.com" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://dotnetshoutout.com/Submit?title=Neat+multiset+in+google+collections+API&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fneat-multiset-in-google-collections-api%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dotnetshoutout.png" alt="Shout it" title="Shout it" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fneat-multiset-in-google-collections-api%2F&amp;title=Neat+multiset+in+google+collections+API&amp;summary=&amp;source=" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/linkedin.png" alt="Share on LinkedIn" title="Share on LinkedIn" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.technorati.com/faves?add=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fneat-multiset-in-google-collections-api%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/technorati.png" alt="Bookmark this on Technorati" title="Bookmark this on Technorati" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://twitter.com/home?status=Reading+http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fneat-multiset-in-google-collections-api%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/twitter.png" alt="Post on Twitter" title="Post on Twitter" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.google.com/buzz/post?url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fneat-multiset-in-google-collections-api%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/google_buzz.png" alt="Google Buzz (aka. Google Reader)" title="Google Buzz (aka. Google Reader)" /></a>&nbsp;&nbsp;</div><div class="dzone_button" style="float: right; margin-left: 5px;">
<script type="text/javascript">
var dzone_url = 'http://martinaharris.com/2009/10/neat-multiset-in-google-collections-api/';
var dzone_title = 'Neat multiset in google collections API';
var dzone_blurb = '';
var dzone_style = '2';
</script>
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
</div>]]></content:encoded>
			<wfw:commentRss>http://martinaharris.com/2009/10/neat-multiset-in-google-collections-api/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Refactoring to Google Collections &#8211; ImmutableList  for simple code</title>
		<link>http://martinaharris.com/2009/10/google-collection-refactor-simplifies-implementation/</link>
		<comments>http://martinaharris.com/2009/10/google-collection-refactor-simplifies-implementation/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 14:37:13 +0000</pubDate>
		<dc:creator>Martin Harris</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[collections]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[immutable]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[thread]]></category>

		<guid isPermaLink="false">http://martinaharris.com/?p=54</guid>
		<description><![CDATA[Google collections are excellent in multi-threaded applications, so I thought I would re-factor some code to use them. <a href="http://martinaharris.com/2009/10/google-collection-refactor-simplifies-implementation/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I was reading this excellent post on <a href="http://blog.jayway.com/2009/10/22/google-collections/" target="_blank">google collections by Sune Simonsen</a> and decided to re-factor some code of mine.  I have an immutable results object used in a multi-threaded application.  I wanted to feel the difference using the google ImmutableList.  My existing class provides immutability in a very similar way to the google classes.  Static methods are used to instantiate the object.  An array is used to back a list collection.  Defensive copies are made to protect the immutability of the object.  Moving to <a href="http://code.google.com/p/google-collections/" target="_blank">google collections</a> is easy and makes my implementation simple whilst reducing test overhead.</p>
<p><span id="more-54"></span></p>
<h3>Steps taken to refactor</h3>
<p>There was actually very little to do.</p>
<p><strong>Step 1, add a maven dependency</strong></p>
<pre class="brush: xml; gutter: false">
<dependency>
            <groupId>com.google.collections</groupId>
            <artifactId>google-collections</artifactId>
            <version>1.0-rc3</version>
            <type>jar</type>
</dependency>
</pre>
<p><strong>Step 2, replace the array with an ImmutableList</strong></p>
<pre class="brush: java; light: true">
private static final LineResult[] EMPTY_LINERESULT_ARRAY = new LineResult[0];
private LineResult[] lineResults = EMPTY_LINERESULT_ARRAY;
</pre>
<p>could become:</p>
<pre class="brush: java; light: true">
private List&lt;LineResult&gt;  lineResults = ImmutableList.of();
</pre>
<p>In the end I used ImmutableList to clearly my intent.  The usage is internal to the class so there is no advantage in using the List interface.</p>
<pre class="brush: java; light: true">
private ImmutableListst&lt;LineResult&gt;  lineResults = ImmutableList.of();
</pre>
<p><strong>Step 3, The constructor and static factory methods are simplified</strong><br />
We are no longer using an array to back the list, so the protective copy can be moved from the constructor into the static method.  This reduces the window of vulnerability you are exposed to before capturing the data held in the list.  This is a direct result of the google class providing a static factory method.  Nb.  Its a shame that the standard collections don&#8217;t provide static factory methods.</p>
<p>Prior to the changes the protective copy was done in construction like this.</p>
<pre class="brush: java; light: true">
 this.lineResults = lineResults.toArray(EMPTY_LINERESULT_ARRAY);
</pre>
<p>This result is a much simpler constructor.</p>
<pre class="brush: java; light: true; highlight: 3">
private ResultsFileSearch(File fileFound, ImmutableList&lt;LineResult&gt; lineResults) {
        this(fileFound);
        this.lineResults = lineResults;
}
</pre>
<p>My newInstance method makes use of the ImmutableList.copyOf static factory method resulting in a very clean conversion of List into Immutable list.  Note that generics is inferring the type.</p>
<pre class="brush: java; light: true; highlight: 4">
public static ResultsFileSearch newInstance(File fileFound
                 , List&lt;LineResult&gt; lineResults) {
        return new ResultsFileSearch(fileFound
                        ,ImmutableList.copyOf(lineResults));
}
</pre>
<p><strong>Step 4, the getLineResults method no longer has to deal with the array or conversion.</strong><br />
The original array backed object had to deal with returning either an empty list or conversion to an unmodifiable list.  There are two things I did not like about this when I wrote the original.</p>
<ul>
<li>The code has to check the array resulting in some conditional logic.  This increases the test overhead slightly.</li>
<li>The use of Collections.unmodifiableList(Arrays.asList(lineResults)); is very ugly and slow.</li>
</ul>
<p>This was the original, <em>note the subtle bug when returning an empty list</em>.</p>
<pre class="brush: java; light: true">
 public List&lt;LineResult&gt; getLineResults() {

        if ((this.lineResults.length == 0)) {
            return Collections.emptyList();
        } else {
            return Collections.unmodifiableList(Arrays.asList(lineResults));
        }
        return this.lineResults;
}
</pre>
<p>I hope you agree its a much simpler version using the google code.  As we are now using the Immutable implementation, we don&#8217;t have to worry about exposing the reference.</p>
<pre class="brush: java; light: true">
public List&lt;LineResult&gt; getLineResults() {
        return this.lineResults;
}
</pre>
<h3>Conclusions</h3>
<p>Using the google collections makes my implementation much cleaner.  Several complexities are given over to the google collections classes.  Moreover testing of my class is simpler.  The changes are contained within the class.  None of the method signatures change, which has two consequences.  On the plus side the re-factor is contained within this class.  On the negative side, any code modifying the list will raise an UnsupportedOperationException which was no different to my original version.  The construction of my class might be slightly slower, the memory overhead slightly bigger now that we are not using the array.  Counter to this, the getLineResults method is quicker as it now only returns a reference.  In my program this is more important as architecturally it uses bounded queues to hold these objects for processing by threads.  So although its using a little extra memory, its constrained.  The performance gain is worth it.</p>
<p>I recommend using the com.google.common.collect package but adhere to the <a href="http://code.google.com/p/google-collections/wiki/Faq" target="_blank">caveats</a> i.e don&#8217;t expose the types in an API etc.</p>
<div class="lightsocial_container"><a class="lightsocial_a" href="http://digg.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fgoogle-collection-refactor-simplifies-implementation%2F&amp;title=Refactoring+to+Google+Collections+-+ImmutableList++for+simple+code" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/digg.png" alt="Digg This" title="Digg This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.reddit.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fgoogle-collection-refactor-simplifies-implementation%2F&amp;title=Refactoring+to+Google+Collections+-+ImmutableList++for+simple+code" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/reddit.png" alt="Reddit This" title="Reddit This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fgoogle-collection-refactor-simplifies-implementation%2F&amp;title=Refactoring+to+Google+Collections+-+ImmutableList++for+simple+code" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/stumbleupon.png" alt="Stumble Now!" title="Stumble Now!" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://buzz.yahoo.com/buzz?targetUrl=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fgoogle-collection-refactor-simplifies-implementation%2F&amp;headline=Refactoring+to+Google+Collections+-+ImmutableList++for+simple+code" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/yahoo_buzz.png" alt="Buzz This" title="Buzz This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dzone.com/links/add.html?title=Refactoring+to+Google+Collections+-+ImmutableList++for+simple+code&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fgoogle-collection-refactor-simplifies-implementation%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dzone.png" alt="Vote on DZone" title="Vote on DZone" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.facebook.com/sharer.php?t=Refactoring+to+Google+Collections+-+ImmutableList++for+simple+code&amp;u=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fgoogle-collection-refactor-simplifies-implementation%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/facebook.png" alt="Share on Facebook" title="Share on Facebook" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://delicious.com/save?title=Refactoring+to+Google+Collections+-+ImmutableList++for+simple+code&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fgoogle-collection-refactor-simplifies-implementation%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/delicious.png" alt="Bookmark this on Delicious" title="Bookmark this on Delicious" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dotnetkicks.com/kick/?title=Refactoring+to+Google+Collections+-+ImmutableList++for+simple+code&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fgoogle-collection-refactor-simplifies-implementation%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dotnetkicks.png" alt="Kick It on DotNetKicks.com" title="Kick It on DotNetKicks.com" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://dotnetshoutout.com/Submit?title=Refactoring+to+Google+Collections+-+ImmutableList++for+simple+code&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fgoogle-collection-refactor-simplifies-implementation%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/dotnetshoutout.png" alt="Shout it" title="Shout it" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fgoogle-collection-refactor-simplifies-implementation%2F&amp;title=Refactoring+to+Google+Collections+-+ImmutableList++for+simple+code&amp;summary=&amp;source=" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/linkedin.png" alt="Share on LinkedIn" title="Share on LinkedIn" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.technorati.com/faves?add=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fgoogle-collection-refactor-simplifies-implementation%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/technorati.png" alt="Bookmark this on Technorati" title="Bookmark this on Technorati" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://twitter.com/home?status=Reading+http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fgoogle-collection-refactor-simplifies-implementation%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/twitter.png" alt="Post on Twitter" title="Post on Twitter" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.google.com/buzz/post?url=http%3A%2F%2Fmartinaharris.com%2F2009%2F10%2Fgoogle-collection-refactor-simplifies-implementation%2F" target="_blank"><img class="lightsocial_img" src="http://martinaharris.com/wp-content/plugins/light-social/google_buzz.png" alt="Google Buzz (aka. Google Reader)" title="Google Buzz (aka. Google Reader)" /></a>&nbsp;&nbsp;</div><div class="dzone_button" style="float: right; margin-left: 5px;">
<script type="text/javascript">
var dzone_url = 'http://martinaharris.com/2009/10/google-collection-refactor-simplifies-implementation/';
var dzone_title = 'Refactoring to Google Collections &#8211; ImmutableList  for simple code';
var dzone_blurb = '';
var dzone_style = '2';
</script>
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
</div>]]></content:encoded>
			<wfw:commentRss>http://martinaharris.com/2009/10/google-collection-refactor-simplifies-implementation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

