<?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>Gerbrand on ICT &#187; Add new tag</title>
	<atom:link href="http://www.gerbrand-ict.nl/tag/add-new-tag/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gerbrand-ict.nl</link>
	<description>Weblog on JEE and software-engineering</description>
	<lastBuildDate>Wed, 11 Aug 2010 20:51:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Generics support for Apache commons collections</title>
		<link>http://www.gerbrand-ict.nl/2009/05/generics-support-for-apache-commons-collections/</link>
		<comments>http://www.gerbrand-ict.nl/2009/05/generics-support-for-apache-commons-collections/#comments</comments>
		<pubDate>Sat, 16 May 2009 21:30:00 +0000</pubDate>
		<dc:creator>gerbrand</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Add new tag]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.gerbrand-ict.nl/?p=281</guid>
		<description><![CDATA[In my day to day work program I use the Apache Commons library quite a lot.  One of those libraries is the commons collections library, of which I use the CollectionUtils class and ListUtils mostly. One great disadvantage of the commons collection library is lack of Java Generics support, added in Java 1.5.  This means [...]]]></description>
			<content:encoded><![CDATA[<p>In my day to day work program I use the <a href="http://commons.apache.org/">Apache Commons</a> library quite a lot.  One of those libraries is the<a href="http://commons.apache.org/collections/"> commons collections </a>library, of which I use the <a href="http://commons.apache.org/collections/api-release/org/apache/commons/collections/CollectionUtils.html">CollectionUtils</a> class and <a href="http://commons.apache.org/collections/api-release/org/apache/commons/collections/ListUtils.html">ListUtils </a>mostly. One great disadvantage of the commons collection library is lack of Java Generics support, added in Java 1.5.  This means if you call a function like <a href="http://commons.apache.org/collections/api-release/org/apache/commons/collections/ListUtils.html#predicatedList(java.util.List,%20org.apache.commons.collections.Predicate)">predicatedList</a> with a typed list and predicate argument, the function will return an untyped list. The list has to be cast again, which clutters up the code and doesn&#8217;t look very nice, like the following example:</p>
<pre class="brush: java;">
class Address {
    String firstName;
}

List <Address> myAddresses ;
...
List <Address> onlyJohns = (List <Address>) ListUtils.predicatedList(myAddresses,new Predicate() {
   public boolean evaluate(Object o) {
       Address a=(Address)o;
       return a.firstName.equals("John");
   }
});
 </pre>
<p>Quite a lot of casting as you can see in the above code. Less code would be needed if generics support would be added to the collection framework. Added generics support to the collections library should be too hard, and would improve my code.<br/><br />
I&#8217;d think other people would have that idea, and I quickly found the following posting on <a href="http://www.devx.com/Java/Article/36183">devx</a>. Turns out there&#8217;s a sourceforge project that has modified the collections library to have generics support: <a href="http://sourceforge.net/projects/collections">Commons Collections with generics</a>.<br />
The project is even added to the central maven repository, so to use the new collection library adding the following dependency is enough:</p>
<pre class="brush: xml;">
<groupId>net.sourceforge.collections</groupId>
<artifactId>collections-generic</artifactId>
<version>4.01</version>
</pre>
<p>The new code now becomes (excluding the Address class, which is the same):</p>
<pre class="brush: java;">
List <Address> onlyJohns = ListUtils.predicatedList(myAddresses,new Predicate<Address>() {
        @Override
   public boolean evaluate(Address a) {
       return a.firstName.equals("John");
   }
});
</pre>
<p>The code looks quite better! A lot less code would be needed if <a href="http://www.javac.info/">closures </a>would be added to Java too, but that&#8217;ll have to wait until JDK 1.8 or something is released.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gerbrand-ict.nl/2009/05/generics-support-for-apache-commons-collections/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
