<?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>Stefan Sobek Blog &#187; Java</title>
	<atom:link href="http://www.sobek.info/blog/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sobek.info/blog</link>
	<description>Writing about IT, Software Engineering, sports and other stuff</description>
	<lastBuildDate>Mon, 24 Jan 2011 12:50:08 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Override equals, toString and hashCode in Java classes</title>
		<link>http://www.sobek.info/blog/2010/12/11/override-equals-tostring-and-hashcode-in-java-classes/</link>
		<comments>http://www.sobek.info/blog/2010/12/11/override-equals-tostring-and-hashcode-in-java-classes/#comments</comments>
		<pubDate>Sat, 11 Dec 2010 20:02:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[effective java]]></category>
		<category><![CDATA[equals method]]></category>
		<category><![CDATA[hashCode]]></category>
		<category><![CDATA[toString]]></category>

		<guid isPermaLink="false">http://www.sobek.info/blog/?p=307</guid>
		<description><![CDATA[Override equals
Although some people think it might be easy to override the equals method of a class, you can do it wrong in many cases and this can cause problems.
The easiest case is not to override the equals method, then each class is equals only to itself and equals from object is used. Do this [...]]]></description>
			<content:encoded><![CDATA[<h5>Override equals</h5>
<p>Although some people think it might be easy to override the equals method of a class, you can do it wrong in many cases and this can cause problems.</p>
<p>The easiest case is not to override the equals method, then each class is equals only to itself and equals from object is used. Do this only in following cases:</p>
<ul>
<li>When each instance is inherently unique. For example classes as Thread. These represent active entites rather than values. So for value objects this is no applicable.</li>
<li>You don&#8217;t need a &#8220;logical equality&#8221; test. For example the java.util.Random where it does not make sense to check if two random numbers are equal.</li>
<li>The superclass equals is fully appropriate. E.g. Set -&gt; AbstractSet, List -&gt; AbstractList</li>
<li>The class or the package is private and it is assured that it will not been invoced</li>
</ul>
<p><strong>When to override equals method?</strong></p>
<p>When the logical equality differs from object identity. This is the case for value classes. This class represents a value such as Integer or Date. If these objects will be compared you want to know if the values are the same and not if the objects are the same.</p>
<p>The Java6 specification says about the contract which must be assured when overriding equals:</p>
<p>• Reflexive:Foranynon-nullreferencevaluex,x.equals(x)mustreturntrue.<br />
• Symmetric:Foranynon-nullreferencevaluesxandy,x.equals(y)mustre- turn true if and only if y.equals(x) returns true.<br />
• Transitive:Foranynon-nullreferencevaluesx,y,z,ifx.equals(y)returns true and y.equals(z) returns true, then x.equals(z) must return true.<br />
• Consistent: For any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, pro- vided no information used in equals comparisons on the objects is modified.<br />
• For any non-null reference value x, x.equals(null) must return false.</p>
<p><strong>How should equals be overridden?</strong></p>
<p>This list was taken from Effective Java 2nd Edition.</p>
<ul>
<li>Use the == operator to check if the argument is a reference to this object.</li>
<li>Use the instanceof operator to check if the argument has the correct type.</li>
<li>Cast the argument to the correct type.</li>
<li>For each “significant” field in the class, check if that field of the argument matches the corresponding field of this object.</li>
<li>When you are finished writing your equals method, ask yourself three questions: Is it symmetric? Is it transitive? Is it consistent?</li>
</ul>
<h5>Override hashCode</h5>
<p>Always override hashCode when you override equals.</p>
<p>Here is the contract from the Java specification:</p>
<p>Here is the contract, copied from the Object specification [JavaSE6]:<br />
• Whenever it is invoked on the same object more than once during an execu- tion of an application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execu- tion of an application to another execution of the same application.<br />
• If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.<br />
• It is not required that if two objects are unequal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.</p>
<p>Furthermore two equal objects must have the same hash code.</p>
<p><strong>How to create a good has method?</strong></p>
<p>Here are some tips taken from Effective Java book:</p>
<ul>
<li>Store some constant nonzero value, say 17, in an int variable called result.</li>
<li>For each significant field f in your object (each field taken into account by the equals method, that is), do the following:
<ul>
<li>Compute an int has code c for the field:
<ul>
<li>If the field is a boolean, compute (f ? 1 : 0)</li>
<li>if the field is a byte, char, short, or int, computer (int) f.</li>
<li>If the field is a long, computer (int) (f^&gt;&gt;&gt;32)).</li>
<li>If the field is a float, computer Float.floatToIntBits(f).</li>
<li>If the field is a double, compute Double.doubleToLongBits(f), and then hash the resulting long as in step for long.</li>
<li>If the field is an object reference and this class&#8217;s equals method compares the field by recursively invoking equals, recursively invoke hashCode on the field. If a more complex comparison is required, compute a &#8220;canonical representation&#8221; for this field and invoke hasCode on the canonical representation. If the value of the field is null, return 0 or some other constant.</li>
<li>If the field is an array, treat it as if each element were a separate field. That is, compute a hash code for each significant element by applying these rules recursively, and combine these values per step 2 for byte,short etc. If every element in an array field is significant, you can use one of the Arrays.hasCode methods added in release 1.5.</li>
</ul>
</li>
<li>Combine the has code c computed in step 2 into result as follows:<br />
result = 31 * result + c;</li>
</ul>
</li>
<li>Return result.</li>
<li>When you are finished writing the hashCode method, ask yourself whether equal instances have equal hash codes. Write unit tests to verify your intuition! If equal instances have unequal hash codes, figure out why and fix the problem.</li>
</ul>
<p><strong>Override toString</strong></p>
<p>If you do not override toString method and print an object you usally see something like this PhoneNumber@434b65. Ok, it is clear that this is a phone number, but wouldn&#8217;t it be better to have a representation like &#8220;(0221) 77744499&#8243;?</p>
<p>So when overriding the toString method it should return all of the interesting information contained in the object. If you have large objects or objects with a not conductive state you can return something like &#8220;Yellow pages (2355333 listings)&#8221;.</p>
<p>Provide your toString method with good documentation and describe your format. Here is an example taken from Effective Java 2nd Edition:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p307code3'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p3073"><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" id="p307code3"><pre class="java" style="font-family:monospace;"><span style="color: #008000; font-style: italic; font-weight: bold;">/** 
* Returns the string representation of this phone number. 
* The string consists of fourteen characters whose format 
* is &quot;(XXX) YYY-ZZZZ&quot;, where XXX is the area code, YYY is 
* the prefix, and ZZZZ is the line number. (Each of the 
* capital letters represents a single decimal digit.) 
* 
* If any of the three parts of this phone number is too small 
* to fill up its field, the field is padded with leading zeros. 
* For example, if the value of the line number is 123, the last 
* four characters of the string representation will be &quot;0123&quot;. 
* 
* Note that there is a single space separating the closing 
* parenthesis after the area code from the first digit of the 
* prefix. 
*/</span>
@Override <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> toString<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> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;(%03d) %03d-%04d&quot;</span>, areaCode, prefix, lineNumber<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>If you want not a specific format make something like this:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p307code4'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p3074"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p307code4"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/**
* Returns a brief description of this potion. The exact details 
* of the representation are unspecified and subject to change, 
* but the following may be regarded as typical:
* &quot;[Potion #9: type=love, smell=turpentine, look=india ink]&quot; 
@Override public String toString() { ... }</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.sobek.info/blog/2010/12/11/override-equals-tostring-and-hashcode-in-java-classes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use builder instead of constructor with many attributes</title>
		<link>http://www.sobek.info/blog/2010/12/02/use-builder-instead-of-constructor-with-many-attributes/</link>
		<comments>http://www.sobek.info/blog/2010/12/02/use-builder-instead-of-constructor-with-many-attributes/#comments</comments>
		<pubDate>Thu, 02 Dec 2010 09:28:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Patterns]]></category>
		<category><![CDATA[builder pattern]]></category>
		<category><![CDATA[effective java]]></category>
		<category><![CDATA[English]]></category>
		<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://www.sobek.info/blog/?p=291</guid>
		<description><![CDATA[Imagine you would like to create a payment in banking domain and you have to create an instance with many parameters. The first two parameters are mandatory and the others are optional. You would normally create it like:

?View Code JAVA1
Payment credit = new Payment&#40;Payment.CREDIT, &#34;1000&#34;, &#34;Beneficiary account&#34;, &#34;Beneficiary bank name&#34;, &#34;Remitter account name&#34;, &#34;Remitter bank [...]]]></description>
			<content:encoded><![CDATA[<p>Imagine you would like to create a payment in banking domain and you have to create an instance with many parameters. The first two parameters are mandatory and the others are optional. You would normally create it like:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p291code9'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p2919"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p291code9"><pre class="java" style="font-family:monospace;">Payment credit <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Payment<span style="color: #009900;">&#40;</span>Payment.<span style="color: #006633;">CREDIT</span>, <span style="color: #0000ff;">&quot;1000&quot;</span>, <span style="color: #0000ff;">&quot;Beneficiary account&quot;</span>, <span style="color: #0000ff;">&quot;Beneficiary bank name&quot;</span>, <span style="color: #0000ff;">&quot;Remitter account name&quot;</span>, <span style="color: #0000ff;">&quot;Remitter bank name&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This could be a long line, depending on the number of parameters. Also from the code you don&#8217;t know which parameter you actually set in the constructor. Ok, lets use Java Beans style with getter and setters:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p291code10'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p29110"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p291code10"><pre class="java" style="font-family:monospace;">Payment credit <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Payment<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
credit.<span style="color: #006633;">setType</span><span style="color: #009900;">&#40;</span>Payment.<span style="color: #006633;">CREDIT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
credit.<span style="color: #006633;">setAmount</span><span style="color: #009900;">&#40;</span>1000<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
credit.<span style="color: #006633;">setBeneficiaryAccountName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Beneficiary account name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
credit.<span style="color: #006633;">setBeneficiaryAccountBankName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Beneficiary bank name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
credit.<span style="color: #006633;">setRemitterAccountName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Remitter account name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
credit.<span style="color: #006633;">setRemitterAccountBankName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Remitter bank name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>So far so good, now we know exactly what we set and we use Java Beans spec. But now we have the problem that the creation process is split between multiple calls of the setters. This could lead to an inconsistent state during the creation process. If somewhere between these setter calls a problem occurs, the objects could be initialized incompletely.</p>
<p>What if you could create an instance like the following:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p291code11'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p29111"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p291code11"><pre class="java" style="font-family:monospace;">Payment credit <span style="color: #339933;">=</span> Payment.<span style="color: #006633;">Builder</span><span style="color: #009900;">&#40;</span>Payment.<span style="color: #006633;">CREDIT</span>, <span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">beneficiaryName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Beneficiary name&quot;</span><span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">beneficiaryAccount</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Beneficiary account name&quot;</span><span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">remitterName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Remitter name&quot;</span><span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">remitterAccount</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Remitter account name&quot;</span><span style="color: #009900;">&#41;</span>
                        .<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>We seperated the optional from the mandatory parameters and improved readability. Also this is only one call and so is the instance in a consistent state.</p>
<p>Here is the example code of the class Payment:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p291code12'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p29112"><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
41
42
</pre></td><td class="code" id="p291code12"><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> Payment <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> CREDIT <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> DEBIT <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> type<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// required</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">double</span> amount<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// required</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> beneficiaryAccount<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// required</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> beneficiaryName<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// required</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> remitterName<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// optional</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> remitterAccount<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// optional</span>
&nbsp;
&nbsp;
   <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;">class</span> Builder <span style="color: #009900;">&#123;</span>
     <span style="color: #666666; font-style: italic;">// Required parameters</span>
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> type<span style="color: #339933;">;</span>
     <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">double</span> amount<span style="color: #339933;">;</span>
     <span style="color: #666666; font-style: italic;">// Optional parameters - initialized to default values</span>
     <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> beneficiaryAccount <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
     <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> beneficiaryName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
     <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> remitterName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
     <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> remitterAccount <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
     <span style="color: #000000; font-weight: bold;">public</span> Builder<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> type, <span style="color: #000066; font-weight: bold;">double</span> amount<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;">type</span> <span style="color: #339933;">=</span> type<span style="color: #339933;">;</span>
       <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">amount</span> <span style="color: #339933;">=</span> amount<span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
&nbsp;
     <span style="color: #000000; font-weight: bold;">public</span> Builder beneficiaryAccount <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> val<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> remitterAccount <span style="color: #339933;">=</span> val<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>
     <span style="color: #000000; font-weight: bold;">public</span> Builder beneficiaryName <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> val<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> remitterAccount <span style="color: #339933;">=</span> val<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>
     <span style="color: #000000; font-weight: bold;">public</span> Builder remitterName <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> val<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> remitterAccount <span style="color: #339933;">=</span> val<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>
     <span style="color: #000000; font-weight: bold;">public</span> Builder remitterAccount <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> val<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> remitterAccount <span style="color: #339933;">=</span> val<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>
     <span style="color: #000000; font-weight: bold;">public</span> Payment build<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> Payment<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">private</span> Payment<span style="color: #009900;">&#40;</span>Builder builder<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     type <span style="color: #339933;">=</span> builder.<span style="color: #006633;">type</span><span style="color: #339933;">;</span>
     amount <span style="color: #339933;">=</span> builder.<span style="color: #006633;">amount</span><span style="color: #339933;">;</span>
     beneficiaryAccount <span style="color: #339933;">=</span> builder.<span style="color: #006633;">beneficiaryAccount</span><span style="color: #339933;">;</span>
     beneficiaryName <span style="color: #339933;">=</span> builder.<span style="color: #006633;">beneficiaryName</span><span style="color: #339933;">;</span>
     remitterName <span style="color: #339933;">=</span> builder.<span style="color: #006633;">remitterName</span><span style="color: #339933;">;</span>
     remitterAccount <span style="color: #339933;">=</span> builder.<span style="color: #006633;">remitterAccount</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Examples modified from the Book &#8220;Effective Java&#8221; http://www.amazon.de/gp/product/0321356683</p>
<p>Another great article can you find here: http://www.javablog.ch/2008/08/23/effective-java-builder-pattern/</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sobek.info/blog/2010/12/02/use-builder-instead-of-constructor-with-many-attributes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Good programmer or architect</title>
		<link>http://www.sobek.info/blog/2010/10/26/good-programmer-or-architect/</link>
		<comments>http://www.sobek.info/blog/2010/10/26/good-programmer-or-architect/#comments</comments>
		<pubDate>Tue, 26 Oct 2010 10:43:22 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[J2EE/JEE]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[English]]></category>
		<category><![CDATA[Good programmer]]></category>
		<category><![CDATA[J2EE]]></category>
		<category><![CDATA[JEE]]></category>

		<guid isPermaLink="false">http://www.sobek.info/blog/?p=289</guid>
		<description><![CDATA[I recently read in a chapter of Rod Johnsons Book &#8220;expert one-to-one, J2EE Design and Development&#8221; an interesting sentence:
A professional programmer or architect cares more about learning and discovering the best solution than the buzz of finding their own solution to a particular problem.
This is so true. Don&#8217;t waste (project) time with finding a very [...]]]></description>
			<content:encoded><![CDATA[<p>I recently read in a chapter of Rod Johnsons Book &#8220;expert one-to-one, J2EE Design and Development&#8221; an interesting sentence:</p>
<blockquote><p>A professional programmer or architect cares more about learning and discovering the best solution than the buzz of finding their own solution to a particular problem.</p></blockquote>
<p>This is so true. Don&#8217;t waste (project) time with finding a very own solution and reinventing the wheel. Find solutions that fit your needs, learn how they solved the problem. Of course they must fit to your problem with a coerage of lets say >90%. But these solutions are tested. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.sobek.info/blog/2010/10/26/good-programmer-or-architect/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using attribute values of composite component in commandLink</title>
		<link>http://www.sobek.info/blog/2010/10/14/using-attribute-values-of-composite-component-in-commandlink/</link>
		<comments>http://www.sobek.info/blog/2010/10/14/using-attribute-values-of-composite-component-in-commandlink/#comments</comments>
		<pubDate>Thu, 14 Oct 2010 11:39:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[commandLink]]></category>
		<category><![CDATA[composite component]]></category>
		<category><![CDATA[English]]></category>
		<category><![CDATA[jsf]]></category>
		<category><![CDATA[jsf 2.0]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://www.sobek.info/blog/?p=284</guid>
		<description><![CDATA[Situation:
You created a composite component in JSF 2.0. This component now should have some attributes which contains direct links. This links should now be declared when you use the component.
Example:

?View Code HTML1
2
3
    &#60;composite:attribute type=&#34;java.lang.String&#34; required=&#34;false&#34;
        name=&#34;logoutURL&#34; default=&#34;/faces/logout.jsf&#34;
        shortDescription=&#34;The [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Situation:</strong><br />
You created a composite component in JSF 2.0. This component now should have some attributes which contains direct links. This links should now be declared when you use the component.<br />
Example:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p284code16'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p28416"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p284code16"><pre class="html" style="font-family:monospace;">    &lt;composite:attribute type=&quot;java.lang.String&quot; required=&quot;false&quot;
        name=&quot;logoutURL&quot; default=&quot;/faces/logout.jsf&quot;
        shortDescription=&quot;The logout URL.&quot; /&gt;</pre></td></tr></table></div>

<p>Later in the implementation you use it like the following example:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p284code17'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p28417"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p284code17"><pre class="html" style="font-family:monospace;">		&lt;h:commandLink action=&quot;#{cc.attrs.helpURL}&quot;
			value=&quot;#{textbundle['status.linktext.logout']}&quot; /&gt;</pre></td></tr></table></div>

<p><strong>Problem</strong><br />
You now get a class cast exception. JSF excepts a String result of a method which you can use in the action attribute of commandLink. Directly putting a String in this result is ok. </p>
<p><strong>Solution</strong><br />
Use toString (although you declared the attribute as type of String)</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p284code18'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p28418"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p284code18"><pre class="html" style="font-family:monospace;">		&lt;h:commandLink action=&quot;#{cc.attrs.helpURL.toString}&quot;
			value=&quot;#{textbundle['status.linktext.logout']}&quot; /&gt;</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.sobek.info/blog/2010/10/14/using-attribute-values-of-composite-component-in-commandlink/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Short Maven introduction</title>
		<link>http://www.sobek.info/blog/2010/03/02/short-maven-introduction/</link>
		<comments>http://www.sobek.info/blog/2010/03/02/short-maven-introduction/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 13:28:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[MacOS]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[build]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[maven2]]></category>
		<category><![CDATA[mvn]]></category>

		<guid isPermaLink="false">http://www.sobek.info/blog/?p=231</guid>
		<description><![CDATA[Due to the fact I got in touch with maven2 and I think that maven2 is very helpful will give a short introduction. Bigger projects normally have a more complex configuration but I will start from the scratch.
Maven will create a kind of project sceleton with the following command

?View Code JAVA1
mvn archetype:create -DgroupId=info.sobek.testapp -DartifactId=testapp

If you [...]]]></description>
			<content:encoded><![CDATA[<p>Due to the fact I got in touch with maven2 and I think that maven2 is very helpful will give a short introduction. Bigger projects normally have a more complex configuration but I will start from the scratch.</p>
<p><strong>Maven will create a kind of project sceleton with the following command</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p231code21'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p23121"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p231code21"><pre class="java" style="font-family:monospace;">mvn archetype<span style="color: #339933;">:</span>create <span style="color: #339933;">-</span>DgroupId<span style="color: #339933;">=</span>info.<span style="color: #006633;">sobek</span>.<span style="color: #006633;">testapp</span> <span style="color: #339933;">-</span>DartifactId<span style="color: #339933;">=</span>testapp</pre></td></tr></table></div>

<p>If you work with e.g. eclipse, you can create the project files for eclipse:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p231code22'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p23122"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p231code22"><pre class="java" style="font-family:monospace;">mvn eclipse<span style="color: #339933;">:</span>eclipse</pre></td></tr></table></div>

<p>Now you can import it into eclipse with &#8220;import into existing workspace&#8221; option of eclipse. </p>
<p>Maven will normally copy the created so called &#8220;artifacts&#8221; into your local maven repository. Normally located under your home dir and then .m2</p>
<p>When you program depends on several jars and these dependencies are defined in the pom.xml, then maven will copy the jars in their specific version into the local maven repository. This is very helpful because you can simply build your project with another newer or older version of your dependent jar-file. </p>
<p>How to create multi-module projects will be explained in another episode. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.sobek.info/blog/2010/03/02/short-maven-introduction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Create spellout numbers from integers with java</title>
		<link>http://www.sobek.info/blog/2009/12/18/create-spellout-numbers-from-integers-with-java/</link>
		<comments>http://www.sobek.info/blog/2009/12/18/create-spellout-numbers-from-integers-with-java/#comments</comments>
		<pubDate>Fri, 18 Dec 2009 12:49:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[ibm]]></category>
		<category><![CDATA[ibm icu]]></category>
		<category><![CDATA[icu]]></category>
		<category><![CDATA[integer]]></category>
		<category><![CDATA[spellout]]></category>

		<guid isPermaLink="false">http://www.sobek.info/blog/?p=198</guid>
		<description><![CDATA[You would like to know the spellout word from an integer? Use the ICU library from IBM.

?View Code JAVA1
2
3
4
import com.ibm.icu.text.RuleBasedNumberFormat;
// ...
int num = 100198
System.out.println&#40;new RuleBasedNumberFormat&#40;Locale.US, RuleBasedNumberFormat.SPELLOUT&#41;.format&#40;number&#41;&#41;;

The result is: one hundred thousand, one hundred and ninety-eight
You can get the spellout of the number in other languages. Check Locale.GERMANY instead of Locale.US and you have: hunderttausendhundertachtundneunzig
]]></description>
			<content:encoded><![CDATA[<p>You would like to know the spellout word from an integer? Use the ICU library from IBM.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p198code24'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p19824"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p198code24"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.ibm.icu.text.RuleBasedNumberFormat</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #000066; font-weight: bold;">int</span> num <span style="color: #339933;">=</span> 100198
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> RuleBasedNumberFormat<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alocale+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Locale</span></a>.<span style="color: #006633;">US</span>, RuleBasedNumberFormat.<span style="color: #006633;">SPELLOUT</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span>number<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The result is: <strong>one hundred thousand, one hundred and ninety-eight</strong><br />
You can get the spellout of the number in other languages. Check Locale.GERMANY instead of Locale.US and you have: <strong>hunderttausendhundertachtundneunzig</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sobek.info/blog/2009/12/18/create-spellout-numbers-from-integers-with-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fill swing and awt windows by robot</title>
		<link>http://www.sobek.info/blog/2009/12/17/fill-swing-and-awt-windows-by-robot/</link>
		<comments>http://www.sobek.info/blog/2009/12/17/fill-swing-and-awt-windows-by-robot/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 14:28:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[awt]]></category>
		<category><![CDATA[gui]]></category>
		<category><![CDATA[robot]]></category>
		<category><![CDATA[swing]]></category>

		<guid isPermaLink="false">http://www.sobek.info/blog/?p=188</guid>
		<description><![CDATA[I just stumbled upon an article about the java.awt.robot from java while reading java stuff. When you would like to fill textboxes or to click buttons etc. on an swing or awt window automatically you can use the java.awt.robot class.
Check this example:

?View Code JAVA1
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
&#160;
package desktopapplication1;
&#160;
import java.awt.AWTException;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.awt.Point;
import java.awt.event.InputEvent;
import org.jdesktop.application.FrameView;
&#160;
public class Robot implements [...]]]></description>
			<content:encoded><![CDATA[<p>I just stumbled upon an article about the <strong>java.awt.robot</strong> from java while reading java stuff. When you would like to fill textboxes or to click buttons etc. on an swing or awt window automatically you can use the java.awt.robot class.</p>
<p>Check this example:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p188code27'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p18827"><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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
</pre></td><td class="code" id="p188code27"><pre class="java" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">desktopapplication1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.AWTException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.event.KeyEvent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.logging.Level</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.logging.Logger</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.Point</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.event.InputEvent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.jdesktop.application.FrameView</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> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Arobot+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Robot</span></a> <span style="color: #000000; font-weight: bold;">implements</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Arunnable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Runnable</span></a> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> FrameView frameview<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Arobot+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Robot</span></a><span style="color: #009900;">&#40;</span>FrameView frameview<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;">frameview</span> <span style="color: #339933;">=</span> frameview<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> run<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;">try</span> <span style="color: #009900;">&#123;</span>
            initRobot<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: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aawtexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">AWTException</span></a> ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            Logger.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Arobot+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Robot</span></a>.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">log</span><span style="color: #009900;">&#40;</span>Level.<span style="color: #006633;">SEVERE</span>, <span style="color: #000066; font-weight: bold;">null</span>, ex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</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> initRobot<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aawtexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">AWTException</span></a> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// create a robot to feed in GUI events</span>
&nbsp;
        java.<span style="color: #006633;">awt</span>.<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Arobot+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Robot</span></a> rob <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> java.<span style="color: #006633;">awt</span>.<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Arobot+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Robot</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// enter some keystrokes</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">int</span> keyinput<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
            <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Akeyevent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">KeyEvent</span></a>.<span style="color: #006633;">VK_T</span>,
            <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Akeyevent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">KeyEvent</span></a>.<span style="color: #006633;">VK_E</span>,
            <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Akeyevent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">KeyEvent</span></a>.<span style="color: #006633;">VK_S</span>,
            <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Akeyevent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">KeyEvent</span></a>.<span style="color: #006633;">VK_T</span>,
            <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Akeyevent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">KeyEvent</span></a>.<span style="color: #006633;">VK_I</span>,
            <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Akeyevent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">KeyEvent</span></a>.<span style="color: #006633;">VK_N</span>,
            <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Akeyevent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">KeyEvent</span></a>.<span style="color: #006633;">VK_G</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        rob.<span style="color: #006633;">delay</span><span style="color: #009900;">&#40;</span>1000<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        rob.<span style="color: #006633;">keyPress</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Akeyevent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">KeyEvent</span></a>.<span style="color: #006633;">VK_SHIFT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>DesktopApplication1View<span style="color: #009900;">&#41;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">frameview</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getjTextField1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">requestFocus</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;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> keyinput.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            rob.<span style="color: #006633;">keyPress</span><span style="color: #009900;">&#40;</span>keyinput<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            rob.<span style="color: #006633;">delay</span><span style="color: #009900;">&#40;</span>1000<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        rob.<span style="color: #006633;">keyRelease</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Akeyevent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">KeyEvent</span></a>.<span style="color: #006633;">VK_SHIFT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        rob.<span style="color: #006633;">keyPress</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Akeyevent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">KeyEvent</span></a>.<span style="color: #006633;">VK_ENTER</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// move cursor to exit button</span>
&nbsp;
        <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Apoint+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Point</span></a> p <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>DesktopApplication1View<span style="color: #009900;">&#41;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">frameview</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getjButton1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getLocationOnScreen</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        rob.<span style="color: #006633;">mouseMove</span><span style="color: #009900;">&#40;</span>p.<span style="color: #006633;">x</span> <span style="color: #339933;">+</span> 5, p.<span style="color: #006633;">y</span> <span style="color: #339933;">+</span> 5<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        rob.<span style="color: #006633;">delay</span><span style="color: #009900;">&#40;</span>2000<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// press and release left mouse button</span>
&nbsp;
        rob.<span style="color: #006633;">mousePress</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainputevent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">InputEvent</span></a>.<span style="color: #006633;">BUTTON1_MASK</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        rob.<span style="color: #006633;">delay</span><span style="color: #009900;">&#40;</span>2000<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        rob.<span style="color: #006633;">mouseRelease</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainputevent+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">InputEvent</span></a>.<span style="color: #006633;">BUTTON1_MASK</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>I started the class in a separate thread, the example from sun, where I took most parts of the code uses ActionListener to update the textfield.</p>
<p>Now you have to start the thread with:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p188code28'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p18828"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p188code28"><pre class="java" style="font-family:monospace;">       <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Athread+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Thread</span></a> t <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Athread+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Thread</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> desktopapplication1.<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Arobot+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Robot</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       t.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Check the example from sun &#8211; <a href="http://java.sun.com/developer/TechTips/2000/tt0711.html">http://java.sun.com/developer/TechTips/2000/tt0711.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sobek.info/blog/2009/12/17/fill-swing-and-awt-windows-by-robot/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple mailing in java</title>
		<link>http://www.sobek.info/blog/2009/12/14/simple-mailing-in-java/</link>
		<comments>http://www.sobek.info/blog/2009/12/14/simple-mailing-in-java/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 15:58:37 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[apache commons]]></category>
		<category><![CDATA[lib]]></category>
		<category><![CDATA[mail]]></category>
		<category><![CDATA[simplemail]]></category>

		<guid isPermaLink="false">http://www.sobek.info/blog/?p=184</guid>
		<description><![CDATA[To send simple mails im Java, e.g. without header modifications or attachment you can use apache commons library. 
Download it at http://commons.apache.org/ and add the jar to your project.

?View Code JAVA1
2
3
4
5
6
7
8
9
SimpleEmail email = new SimpleEmail&#40;&#41;;
email.setHostName&#40;&#34;host.test.com&#34;&#41;;
email.setFrom&#40;&#34;from@test.com&#34;, &#34;testfromname&#34;&#41;;
email.addTo&#40;&#34;to@test.com&#34;, &#34;testtoname&#34;&#41;;
email.addBcc&#40;&#34;bcc@test.com&#34;, &#34;testbccname&#34;&#41;;
email.setSubject&#40;&#34;testsubject&#34;&#41;;
email.setMsg&#40;&#34;testmessage&#34;&#41;;
email.setAuthentication&#40;&#34;username&#34;, &#34;pass&#34;&#41;;
email.send&#40;&#41;;

Check simple email-class documentation for further configuration as smtp-port etc.
]]></description>
			<content:encoded><![CDATA[<p>To send simple mails im Java, e.g. without header modifications or attachment you can use apache commons library. </p>
<p>Download it at <a href="http://commons.apache.org/">http://commons.apache.org/</a> and add the jar to your project.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p184code30'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p18430"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p184code30"><pre class="java" style="font-family:monospace;">SimpleEmail email <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleEmail<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
email.<span style="color: #006633;">setHostName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;host.test.com&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
email.<span style="color: #006633;">setFrom</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;from@test.com&quot;</span>, <span style="color: #0000ff;">&quot;testfromname&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
email.<span style="color: #006633;">addTo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;to@test.com&quot;</span>, <span style="color: #0000ff;">&quot;testtoname&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
email.<span style="color: #006633;">addBcc</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;bcc@test.com&quot;</span>, <span style="color: #0000ff;">&quot;testbccname&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
email.<span style="color: #006633;">setSubject</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;testsubject&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
email.<span style="color: #006633;">setMsg</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;testmessage&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
email.<span style="color: #006633;">setAuthentication</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;username&quot;</span>, <span style="color: #0000ff;">&quot;pass&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
email.<span style="color: #006633;">send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Check simple email-class documentation for further configuration as smtp-port etc.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sobek.info/blog/2009/12/14/simple-mailing-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generating stub classes from wsdl</title>
		<link>http://www.sobek.info/blog/2009/12/09/generating-stub-classes-from-wsdl/</link>
		<comments>http://www.sobek.info/blog/2009/12/09/generating-stub-classes-from-wsdl/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 16:20:39 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[ant]]></category>
		<category><![CDATA[build]]></category>
		<category><![CDATA[netbeans]]></category>
		<category><![CDATA[stubs]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wsdl]]></category>
		<category><![CDATA[wsdl2java]]></category>

		<guid isPermaLink="false">http://www.sobek.info/blog/?p=163</guid>
		<description><![CDATA[Normally you can create stub classes from a wsdl-file via console by calling wsdl2java. To generate stub classes from wsdl with netbeans you have to add to the build.xml file the following code:

?View Code XML1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
&#60;path id=&#34;axis.classpath&#34;&#62;
   &#60;fileset dir=&#34;/usr/local/axis/lib/&#34;&#62;
      &#60;include name=&#34;**/*.jar&#34; /&#62;
    &#60;/fileset&#62;
&#60;/path&#62;
&#160;
&#60;taskdef resource=&#34;axis-tasks.properties&#34; classpathref=&#34;axis.classpath&#34; /&#62;
&#160;
&#60;target [...]]]></description>
			<content:encoded><![CDATA[<p>Normally you can create stub classes from a wsdl-file via console by calling <strong>wsdl2java</strong>. To generate stub classes from wsdl with <strong>netbeans </strong>you have to add to the <strong>build.xml</strong> file the following code:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p163code32'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p16332"><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
</pre></td><td class="code" id="p163code32"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;path</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;axis.classpath&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fileset</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;/usr/local/axis/lib/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;include</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;**/*.jar&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fileset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/path<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;taskdef</span> <span style="color: #000066;">resource</span>=<span style="color: #ff0000;">&quot;axis-tasks.properties&quot;</span> <span style="color: #000066;">classpathref</span>=<span style="color: #ff0000;">&quot;axis.classpath&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Generate From WSDL&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;axis-wsdl2java</span></span>
<span style="color: #009900;">    <span style="color: #000066;">output</span>=<span style="color: #ff0000;">&quot;src/&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">testcase</span>=<span style="color: #ff0000;">&quot;false&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">verbose</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">serverside</span>=<span style="color: #ff0000;">&quot;false&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">url</span>=<span style="color: #ff0000;">&quot;http://url.to-webservice.com/?wsdl&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mapping</span> <span style="color: #000066;">namespace</span>=<span style="color: #ff0000;">&quot;http://axis.apache.org/ns/interop&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">package</span>=<span style="color: #ff0000;">&quot;wsdltester&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mapping</span> <span style="color: #000066;">namespace</span>=<span style="color: #ff0000;">&quot;http://www.namespace.de/types/&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">package</span>=<span style="color: #ff0000;">&quot;wsdltester.stubs&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mapping</span> <span style="color: #000066;">namespace</span>=<span style="color: #ff0000;">&quot;http://www.namespace.de/services/&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">package</span>=<span style="color: #ff0000;">&quot;wsdltester.service&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/axis-wsdl2java<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Note that you have to modify several settings like the namespaces and the url to the web service on line 15! Do not just copy the code. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.sobek.info/blog/2009/12/09/generating-stub-classes-from-wsdl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Replace nested conditionals with guard clauses</title>
		<link>http://www.sobek.info/blog/2009/12/01/replace-nested-conditionals-with-guard-clauses/</link>
		<comments>http://www.sobek.info/blog/2009/12/01/replace-nested-conditionals-with-guard-clauses/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 14:26:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[fowler]]></category>
		<category><![CDATA[guard clauses]]></category>
		<category><![CDATA[refactoring]]></category>

		<guid isPermaLink="false">http://www.sobek.info/blog/?p=135</guid>
		<description><![CDATA[Imagine you have java code like the following:

?View Code JAVA1
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
   if &#40;o.isShipped&#41; //order is shipped --&#62; archive
                &#123;
                    //archived [...]]]></description>
			<content:encoded><![CDATA[<p>Imagine you have java code like the following:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p135code36'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p13536"><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
</pre></td><td class="code" id="p135code36"><pre class="java" style="font-family:monospace;">   <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>o.<span style="color: #006633;">isShipped</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">//order is shipped --&gt; archive</span>
                <span style="color: #009900;">&#123;</span>
                    <span style="color: #666666; font-style: italic;">//archived 21 days after delivery</span>
                    <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Calendar</span></a> cal <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Agregoriancalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">GregorianCalendar</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    cal.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acalendar+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Calendar</span></a>.<span style="color: #006633;">DAY_OF_MONTH</span>,DAYS_TO_ARCHIVE_AFTER_SHIPPING<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>
                       ...
                        <span style="color: #009900;">&#125;</span>
                    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        ...
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>o.<span style="color: #006633;">isCancelled</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">true</span> <span style="color: #339933;">||</span> o.<span style="color: #006633;">isClosed</span> <span style="color: #339933;">==</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: #666666; font-style: italic;">//cancelled more than 7 days ago</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: #009900;">&#125;</span>
                            <span style="color: #009900;">&#125;</span>
                        <span style="color: #009900;">&#125;</span>
                    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        ...
                    <span style="color: #009900;">&#125;</span>
                    <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>o.<span style="color: #006633;">isClosedAt</span>.<span style="color: #006633;">getTimeInMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> cal.<span style="color: #006633;">getTimeInMillis</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>
&nbsp;
                           ...
&nbsp;
                                <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                                    ...
                                <span style="color: #009900;">&#125;</span>
                            <span style="color: #009900;">&#125;</span>
                        <span style="color: #009900;">&#125;</span>
                    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        ...
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>o.<span style="color: #006633;">isReadyForShipping</span> <span style="color: #339933;">&amp;&amp;</span> o.<span style="color: #006633;">isDispatched</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                 ...
                <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Isn&#8217;t it extreme hard to read and to understand? Martin Fowlers Book &#8220;Refactoring&#8221; shows how to handle that. Replace all these nested if&#8230;else with if-guards. It could lool like this:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p135code37'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p13537"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p135code37"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>order.<span style="color: #006633;">isShipped</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>isCancelled <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>isClosed<span style="color: #009900;">&#41;</span> archiveOrder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>order.<span style="color: #006633;">isShipped</span> <span style="color: #339933;">&amp;&amp;</span> order.<span style="color: #006633;">isCancelled</span> <span style="color: #339933;">&amp;&amp;</span> order.<span style="color: #006633;">isCancelledAt</span>.<span style="color: #006633;">getTimeInMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> cal.<span style="color: #006633;">getTimeInMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> moveReservationOnCancel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>order.<span style="color: #006633;">isShipped</span> <span style="color: #339933;">&amp;&amp;</span> order.<span style="color: #006633;">isClosed</span> <span style="color: #339933;">&amp;&amp;</span> order.<span style="color: #006633;">isClosedAt</span>.<span style="color: #006633;">getTimeInMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> cal.<span style="color: #006633;">getTimeInMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> moveReservationOnClosing<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
...</pre></td></tr></table></div>

<p>What did we do here? We renamed the order object o into order, and put the actions into separate methods. To improve readability a bit more we can create a method for the boolean checks. See the next example:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p135code38'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p13538"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p135code38"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isOrderShipped<span style="color: #009900;">&#40;</span>order<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> archiveOrder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isOrderCanceled<span style="color: #009900;">&#40;</span>order<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> moveReservationOnCancel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isOrderClosed<span style="color: #009900;">&#40;</span>order<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> moveReservationOnClosing<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
...
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> isOrderShipped<span style="color: #009900;">&#40;</span>OrderDetails order<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> order.<span style="color: #006633;">isShipped</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>order.<span style="color: #006633;">isCancelled</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>order.<span style="color: #006633;">isClosed</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> isOrderCancelled<span style="color: #009900;">&#40;</span>OrderDetails order<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>order.<span style="color: #006633;">isShipped</span> <span style="color: #339933;">&amp;&amp;</span> order.<span style="color: #006633;">isCancelled</span> <span style="color: #339933;">&amp;&amp;</span> order.<span style="color: #006633;">isCancelledAt</span>.<span style="color: #006633;">getTimeInMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> cal.<span style="color: #006633;">getTimeInMillis</span><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: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> isOrderClosed<span style="color: #009900;">&#40;</span>OrderDetails order<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>order.<span style="color: #006633;">isShipped</span> <span style="color: #339933;">&amp;&amp;</span> order.<span style="color: #006633;">isClosed</span> <span style="color: #339933;">&amp;&amp;</span> order.<span style="color: #006633;">isClosedAt</span>.<span style="color: #006633;">getTimeInMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> cal.<span style="color: #006633;">getTimeInMillis</span><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>

<p>Pretty readable isn&#8217;t it? You just need a few seconds to understand what is going on here. You have the main flow right in sight. When it is needed to go deeper into the code you can pick any method you like to inspect. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.sobek.info/blog/2009/12/01/replace-nested-conditionals-with-guard-clauses/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

