<?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>FactoryPattern.com &#187; Java</title>
	<atom:link href="http://www.factorypattern.com/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.factorypattern.com</link>
	<description>Just another Object Oriented Weblog</description>
	<lastBuildDate>Sat, 02 Jan 2010 22:08:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Method Chaining</title>
		<link>http://www.factorypattern.com/method-chaining/</link>
		<comments>http://www.factorypattern.com/method-chaining/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 00:48:47 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[patterns]]></category>

		<guid isPermaLink="false">http://www.factorypattern.com/?p=64</guid>
		<description><![CDATA[Method chaining is a simple programming technique that can be implemented in almost any programming language. In simple words, it means that a method performs some operations on &#8220;this&#8221; object and then returns it so it can be used further more. It allows us to invoke several methods one after another, on one or different [...]]]></description>
			<content:encoded><![CDATA[<p>Method chaining is a simple programming technique that can be implemented in almost any programming language. In simple words, it means that a method performs some operations on &#8220;this&#8221; object and then returns it so it can be used further more. It allows us to invoke several methods one after another, on one or different objects, usually written in the same line.</p>
<p>For example we can build a rectangle class using using the Method Chaining for setters. You can see the setters are a little bit different than regular setters:</p>
<pre name="code" class="java">class ChainedRectangle
{
    protected int x;
    public ChainedRectangle setX(int x) { this.x = x; return this; }

    protected int y;
    public ChainedRectangle setY(int y) { this.y = y; return this; }

    protected int width;
    public ChainedRectangle setWidth(int width) { this.width = width; return this; }

    protected int height;
    public ChainedRectangle setHeight(int height) { this.height = height; return this; }

    protected String color;
    public ChainedRectangle setColor(String color) { this.color = color; return this; }
}
</pre>
<p>Then if we need to build a rectangle we can write a single line for setting all the required values:</p>
<pre name="code" class="java">new         Rectangle().setX(10).setY(10).setWidth(13).setHeight(15).setColor("red");</pre>
<p>Along with autocomplete option in most of todays IDEs method chaining might provide a suggestive way of doing some actions in less code.<br />
<span id="more-64"></span><br />
For example we can write a simple class to build  sql queries. Then we can invoke in a simple way.</p>
<pre name="code" class="java">public class Query
{
    protected String queryString = "";

    public Query select(String what)
    {
        queryString = "SELECT " + what;
    }

    public Query from(String from)
    {
        queryString = " FROM " + from;
    }

    public Query where(String where)
    {
        queryString = " WHERE " + where;
    }

    public ResultSet Invoke()
    {
        //... database invocation bla bla
        return result;
    }
}</pre>
<p>Then, to build the query we invoke:</p>
<pre name="code" class="java">new Query().select("*").from("table").where("a=b").Invoke();</pre>
<p>One of the problems of the Method Chaining pattern is the fact that it can not ensure the order of the invoked method. In order to do that each method can return a specific interface object which implements only the allowed methods.</p>
<p>For example we define an interface ISelectedQuery with a single method from, IFromQuery with a single method where. ISelectedQuery.from returns and object of type IFromQuery and IFromQuery returns a Query object. All the interfaces are implemented by the Query object. That way the order of invoking the methods is ensured.</p>
<p>Here is how the code looks like:</p>
<pre name="code" class="java">public interface ISelectedQuery
{
    IFromQuery from(String from);
}

public interface IFromQuery
{
    Query where(String where);
}

 public class Query implements ISelectedQuery, IFromQuery
{
    protected String queryString = "";

    public ISelectedQuery select(String what)
    {
        queryString = "SELECT " + what;
        return this;
    }

    public IFromQuery from(String from)
    {
        queryString = " FROM " + from;
        return this;
    }

    public Query where(String where)
    {
        queryString = " WHERE " + where;
        return this;
    }

    public ResultSet Invoke()
    {
        //... database invocation bla bla
        return result;
    }
}</pre>
<p>The above code ensure that only the right methods can be invoked. Further more the syntax autocomplete option from most of today IDEs will show in the drop down only the right method(s).</p>
<div style="text-align:center;"><a href="http://api.tweetmeme.com/share?url=http://www.factorypattern.com/method-chaining/"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://www.factorypattern.com/method-chaining/" height="61" width="51" /></a></div><h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li>No Related Post</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.factorypattern.com/method-chaining/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Using VBS to set the environment variables</title>
		<link>http://www.factorypattern.com/using-vbs-to-set-the-environment-variables/</link>
		<comments>http://www.factorypattern.com/using-vbs-to-set-the-environment-variables/#comments</comments>
		<pubDate>Thu, 18 Sep 2008 18:22:54 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[environment variables]]></category>

		<guid isPermaLink="false">http://www.factorypattern.com/using-vbs-to-set-the-environment-variables/</guid>
		<description><![CDATA[I use windows and I hate when I have to switch on another computer, or when I have to do any change related to windows especially changing the environment variables. I like to download the tool, framework, &#8230; I  use as a zip and then to do minimal operations to install it. The thing [...]]]></description>
			<content:encoded><![CDATA[<p>I use windows and I hate when I have to switch on another computer, or when I have to do any change related to windows especially changing the environment variables. I like to download the tool, framework, &#8230; I  use as a zip and then to do minimal operations to install it. The thing I hate the most is setting environment variables, and path in that small dialog window. I have all my java applications and frameworks in one single directory so the configuration depends only on the thing I hate most: thats right, environment variables.<br />
<span id="more-34"></span><br />
Now I decided to move them all in one single file, and I&#8217;m using Vbs for that. Don&#8217;t hurry to blame me for that, I&#8217;m a java programmer but in this case is the best solution. If you take a look on wikipedia you&#8217;ll see that &#8220;VBScript is installed by default in every desktop release of the Windows Operating System (OS) since Windows 98&#8243;. That&#8217;s all I need to know about VBScript to make me use it to create the script that will help me to put my java tools on every windows computer I can access, without having to add the env variables manually(muahahahahaa). Unfortunately the computer requires a restart.</p>
<p>Now, lets cut the chat. Here is the script(maven-env.vbs) which sets the environments required by maven2 according to the <a href="http://maven.apache.org/download.html#Installation">instructions</a> and add the bin folder to the class path:</p>
<pre><code>Dim WSHShell
Set WSHShell = WScript.CreateObject("WScript.Shell")

WScript.Echo "The current PATH is:" &amp; chr(10) &amp; chr(10) &amp; Replace(WSHShell.Environment.item("PATH"),";", chr(10))

WScript.Echo "Current values of the variables to be changed or added:" &amp; chr(10) _
	&amp; "M2_HOME=" &amp; WSHShell.Environment.item("M2_HOME") &amp; chr(10) _
	&amp; "M2=" &amp; WSHShell.Environment.item("M2")
	
'maven environment variables are created according to http://maven.apache.org/download.html#Installation
WSHShell.Environment.item("M2_HOME") = "C:\java\apache-maven-2.0.9"
WSHShell.Environment.item("M2") = "%M2_HOME%\bin"
WSHShell.Environment.item("PATH") = WSHShell.Environment.item("path") &amp; ";%M2%"

Set WSHShell = Nothing    
WScript.Quit(0)</code></pre>
<p>I&#8217;m using the first echo line in a separate script to display the path and to display one directory on each line replacing ; with end of line(chr(0) represents end of line). If you want to change registry values you can use:</p>
<pre><code>WSHShell.RegWrite "HKCU\path\key", "value"
WSHShell.RegRead("HKCU\path\key")</code></pre>
<p>If you need to know that script for more advanced operation you can check <a href="http://www.w3schools.com/VBscript/vbscript_ref_functions.asp">VBScript Functions</a></p>
<div style="text-align:center;"><a href="http://api.tweetmeme.com/share?url=http://www.factorypattern.com/using-vbs-to-set-the-environment-variables/"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://www.factorypattern.com/using-vbs-to-set-the-environment-variables/" height="61" width="51" /></a></div><h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li>No Related Post</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.factorypattern.com/using-vbs-to-set-the-environment-variables/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Wrapping (evil)checked exceptions in Java</title>
		<link>http://www.factorypattern.com/wrapping-checked-exceptions-java/</link>
		<comments>http://www.factorypattern.com/wrapping-checked-exceptions-java/#comments</comments>
		<pubDate>Mon, 25 Aug 2008 21:44:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[java foundation]]></category>

		<guid isPermaLink="false">http://www.factorypattern.com/wrapping-evilchecked-exceptions-in-java/</guid>
		<description><![CDATA[What?
There are various reasons when we have to wrap java exceptions. Often those reasons are called: Checked Exceptions. According to the definition there are 2 types of exceptions:

Checked Exceptions &#8211; Exceptions that are not descending from RuntimeException are called checked exceptions. Checked exceptions must be handled by a try catch mechanism or by adding the [...]]]></description>
			<content:encoded><![CDATA[<p><strong>What?</strong></p>
<p>There are various reasons when we have to wrap java exceptions. Often those reasons are called: <strong>Checked Exceptions</strong>. According to the definition there are 2 types of exceptions:<span id="more-31"></span></p>
<ul>
<li><strong>Checked Exceptions</strong> &#8211; Exceptions that are not descending from RuntimeException are called checked exceptions. Checked exceptions must be handled by a try catch mechanism or by adding the throws declaration to the method. If neither of those is done then the compiler throws an exception. </li>
<li><strong>Unchecked Exceptions</strong> &#8211; The exceptions that are not checked, of course. They are inherited from RuntimeException and no handling is enforced by the compiler. Simply put you don&#8217;t have to check them.</li>
</ul>
<p><strong>But Why?</strong></p>
<p>In practice using checked exceptions will lead to undesired behavior. Here are a few scenarios:</p>
<ul>
<li>
<p>Let&#8217;s assume we have to use a method called <em>getProperty(String key) throws SQLException</em> defined in DBConfig to read a color property from the database and then to display it on screen. We instantiate the config object and then invoke the method. For avoiding a compiler exception we&#8217;ll add a throws SQLException to our method declaration. And then imagine that you have to add another one to the method invoking the method we wrote. The more mothods are involved the more checked exceptions added to the method declaration. It doesn&#8217;t make sense for a method called paintScreen to force you to handle excpetions like SQLException or IOException, doesn&#8217;t it?</p>
</li>
<li>
<p>Now we are going to add an interface named IConfig. We&#8217;ll have the existing class DBConfig and we&#8217;ll create another one FileConfig. The first one will throw SQLException and the second one IOException. If we&#8217;ll have too many checked exceptions we&#8217;ll pollute the interface with exceptions that might depend on implementation, inducing a tight coupling in our classes: the abstraction depends on modules exceptions. Pretty bad, isn&#8217;t it?</p>
</li>
<li>
<p>
Now the bad practice: We&#8217;ll not going to use throws declaration. Not at all, because it generates too many problems. So let&#8217;s just try, catch, printstacktrace and that&#8217;s it. We&#8217;ll see later what to do. This is fast. This practice is again bad, because it just hide the problems. Especially in large applications with many programmers they tend to forget about the caught exceptions and real problems are not discovered. After all remember that exceptions are meant to signal a real abnormal behavior of the application.</p>
</li>
<li>
<p>
There is also another reason that make us wrapping exceptions which is not related to checked or unchecked exceptions. Sometimes we simply want to hide and separate the implementation details from the exposed interface in order to reduce the complexity and to simplify the interface.</p>
</li>
</ul>
<p><strong>Ok, then. But How?</strong></p>
<p>We have 2 simple options:</p>
<ul>
<li>To wrap the exceptions in such a way that the original exception object is not used.and only the message as String is encapsulated in the new exception.</li>
<li>Create another exception that contains references to the thrown exception</li>
</ul>
<p>There is a very thin line between those 2 options and you have to understand it very well. First of all let&#8217;s think about it. The module which invokes the code throwing exceptions will have the wrapped class in the class path? If the response is definitely an yes then go for second point, otherwise choose the first one. </p>
<p>For example a client invokes remotely your api using rmi. The api throws the exception wrapped but the client have only the wrapper exception class in the classpath, not the wrapped one. An exception will be thrown. In cases like this one runtime exception should be created containing a descriptive message and should not wrap the original exception.</p>
<div style="text-align:center;"><a href="http://api.tweetmeme.com/share?url=http://www.factorypattern.com/wrapping-checked-exceptions-java/"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://www.factorypattern.com/wrapping-checked-exceptions-java/" height="61" width="51" /></a></div><h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li>No Related Post</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.factorypattern.com/wrapping-checked-exceptions-java/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Storing parameters in web.xml: context-param &amp; init-param</title>
		<link>http://www.factorypattern.com/storing-parameters-in-webxml-context-param-init-param/</link>
		<comments>http://www.factorypattern.com/storing-parameters-in-webxml-context-param-init-param/#comments</comments>
		<pubDate>Mon, 04 Aug 2008 22:48:16 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[java snippets]]></category>
		<category><![CDATA[java web apps]]></category>

		<guid isPermaLink="false">http://www.factorypattern.com/storing-parameters-in-webxml-context-param-init-param/</guid>
		<description><![CDATA[There are two options to store parameters in web.xml:
- context parameters &#8211; available to the entire scope of the web application
- init parameters &#8211; available in the context of a servlet or filter in the web application
Context Parameters

&#60;?xml version="1.0" encoding="UTF-8"?&#62;
&#60;web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&#62;
  &#60;context-param&#62;
    &#60;description&#62;This is a context [...]]]></description>
			<content:encoded><![CDATA[<p>There are two options to store parameters in web.xml:<br />
- <strong>context parameters</strong> &#8211; available to the entire scope of the web application<br />
- <strong>init parameters</strong> &#8211; available in the context of a servlet or filter in the web application</p>
<h2>Context Parameters</h2>
<p><span id="more-30"></span></p>
<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&gt;
  &lt;context-param&gt;
    &lt;description&gt;This is a context parameter example&lt;/description&gt;
    &lt;param-name&gt;ContextParam&lt;/param-name&gt;
    &lt;param-value&gt;ContextParam value&lt;/param-value&gt;
  &lt;/context-param&gt;
...
&lt;web-app&gt;</code></pre>
<p>The following code can be be invoked from a servlet or a filter to retrieve the ContextParam value. The parameter can be read successfully from any servlet or filter class.</p>
<pre><code>@Override
	public void init(ServletConfig config) throws ServletException {
		String contextParam = config.getServletContext().getInitParameter("ContextParam");
	}

//or
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String contextParam = this.getServletContext().getInitParameter("ContextParam");
		...
	}
	...
}

String value = this.getServletContext().getInitParameter("ContextParam");</code></pre>
<h2>Init Parameters</h2>
<pre><code>...
&lt;servlet&gt;
    &lt;servlet-name&gt;A Servlet&lt;/servlet-name&gt;
    &lt;servlet-class&gt;com.controller.TestServlet&lt;/servlet-class&gt;
    &lt;init-param&gt; 
        &lt;description&gt;This is an init parameter example&lt;/description&gt; 
        &lt;param-name&gt;InitParam&lt;/param-name&gt; 
        &lt;param-value&gt;init param value&lt;/param-value&gt; 
    &lt;/init-param&gt; 
&lt;/servlet&gt;
...</code></pre>
<p>The following code can be be invoked to retrieve the value of InitParam. The parameter can be accessed only from com.controller.TestServlet.</p>
<pre><code>public class DefaultController extends HttpServlet
{
	@Override
	public void init(ServletConfig config) throws ServletException {
		String initParam  = config.getInitParameter("InitParam");
	}

//or
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String initParam = getServletConfig().getInitParameter("InitParam");
		...
	}
	...
}</code></pre>
<h2>Iterating through context-params and init-params</h2>
<p>It&#8217;s possible to iterate through the paramters if required:</p>
<pre><code>public class DefaultController extends HttpServlet
{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Enumeration contextParams = getServletContext().getInitParameterNames();

		Sytem.out.println("context-params: ");
		while (params.hasMoreElements()) {
			String name = (String) params.nextElement();
			Sytem.out.println(name + " = " + config.getInitParameter(name));
		}

		Enumeration initParams = getServletConfig().getInitParameterNames();

		Sytem.out.println("init-params: ");
		while (params.hasMoreElements()) {
			String name = (String) params.nextElement();
			Sytem.out.println(name + " = " + config.getInitParameter(name));
		}

		...
	}
	...
}</code></pre>
<div style="text-align:center;"><a href="http://api.tweetmeme.com/share?url=http://www.factorypattern.com/storing-parameters-in-webxml-context-param-init-param/"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://www.factorypattern.com/storing-parameters-in-webxml-context-param-init-param/" height="61" width="51" /></a></div><h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li>No Related Post</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.factorypattern.com/storing-parameters-in-webxml-context-param-init-param/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Multimap in Google Collections Library</title>
		<link>http://www.factorypattern.com/multimap-in-google-collections-library/</link>
		<comments>http://www.factorypattern.com/multimap-in-google-collections-library/#comments</comments>
		<pubDate>Thu, 27 Mar 2008 16:02:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Java Frameworks]]></category>
		<category><![CDATA[Google Collections]]></category>

		<guid isPermaLink="false">http://www.factorypattern.com/multimap-in-google-collections-library/</guid>
		<description><![CDATA[<p> I just took the Google Collections Library and start playing with it. I discovered a class I needed in many and many situations in the past. I’m talking about Multimap. Actually Multimap is an interface having a few classes implementing it. Along with some other interfaces it comes to fulfill a gap in the Java Collections. <span id="more-18"></span></p>
<p>Before giving a few examples about it, a few words about Google Collections. This is a really light java framework, build in one jar of about 300 kb. Currently only the Alpha version is released, but it doesn’t mean it’s not tested. In the <a href="http://code.google.com/p/google-collections/wiki/Faq">Google Collection FAQ</a> on <a href="http://code.google.com/">Google Code</a> the authors explain what 0.5 Alpha means” “We already use this library extensively in production for services like GMail, Reader, Blogger, Docs &#038; Spreadsheets, AdWords, AdSense and dozens more. We consider it to be pretty safe. However, during the alpha period, we do reserve the right to make changes of any kind to it at any time.”</p>
<p>All you have to do is to download the latest <a href="http://code.google.com/p/google-collections/">Google Snapshot</a>, unzip google-collect-snapshot-20080321.jar and then add it to your classpath. In our sample we are going to use one class and one interface (classes link to javadocs): <a href="http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/HashMultimap.html">com.google.common.collect.HashMultimap</a>, <a href="http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/Multimap.html">com.google.common.collect.Multimap</a>. Here is an example of Google Collection in action:</p>
<pre class="prettyprint">Multimap multimap = new HashMultimap();
multimap.put("key1", "value1A");
multimap.put("key2", "value1A");
multimap.put("key1", "value1B");
System.out.println(multimap.get("key1").toString());</pre>
<p>Something similar using Generics:</p>
<pre class="prettyprint">Multimap<String,
Multimap<String,String>> peoples = new HashMultimap<String,Multimap<String,String>>();

Multimap<String,String> vehicles = new HashMultimap<String,String>();
vehicles.put("Ferrari", "Silver Ferrari");
vehicles.put("Ferrari", "Red Ferrari");
vehicles.put("Car", "Honda Civic");
vehicles.put("Bycicle", "33 Gears Bycicle");
peoples.put("Kevin S.", vehicles);

System.out.println(peoples);</pre>
<p>And the final snippet using some classes created by us. We use Google Collection to keep some existing ‘has’ relations:</p>
<pre class="prettyprint">Multimap<Person,
Multimap<String,Vehicle>> samepeoples = new HashMultimap<Person,Multimap<String,Vehicle>>();
Multimap<String,Vehicle> newvehicles = new HashMultimap<String,Vehicle>();
newvehicles.put("Porsche", new Car("Silver Porsche"));
newvehicles.put("Porsche", new Car("Red Porsche"));
newvehicles.put("Car", new Car("Honda Civic"));
newvehicles.put("Bycicle", new Bycicle("33 Gears Bycicle"));
samepeoples.put(new Person("Kevin S."), newvehicles);

System.out.println(samepeoples);</pre>
<p>The result of the test program printed to the console:</p>
<pre>[value1B, value1A]
{Kevin S.=[{Car=[Honda Civic], Ferrari=[Red Ferrari, Silver Ferrari], Bycicle=[33 Gears Bycicle]}]}
{Kevin S.=[{Car=[Honda Civic], Porsche=[Silver Porsche, Red Porsche], Bycicle=[33 Gears Bycicle]}]}</pre>
<p>Here are the classes I created to for the previous snippet:</p>
<pre class="prettyprint">class Person {
	private String name;
	public Person(String name)
	{
		this.name = name;
	}
	public String toString()
	{
		return name;
	}
}
class Vehicle {
	private String name;
	public Vehicle(String name)
	{
		this.name = name;
	}
	public String toString()
	{
		return name;
	}
}
class Car extends Vehicle {
	public Car(String name)
	{
		super(name);
	}
}
class Bycicle extends Vehicle {
	public Bycicle(String name)
	{
		super(name);
	}
}</pre>
<p>Many situations occurred in the past when I had to organize some data in some hierarchical structures. I didn’t want to create some classes keep hierarchical structures so I used hashmaps, hashtables or even sets or lists, storing Set or some other collection as values. Some sort of sets of sets, maps of lists and so on. The Multimap comes to fulfill this need and opens the way for a multitude of uses of collections. Until now, when I thought how I used the collections I felt a little bit that I&#8217;m doing something wrong since is not something that Java Collection was providing. Now, that I know that Google has this class I feel better.</p>
<p>Just think to the coupling degree if we modify the classes to keep association relations. For example a Person class should be enhanced to keep collection of Vehicles so the person class will be coupled with the Vehicle interface. If we just want to  add some 1-to-many relations between some existing classes, changing them might not be the best way of doing it. </p>
Related PostsHow to Read/Write Java Properties FilesGoogle Guice TutorialHow To Use AntHow to write an Ant xml build file to Generate JAXB Code and Compile and Build it to a Jar]]></description>
			<content:encoded><![CDATA[<p> I just took the Google Collections Library and start playing with it. I discovered a class I needed in many and many situations in the past. I’m talking about Multimap. Actually Multimap is an interface having a few classes implementing it. Along with some other interfaces it comes to fulfill a gap in the Java Collections. <span id="more-18"></span></p>
<p>Before giving a few examples about it, a few words about Google Collections. This is a really light java framework, build in one jar of about 300 kb. Currently only the Alpha version is released, but it doesn’t mean it’s not tested. In the <a href="http://code.google.com/p/google-collections/wiki/Faq">Google Collection FAQ</a> on <a href="http://code.google.com/">Google Code</a> the authors explain what 0.5 Alpha means” “We already use this library extensively in production for services like GMail, Reader, Blogger, Docs &#038; Spreadsheets, AdWords, AdSense and dozens more. We consider it to be pretty safe. However, during the alpha period, we do reserve the right to make changes of any kind to it at any time.”</p>
<p>All you have to do is to download the latest <a href="http://code.google.com/p/google-collections/">Google Snapshot</a>, unzip google-collect-snapshot-20080321.jar and then add it to your classpath. In our sample we are going to use one class and one interface (classes link to javadocs): <a href="http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/HashMultimap.html">com.google.common.collect.HashMultimap</a>, <a href="http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/Multimap.html">com.google.common.collect.Multimap</a>. Here is an example of Google Collection in action:</p>
<pre class="prettyprint">Multimap multimap = new HashMultimap();
multimap.put("key1", "value1A");
multimap.put("key2", "value1A");
multimap.put("key1", "value1B");
System.out.println(multimap.get("key1").toString());</pre>
<p>Something similar using Generics:</p>
<pre class="prettyprint">Multimap<String,
Multimap<String,String>> peoples = new HashMultimap<String,Multimap<String,String>>();

Multimap<String,String> vehicles = new HashMultimap<String,String>();
vehicles.put("Ferrari", "Silver Ferrari");
vehicles.put("Ferrari", "Red Ferrari");
vehicles.put("Car", "Honda Civic");
vehicles.put("Bycicle", "33 Gears Bycicle");
peoples.put("Kevin S.", vehicles);

System.out.println(peoples);</pre>
<p>And the final snippet using some classes created by us. We use Google Collection to keep some existing ‘has’ relations:</p>
<pre class="prettyprint">Multimap<Person,
Multimap<String,Vehicle>> samepeoples = new HashMultimap<Person,Multimap<String,Vehicle>>();
Multimap<String,Vehicle> newvehicles = new HashMultimap<String,Vehicle>();
newvehicles.put("Porsche", new Car("Silver Porsche"));
newvehicles.put("Porsche", new Car("Red Porsche"));
newvehicles.put("Car", new Car("Honda Civic"));
newvehicles.put("Bycicle", new Bycicle("33 Gears Bycicle"));
samepeoples.put(new Person("Kevin S."), newvehicles);

System.out.println(samepeoples);</pre>
<p>The result of the test program printed to the console:</p>
<pre>[value1B, value1A]
{Kevin S.=[{Car=[Honda Civic], Ferrari=[Red Ferrari, Silver Ferrari], Bycicle=[33 Gears Bycicle]}]}
{Kevin S.=[{Car=[Honda Civic], Porsche=[Silver Porsche, Red Porsche], Bycicle=[33 Gears Bycicle]}]}</pre>
<p>Here are the classes I created to for the previous snippet:</p>
<pre class="prettyprint">class Person {
	private String name;
	public Person(String name)
	{
		this.name = name;
	}
	public String toString()
	{
		return name;
	}
}
class Vehicle {
	private String name;
	public Vehicle(String name)
	{
		this.name = name;
	}
	public String toString()
	{
		return name;
	}
}
class Car extends Vehicle {
	public Car(String name)
	{
		super(name);
	}
}
class Bycicle extends Vehicle {
	public Bycicle(String name)
	{
		super(name);
	}
}</pre>
<p>Many situations occurred in the past when I had to organize some data in some hierarchical structures. I didn’t want to create some classes keep hierarchical structures so I used hashmaps, hashtables or even sets or lists, storing Set or some other collection as values. Some sort of sets of sets, maps of lists and so on. The Multimap comes to fulfill this need and opens the way for a multitude of uses of collections. Until now, when I thought how I used the collections I felt a little bit that I&#8217;m doing something wrong since is not something that Java Collection was providing. Now, that I know that Google has this class I feel better.</p>
<p>Just think to the coupling degree if we modify the classes to keep association relations. For example a Person class should be enhanced to keep collection of Vehicles so the person class will be coupled with the Vehicle interface. If we just want to  add some 1-to-many relations between some existing classes, changing them might not be the best way of doing it. </p>
<div style="text-align:center;"><a href="http://api.tweetmeme.com/share?url=http://www.factorypattern.com/multimap-in-google-collections-library/"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://www.factorypattern.com/multimap-in-google-collections-library/" height="61" width="51" /></a></div><h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li><a href="http://www.factorypattern.com/how-to-readwrite-java-properties-files/" title="How to Read/Write Java Properties Files">How to Read/Write Java Properties Files</a></li><li><a href="http://www.factorypattern.com/google-guice-tutorial/" title="Google Guice Tutorial">Google Guice Tutorial</a></li><li><a href="http://www.factorypattern.com/how-to-use-ant/" title="How To Use Ant">How To Use Ant</a></li><li><a href="http://www.factorypattern.com/how-to-write-ant-xml-build-file-generate-jaxb-code-compile-build-jar/" title="How to write an Ant xml build file to Generate JAXB Code and Compile and Build it to a Jar">How to write an Ant xml build file to Generate JAXB Code and Compile and Build it to a Jar</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.factorypattern.com/multimap-in-google-collections-library/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Google Guice Starts a New Google Age?</title>
		<link>http://www.factorypattern.com/google-guice-starts-a-new-google-age/</link>
		<comments>http://www.factorypattern.com/google-guice-starts-a-new-google-age/#comments</comments>
		<pubDate>Wed, 19 Mar 2008 20:22:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[guice]]></category>

		<guid isPermaLink="false">http://www.factorypattern.com/google-guice-starts-a-new-google-age/</guid>
		<description><![CDATA[<p> Google Guice is a an open source Java framework released by Google under Apache License 2.0. Guice is a light weight Inversion of Control Container. It does what Spring does regarding dependency inversion aspect, without using xml files. Guide uses annotations and requires Java 1.5 or above and follows the Google philosophy of being really easy to use.</p>
<p><span id="more-15"></span></p>
<p>Skipping the technical aspect, I can not ignore a few points.<br />
1.  Google releases a Java framework, produced in their free open source code forge, aiming a &#8220;market&#8221; where there is only one &#8220;competitor&#8221;, Spring, the only Java IOC Container truly widely used. Guice is not a real Google product, it is resulted from their initiative to support the open source community. The interesting aspect here is the fact that the owner of the project is Google itself, Guice is not only an open source framework hosted on Google Code.</p>
<p>2. Spring is a framework used to integrate any other framework. Guice is intended to be the same. Entire web and enterprise applications can be built on it. It&#8217;s something that everyone needs.</p>
<p>3. Guice is more close to the conventional applications, it is not an MashUp Api and is not based on google products like most of the api&#8217;s made available for the masses, it&#8217;s a framework than can be used for any Java application. In other words Google expands its boundaries toward a more traditional software community.</p>
<p>Guice seems to be really nice and light and many programmers prefer using it, writing java code instead of changing xml files in Spring. You can check the google tech talks presentation on google video:<br />
<embed src="http://video.google.com/googleplayer.swf?docId=6068447410873108038&amp;hl=en" style="width: 420px; height: 339px" id="VideoPlayback" type="application/x-shockwave-flash"></embed></p>
<p>For more details: <a href="http://code.google.com/p/google-guice/">Google Guice Home Page</a><br />
There are many questions emerging here. I&#8217;m asking if Google decided to produce open-source software or it&#8217;s just supporting the open source community. Are we going to live in a java world where we can use many frameworks called Google Something? Recently Google Collections for Java was released; now it&#8217;s just version 0.5, but like Guice it&#8217;s a framework that can be the foundation of any application and has the characteristics to achieve the world domination. For a good cause of course.</p>
Related PostsGuice Servlets IntegrationGoogle Guice Tutorial]]></description>
			<content:encoded><![CDATA[<p> Google Guice is a an open source Java framework released by Google under Apache License 2.0. Guice is a light weight Inversion of Control Container. It does what Spring does regarding dependency inversion aspect, without using xml files. Guide uses annotations and requires Java 1.5 or above and follows the Google philosophy of being really easy to use.</p>
<p><span id="more-15"></span></p>
<p>Skipping the technical aspect, I can not ignore a few points.<br />
1.  Google releases a Java framework, produced in their free open source code forge, aiming a &#8220;market&#8221; where there is only one &#8220;competitor&#8221;, Spring, the only Java IOC Container truly widely used. Guice is not a real Google product, it is resulted from their initiative to support the open source community. The interesting aspect here is the fact that the owner of the project is Google itself, Guice is not only an open source framework hosted on Google Code.</p>
<p>2. Spring is a framework used to integrate any other framework. Guice is intended to be the same. Entire web and enterprise applications can be built on it. It&#8217;s something that everyone needs.</p>
<p>3. Guice is more close to the conventional applications, it is not an MashUp Api and is not based on google products like most of the api&#8217;s made available for the masses, it&#8217;s a framework than can be used for any Java application. In other words Google expands its boundaries toward a more traditional software community.</p>
<p>Guice seems to be really nice and light and many programmers prefer using it, writing java code instead of changing xml files in Spring. You can check the google tech talks presentation on google video:<br />
<embed src="http://video.google.com/googleplayer.swf?docId=6068447410873108038&amp;hl=en" style="width: 420px; height: 339px" id="VideoPlayback" type="application/x-shockwave-flash"></embed></p>
<p>For more details: <a href="http://code.google.com/p/google-guice/">Google Guice Home Page</a><br />
There are many questions emerging here. I&#8217;m asking if Google decided to produce open-source software or it&#8217;s just supporting the open source community. Are we going to live in a java world where we can use many frameworks called Google Something? Recently Google Collections for Java was released; now it&#8217;s just version 0.5, but like Guice it&#8217;s a framework that can be the foundation of any application and has the characteristics to achieve the world domination. For a good cause of course.</p>
<div style="text-align:center;"><a href="http://api.tweetmeme.com/share?url=http://www.factorypattern.com/google-guice-starts-a-new-google-age/"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://www.factorypattern.com/google-guice-starts-a-new-google-age/" height="61" width="51" /></a></div><h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li><a href="http://www.factorypattern.com/guice-servlets/" title="Guice Servlets Integration">Guice Servlets Integration</a></li><li><a href="http://www.factorypattern.com/google-guice-tutorial/" title="Google Guice Tutorial">Google Guice Tutorial</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.factorypattern.com/google-guice-starts-a-new-google-age/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How To Use Ant</title>
		<link>http://www.factorypattern.com/how-to-use-ant/</link>
		<comments>http://www.factorypattern.com/how-to-use-ant/#comments</comments>
		<pubDate>Tue, 18 Mar 2008 15:05:50 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Ant]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[How To Ant]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.factorypattern.com/how-to-use-ant/</guid>
		<description><![CDATA[Ant is a build tool written in Java, intended to be used for Java build processes. Ant is used for building small and large projects as well. You can use ant not only for automation of building Java projects, but for automating any complicated and repetitive tasks. Ant is using xml files to define tasks [...]]]></description>
			<content:encoded><![CDATA[<p>Ant is a build tool written in Java, intended to be used for Java build processes. Ant is used for building small and large projects as well. You can use ant not only for automation of building Java projects, but for automating any complicated and repetitive tasks. Ant is using xml files to define tasks that should be run during a process.<span id="more-13"></span></p>
<p><H3>How To Install Ant</H3></p>
<p>Ant can be installed on almost any operating system in a few easy steps:</p>
<ol>
<li>Install Java Runtime Environment(JRE) or Java Development Kit(JDK). Ant need a JRE or JDK in order to run.</li>
<li>2. Download the ant binary distribution from this <a href="http://ant.apache.org/bindownload.cgi">link</a> and unzip it to a directory of your choice.</li>
<li>3. Add the location of the bin folder to the path environment variable. For example in windows: <em>PATH=%PATH%;c:\ant\bin</em></li>
<li>4. Set ANT_HOME environment variable to the location where ant is installed. For example: <em>ANT_HOME=c:\ant</em></li>
</ol>
<p><H3>How to create your first ANT project</H3></p>
<p>Any ant project consist of one or more xml files containing the ant tasks. Lets create a small file named &#8220;build.xml&#8221;, containing only one task to print a small &#8220;Hello World!!!&#8221;. In order to run you have to go to the directory containing the build.xml file and type and run &#8220;ant&#8221;. </p>
<pre><code>&lt;project name="how-to-use-ant" basedir="." default="hello-world"&gt;

    &lt;target name="hello-world"&gt;
        &lt;echo message="Hello World!!!"/&gt;
    &lt;/target&gt;
	
&lt;/project&gt;</code></pre>
<p>As you noticed we defined in this and project  one task named <em>hello-world</em> and this is the default task. If everything goes well, the ant will run (we added in the path the ant bin directory when we installed it) and will print the following text:</p>
<pre><code>Buildfile: build.xml

hello-world:
     [echo] Hello World!!!

BUILD SUCCESSFUL
Total time: 0 seconds</code></pre>
<p><H3>How To Compile and Build Using Ant</H3></p>
<p>In real life we use Ant as the build tool for our applications and Ant is used in 90 percent of the cases for that. I&#8217;ve created a separate post to serve as a tutorial. You can check <a href="http://www.factorypattern.com/how-to-create-jar-files-from-ant/">How to Create Jar Files from Ant</a>.</p>
<p>Lets take this time in consideration a real life project. Most of the time Ant is used to compile and build java projects. This means generating, compiling and building jar files. For this we are going to define severals tasks. Lets consider the we create a buid.xml file in the directory where sources are located. The sources are put in the subdirectory called <em>src</em>. We also need to include a jar in the classpath while compiling. We are going to create a separate property file to define the location of this file:</p>
<pre><code>#===================================
#	File Name: build.properties
#===================================

env.LIB=c://java//thirdparty//lib</code></pre>
<p>The ant task contains a few tasks for doing the following operations:<br />
1. Clean the build directory if exists.<br />
2. Create the build directory where the new classes will be built.<br />
3. Compile the java classes.<br />
4. Build a jar file from the java classes.</p>
<p>Here is the build.xml file. Note the dependencies between the tasks and how the properties are defined in the ant xml file or loaded from the properties file:</p>
<pre><code>&lt;project name="how-to-use-ant" basedir="." default="hello-world"&gt;

	&lt;property name="build" value="build"/&gt;
	&lt;property file="build.properties"/&gt;
	
	&lt;target name="clean"&gt;
		&lt;delete dir="${build}"/&gt;
	&lt;/target&gt;

	&lt;target name="init" depends="clean"&gt;
		&lt;mkdir dir="${build}"/&gt;
	&lt;/target&gt;	
	
	&lt;target name="compile" depends="init"&gt;
		&lt;!-- Compile the java code --&gt;
		&lt;javac srcdir="src" destdir="${build}"&gt;
			&lt;classpath&gt;
				&lt;pathelement location="${env.LIB}/required-jar1.jar"/&gt;
				&lt;pathelement location="${env.LIB}/required-jar2.jar"/&gt;
			&lt;/classpath&gt;
		&lt;/javac&gt;
	&lt;/target&gt;	
	
	&lt;target name="jar" depends="compile"&gt;
		&lt;!-- Build the jar file --&gt;
		&lt;jar basedir="${build}" destfile="${build}/new-jar.jar"/&gt;
	&lt;/target&gt;
	
&lt;/project&gt;</code></pre>
<p>Now that you&#8217;ve seen how to create an ant task you might ask why you need an ant build file for building a jar file. This can be done as well with a few batch commands which will take only a 4 lines bat file. There are certain advantages. The ant files can be easily parametrized and reused, sub projects cand be created, lots of tasks are implemented, covering most of the operations you want to do (junits, sql operations, zip file tasks, tasks for replacing and editing text and xml files, &#8230;) and new tasks can be implemented in java and used in any ant file.</p>
<div style="text-align:center;"><a href="http://api.tweetmeme.com/share?url=http://www.factorypattern.com/how-to-use-ant/"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://www.factorypattern.com/how-to-use-ant/" height="61" width="51" /></a></div><h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li><a href="http://www.factorypattern.com/how-to-write-ant-xml-build-file-generate-jaxb-code-compile-build-jar/" title="How to write an Ant xml build file to Generate JAXB Code and Compile and Build it to a Jar">How to write an Ant xml build file to Generate JAXB Code and Compile and Build it to a Jar</a></li><li><a href="http://www.factorypattern.com/how-to-create-jar-files-from-ant/" title="How to Create Jar Files from Ant">How to Create Jar Files from Ant</a></li><li><a href="http://www.factorypattern.com/howto-generate-hibernate-pojo-and-mapping-files-using-ant-from-a-db-schema/" title="Howto generate Hibernate POJO and mapping files using ant from a db schema">Howto generate Hibernate POJO and mapping files using ant from a db schema</a></li><li><a href="http://www.factorypattern.com/how-to-readwrite-java-properties-files/" title="How to Read/Write Java Properties Files">How to Read/Write Java Properties Files</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.factorypattern.com/how-to-use-ant/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to write an Ant xml build file to Generate JAXB Code and Compile and Build it to a Jar</title>
		<link>http://www.factorypattern.com/how-to-write-ant-xml-build-file-generate-jaxb-code-compile-build-jar/</link>
		<comments>http://www.factorypattern.com/how-to-write-ant-xml-build-file-generate-jaxb-code-compile-build-jar/#comments</comments>
		<pubDate>Mon, 17 Mar 2008 15:06:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Ant]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Build]]></category>
		<category><![CDATA[compile]]></category>
		<category><![CDATA[jar]]></category>
		<category><![CDATA[Jaxb]]></category>
		<category><![CDATA[packaging]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.factorypattern.com/how-to-write-an-ant-xml-build-file-to-generate-jaxb-code-and-build-jar/</guid>
		<description><![CDATA[There is a pretty common situation to use a java classes generator and to create classes based on a defined pattern. Hibernate, Castor, Jaxb are a few examples. I prefer to have those classes packed in a separate jar file. Since they are generated files it&#8217;s not a good practice to modify them, so there [...]]]></description>
			<content:encoded><![CDATA[<p>There is a pretty common situation to use a java classes generator and to create classes based on a defined pattern. Hibernate, Castor, Jaxb are a few examples. I prefer to have those classes packed in a separate jar file. Since they are generated files it&#8217;s not a good practice to modify them, so there is not need to have them in your project. Debugging is the case when you need the source of generated classes. In this case you can simply attach the jar containing sources in your IDE (eclipse supports it, I think other IDE support it either). <span id="more-14"></span></p>
<p>Mainly you have to use 2 ant tasks after the java code is generated, one for compiling the classes, the other one to package them. In the following example I&#8217;m using ant to generate some jaxb classes, then to compile them and build a jar file. In this task there are 2 ant tasks, named compile and jar, responsible for compiling and generating the ant file:</p>
<pre><code>&lt;?xml version="1.0"?&gt;
&lt;project name="bbindings.jaxb" default="jar" basedir="."&gt;

&lt;property name="home" value="."/&gt;
&lt;property name="build" value="build"/&gt;
&lt;property name="logs" value="logs"/&gt;
&lt;property name="src" value="src"/&gt;
&lt;property file="build.properties"/&gt; 

&lt;target name="clean"&gt;
	&lt;delete dir="${logs}"/&gt;
	&lt;delete dir="${build}"/&gt;
	&lt;delete dir="${src}"/&gt;
&lt;/target&gt;

&lt;target name="init" depends="clean"&gt;
	&lt;mkdir dir="${build}"/&gt;
	&lt;mkdir dir="${logs}"/&gt;
	&lt;mkdir dir="${src}"/&gt;
&lt;/target&gt;

&lt;target name="copy_xsd" depends="init"&gt;
	&lt;copy file="${env.XSD_FILE}" todir="${src}"/&gt;
&lt;/target&gt;       

&lt;target name="generate_jaxb" depends="copy_xsd"&gt;
	&lt;java jar="${env.JWSDP_HOME_LIB}\jaxb-xjc.jar" fork="true" dir="${src}"&gt;
		&lt;arg value="-p"/&gt;
		&lt;arg value="com.bbindings.jaxb"/&gt;
		&lt;arg value="${home}/${env.XSD_FILE}"/&gt;
	&lt;/java&gt;
&lt;/target&gt;  

&lt;target name="compile" depends="init,generate_jaxb"&gt;
	&lt;!-- Compile the java code --&gt;
	&lt;javac srcdir="${src}" destdir="${build}"&gt;
		&lt;classpath&gt;
			&lt;pathelement location="${env.JWSDP_HOME_LIB}/jaxb-api.jar"/&gt;
			&lt;pathelement location="${env.JWSDP_HOME_LIB}/jaxb-impl.jar"/&gt;
			&lt;pathelement location="${env.JWSDP_HOME_LIB}/jaxb-xjc.jar"/&gt;
			&lt;pathelement location="${env.JWSDP_HOME_LIB}/jsr173_api.jar"/&gt;
		&lt;/classpath&gt;
	&lt;/javac&gt;
&lt;/target&gt;
  
&lt;target name="jar" depends="compile"&gt;
	&lt;!-- Build the jar file --&gt;
	&lt;jar basedir="${build}" destfile="${build}/${env.JAR_FILE}"/&gt;
&lt;/target&gt;

&lt;/project&gt;</code></pre>
<div style="text-align:center;"><a href="http://api.tweetmeme.com/share?url=http://www.factorypattern.com/how-to-write-ant-xml-build-file-generate-jaxb-code-compile-build-jar/"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://www.factorypattern.com/how-to-write-ant-xml-build-file-generate-jaxb-code-compile-build-jar/" height="61" width="51" /></a></div><h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li><a href="http://www.factorypattern.com/how-to-create-jar-files-from-ant/" title="How to Create Jar Files from Ant">How to Create Jar Files from Ant</a></li><li><a href="http://www.factorypattern.com/how-to-use-ant/" title="How To Use Ant">How To Use Ant</a></li><li><a href="http://www.factorypattern.com/howto-generate-hibernate-pojo-and-mapping-files-using-ant-from-a-db-schema/" title="Howto generate Hibernate POJO and mapping files using ant from a db schema">Howto generate Hibernate POJO and mapping files using ant from a db schema</a></li><li><a href="http://www.factorypattern.com/jaxb-castor-comparison/" title="JAXB Castor Comparison">JAXB Castor Comparison</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.factorypattern.com/how-to-write-ant-xml-build-file-generate-jaxb-code-compile-build-jar/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Simulate Multiple Inheritance in Java</title>
		<link>http://www.factorypattern.com/how-to-simulate-multiple-inheritance-in-java/</link>
		<comments>http://www.factorypattern.com/how-to-simulate-multiple-inheritance-in-java/#comments</comments>
		<pubDate>Sat, 15 Mar 2008 09:45:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Object Oriented Design]]></category>
		<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Inheritance]]></category>
		<category><![CDATA[VB.NET]]></category>

		<guid isPermaLink="false">http://www.factorypattern.com/how-to-simulate-multiple-inheritance-in-java/</guid>
		<description><![CDATA[Java or .NET languages does not allow multiple inheritance and usually we don&#8217;t need multiple inheritance in our projects. However there are cases when inheriting from multiple classes is a necessity. There are a few tricks we can apply in order to be able to get what multiple inheritance gives us in the languages where [...]]]></description>
			<content:encoded><![CDATA[<p>Java or .NET languages does not allow multiple inheritance and usually we don&#8217;t need multiple inheritance in our projects. However there are cases when inheriting from multiple classes is a necessity. There are a few tricks we can apply in order to be able to get what multiple inheritance gives us in the languages where it is supported.<span id="more-12"></span></p>
<p>The great idea to achieve it is to use delegation instead of inheritance. Let&#8217;s consider we already have to classes we want to extend. We can use the inheritance so our new class will extend the first class. For the second class we can not use inheritance again, because we already inherit the first one, on the other side we want to overwrite some method implementations. The trick is to create an inner class extending the second class, and to delegate the calls from the main outer class to the inner class.</p>
<p>The technique can be &#8220;scaled&#8221; for  any number of classes. For each new class which we need to inherit, we need to create another inner class and delegate the request to it. The big drawback here is the amount of code that should be written for creating the inherited inner classes and the code for delegating all the requests which which in case of multiple inheritance are done by default.</p>
<div style="text-align:center;"><a href="http://api.tweetmeme.com/share?url=http://www.factorypattern.com/how-to-simulate-multiple-inheritance-in-java/"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://www.factorypattern.com/how-to-simulate-multiple-inheritance-in-java/" height="61" width="51" /></a></div><h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li><a href="http://www.factorypattern.com/drawbacks-of-marker-interface-design-pattern/" title="Drawbacks of Marker Interface Design Pattern.">Drawbacks of Marker Interface Design Pattern.</a></li><li><a href="http://www.factorypattern.com/how-to-readwrite-java-properties-files/" title="How to Read/Write Java Properties Files">How to Read/Write Java Properties Files</a></li><li><a href="http://www.factorypattern.com/multimap-in-google-collections-library/" title="Multimap in Google Collections Library">Multimap in Google Collections Library</a></li><li><a href="http://www.factorypattern.com/how-to-use-ant/" title="How To Use Ant">How To Use Ant</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.factorypattern.com/how-to-simulate-multiple-inheritance-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Create Jar Files from Ant</title>
		<link>http://www.factorypattern.com/how-to-create-jar-files-from-ant/</link>
		<comments>http://www.factorypattern.com/how-to-create-jar-files-from-ant/#comments</comments>
		<pubDate>Fri, 29 Feb 2008 07:06:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Ant]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[compile]]></category>
		<category><![CDATA[jar]]></category>
		<category><![CDATA[packaging]]></category>

		<guid isPermaLink="false">http://www.factorypattern.com/how-to-create-jar-files-from-ant/</guid>
		<description><![CDATA[Lets take this time in consideration a real life project. Most of the time Ant is used to compile and build java projects. This means generating, compiling and building jar files. For this we are going to define severals tasks. Lets consider the we create a buid.xml file in the directory where sources are located. [...]]]></description>
			<content:encoded><![CDATA[<p>Lets take this time in consideration a real life project. Most of the time Ant is used to compile and build java projects. This means generating, compiling and building jar files. For this we are going to define severals tasks. Lets consider the we create a buid.xml file in the directory where sources are located. The sources are put in the subdirectory called <em>src</em>. We also need to include a jar in the classpath while compiling. We are going to create a separate property file to define the location of this file:<span id="more-10"></span></p>
<pre><code>#===================================
#	File Name: build.properties
#===================================

env.LIB=c://java//thirdparty//lib</code></pre>
<p>The ant task contains a few tasks for doing the following operations:<br />
1. Clean the build directory if exists.<br />
2. Create the build directory where the new classes will be built.<br />
3. Compile the java classes.<br />
4. Build a jar file from the java classes.</p>
<p>Here is the build.xml file. Note the dependencies between the tasks and how the properties are defined in the ant xml file or loaded from the properties file:</p>
<pre><code>&lt;project name="how-to-use-ant" basedir="." default="jar"&gt;

	&lt;property name="build" value="build"/&gt;
	&lt;property file="build.properties"/&gt;
	
	&lt;target name="clean"&gt;
		&lt;delete dir="${build}"/&gt;
	&lt;/target&gt;

	&lt;target name="init" depends="clean"&gt;
		&lt;mkdir dir="${build}"/&gt;
	&lt;/target&gt;	
	
	&lt;target name="compile" depends="init"&gt;
		&lt;!-- Compile the java code --&gt;
		&lt;javac srcdir="src" destdir="${build}"&gt;
			&lt;classpath&gt;
				&lt;pathelement location="${env.LIB}/required-jar1.jar"/&gt;
				&lt;pathelement location="${env.LIB}/required-jar2.jar"/&gt;
			&lt;/classpath&gt;
		&lt;/javac&gt;
	&lt;/target&gt;	
	
	&lt;target name="jar" depends="compile"&gt;
		&lt;!-- Build the jar file --&gt;
		&lt;jar basedir="${build}" destfile="${build}/new-jar.jar"/&gt;
	&lt;/target&gt;
	
&lt;/project&gt;</code></pre>
<p>Now that you&#8217;ve seen how to create an ant task you might ask why you need an ant build file for building a jar file. This can be done as well with a few batch commands which will take only a 4 lines bat file. There are certain advantages. The ant files can be easily parametrized and reused, sub projects cand be created, lots of tasks are implemented, covering most of the operations you want to do (junits, sql operations, zip file tasks, tasks for replacing and editing text and xml files, &#8230;) and new tasks can be implemented in java and used in any ant file.</p>
<div style="text-align:center;"><a href="http://api.tweetmeme.com/share?url=http://www.factorypattern.com/how-to-create-jar-files-from-ant/"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://www.factorypattern.com/how-to-create-jar-files-from-ant/" height="61" width="51" /></a></div><h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li><a href="http://www.factorypattern.com/how-to-write-ant-xml-build-file-generate-jaxb-code-compile-build-jar/" title="How to write an Ant xml build file to Generate JAXB Code and Compile and Build it to a Jar">How to write an Ant xml build file to Generate JAXB Code and Compile and Build it to a Jar</a></li><li><a href="http://www.factorypattern.com/how-to-use-ant/" title="How To Use Ant">How To Use Ant</a></li><li><a href="http://www.factorypattern.com/howto-generate-hibernate-pojo-and-mapping-files-using-ant-from-a-db-schema/" title="Howto generate Hibernate POJO and mapping files using ant from a db schema">Howto generate Hibernate POJO and mapping files using ant from a db schema</a></li><li><a href="http://www.factorypattern.com/how-to-readwrite-java-properties-files/" title="How to Read/Write Java Properties Files">How to Read/Write Java Properties Files</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.factorypattern.com/how-to-create-jar-files-from-ant/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 1.083 seconds -->
