<?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>[MeIsProgrammer] &#187; Java</title>
	<atom:link href="http://just-thor.com/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://just-thor.com</link>
	<description>all play and no work makes me a dull boy</description>
	<lastBuildDate>Sat, 03 Jul 2010 18:28:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Java 4-ever video</title>
		<link>http://just-thor.com/2010/06/29/java-4-ever-video/</link>
		<comments>http://just-thor.com/2010/06/29/java-4-ever-video/#comments</comments>
		<pubDate>Tue, 29 Jun 2010 04:41:57 +0000</pubDate>
		<dc:creator>Thor</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://just-thor.com/?p=148</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="640" height="385" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/fzza-ZbEY70&amp;hl=en_US&amp;fs=1&amp;" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="640" height="385" src="http://www.youtube.com/v/fzza-ZbEY70&amp;hl=en_US&amp;fs=1&amp;" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://just-thor.com/2010/06/29/java-4-ever-video/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Implement Index controller in Spring Roo</title>
		<link>http://just-thor.com/2010/06/28/implement-index-controller-in-spring-roo/</link>
		<comments>http://just-thor.com/2010/06/28/implement-index-controller-in-spring-roo/#comments</comments>
		<pubDate>Mon, 28 Jun 2010 08:01:05 +0000</pubDate>
		<dc:creator>Thor</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Roo]]></category>
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://just-thor.com/?p=144</guid>
		<description><![CDATA[Recently been using Spring roo to produce a rapid prototype. I must say that Spring roo is a very good framework, although still lacking of some features but the potentials [...]]]></description>
			<content:encoded><![CDATA[<p>Recently been using Spring roo to produce a rapid prototype. I must say that Spring roo is a very good framework, although still lacking of some features but the potentials are there.</p>
<p>When you start a new Spring roo project, the default page will be &#8220;index.jspx&#8221;, what if we want to implement a controller to it?</p>
<p>First of all, locate the webmvc-config.xml in your project folder. Find and remove the following line<br />
<code>&amp;lt;mvc:view-controller path=&quot;/index&quot;/&amp;gt;</code></p>
<p>Then, create a Java class file in the source folder, in my case, will be the web folder. Let&#8217;s call it IndexController<br />
<pre><pre class="brush: java">
package com.justthor.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@RequestMapping(&quot;/&quot;)
@Controller
public class IndexController {
&nbsp;&nbsp;@RequestMapping(value = &quot;/index&quot;, method = RequestMethod.GET)
&nbsp;&nbsp;public String index(ModelMap modelMap) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/* Implement logic here...*/
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return &quot;index&quot;;
&nbsp;&nbsp;}
}</pre></pre><br />
That&#8217;s it. To test it, you can implement a logger in your controller. Its quite useful if we want to load some data on the index page.</p>
<p>Happy coding <img src='http://just-thor.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://just-thor.com/2010/06/28/implement-index-controller-in-spring-roo/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A circular linked list in Java</title>
		<link>http://just-thor.com/2009/12/03/a-circular-linked-list-in-java/</link>
		<comments>http://just-thor.com/2009/12/03/a-circular-linked-list-in-java/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 10:26:21 +0000</pubDate>
		<dc:creator>Thor</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://just-thor.com/?p=117</guid>
		<description><![CDATA[I was helping a friend of mine with her work today, and we came across with a requirement that there are few players in the room we need to know [...]]]></description>
			<content:encoded><![CDATA[<p>I was helping a friend of mine with her work today, and we came across with a requirement that there are few players in the room we need to know whose turn is it after this. Yes, we can definitely use a LinkedList or an ArrayList, but its not so convenient as we have to check whether this player is at the last position of the list. If so,we will need to start from the beginning of the list again. And its also quite cumbersome to retrieve the next player info after this. Therefore, I wrote a simple yet functional Circular Linked List. You can download the source code and the compiled class <a href="http://just-thor.com/sample-works/CircularLinkedList.jar">here</a> . I will port this over to Scala whenever I have the time to do so.</p>
<p>Happy coding <img src='http://just-thor.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://just-thor.com/2009/12/03/a-circular-linked-list-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>File Reading in Java and Scala</title>
		<link>http://just-thor.com/2009/10/01/file-reading-in-java-and-scala/</link>
		<comments>http://just-thor.com/2009/10/01/file-reading-in-java-and-scala/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 16:46:47 +0000</pubDate>
		<dc:creator>Thor</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://just-thor.com/?p=75</guid>
		<description><![CDATA[Today we are going to look at reading files in Java and how to do the same thing in Scala. This is normally how we read files in Java.First we [...]]]></description>
			<content:encoded><![CDATA[<p>Today we are going to look at reading files in Java and how to do the same thing in Scala.</p>
<p>This is normally how we read files in Java.First we open a reading stream, then read the content and process it,and at last we close the stream.</p>
<p><pre><pre class="brush: java">
package com.justthor;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class SampleFileReader {

&nbsp;&nbsp;/**
&nbsp;&nbsp; * @param args
&nbsp;&nbsp; */
&nbsp;&nbsp;public static void main(String[] args) {
&nbsp;&nbsp;&nbsp;&nbsp;BufferedReader in; 
&nbsp;&nbsp;&nbsp;&nbsp;try {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Open file
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;in = new BufferedReader(new FileReader(&quot;test.txt&quot;));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String str;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Start reading
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while ((str = in.readLine()) != null) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Print the content
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(str);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Close the stream
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;in.close();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (IOException e) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}
}
</pre></pre></p>
<p>With some third party libraries, such as <a href="http://commons.apache.org/io/index.html">Apache Common Io</a>, we can simplify the code to something like this.</p>
<p><pre><pre class="brush: java">
package com.justthor;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;

public class SampleFileReader {

&nbsp;&nbsp;/**
&nbsp;&nbsp; * @param args
&nbsp;&nbsp; */
&nbsp;&nbsp;@SuppressWarnings(&quot;unchecked&quot;)
&nbsp;&nbsp;public static void main(String[] args) {
&nbsp;&nbsp;&nbsp;&nbsp;try {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List&lt;String&gt; contents = FileUtils.readLines(new File(&quot;test.txt&quot;));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for ( String content : contents ) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(content);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;} catch (IOException e) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// TODO Auto-generated catch block
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}
}
</pre></pre></p>
<p>Well, it does look a bit simple now compare to the previous version. But we only manage to type 1 or 2 lines less than the previous version. This is due to we are force to handle the checked IOException. What if we try to achieve the same thing using Scala?</p>
<p><pre><pre class="brush: scala">
package com.justthor

import scala.io.Source

object SampleFileReader {
 def main(args : Array[String]) = {
&nbsp;&nbsp;Source fromFile(&quot;test.txt&quot;) getLines foreach { println }
 }
}
</pre></pre></p>
<p>Its much simple, right? Scala provides an util for IO named <strong>Source</strong>. This util read the file and get the contents by calling <strong>getLines</strong> method. This <strong>getLines</strong> method returns a <strong>Iterator[String]</strong>. And from there we can process the content by looping the returned iterator. We can use <strong>Source</strong> util to do more than reading files. I will provide more examples in the next few posts.</p>
<p>Happy coding. <img src='http://just-thor.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://just-thor.com/2009/10/01/file-reading-in-java-and-scala/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP communicate with Java using JSON &#8211; Unicode Version</title>
		<link>http://just-thor.com/2009/09/28/php-communicate-with-java-using-json-unicode-version/</link>
		<comments>http://just-thor.com/2009/09/28/php-communicate-with-java-using-json-unicode-version/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 12:30:13 +0000</pubDate>
		<dc:creator>Thor</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://just-thor.com/?p=71</guid>
		<description><![CDATA[JSON is one of the most popular message exchange format nowadays used in web application.Most of the time,we used JSON as a communication message format between Javascript and Server side [...]]]></description>
			<content:encoded><![CDATA[<p>JSON is one of the most popular message exchange format nowadays used in web application.Most of the time,we used JSON as a communication message format between Javascript and Server side language,such as PHP.We can also use it to exchange messages between different languages.</p>
<p>For instance,we want to store a PHP array in database and later to be processed by Java,we can do something like</p>
<p><pre><pre class="brush: php">
$aValue = array(&quot;key1&quot;=&amp;gt;&quot;value1&quot;,&quot;key2&quot;=&amp;gt;&quot;value2&quot;);
$sEncodedValue = json_encode($aValue)
//we will get something like {&quot;key1&quot;:&quot;value1&quot;,&quot;key2&quot;:&quot;value2&quot;}
//and then, we store the value to db
</pre></pre></p>
<p>On the other hand,we use Java  to read it and decode it.</p>
<p><pre><pre class="brush: java">
//read from db and got the resultset,assuming we are using JDBC
while (rs.next()){
String rawValue = rs.getString(&quot;encodedValue&quot;);
// since we encoded an associative array,it will be translated to a Java Map object instance.
// we are using JSON-lib for JSON parsing the following example
JSONObject json = JSONObject.fromObject(jsonString);
Map decodedValue = (Map) JSONObject.toBean(json, Map.class);
String value1 = decodedValue.get(&quot;key1&quot;);
}
</pre></pre></p>
<p>What if we are trying to store unicode value using JSON format in PHP?<br />
For example,</p>
<p><pre><pre class="brush: php">
$aValue = array(&quot;key1&quot;=&amp;gt;&quot;你好吗&quot;,&quot;key2&quot;=&amp;gt;&quot;value2&quot;);
$sEncodedValue = json_encode($aValue);
//This is what we get after encoded
//{&quot;key1&quot;:&quot;\u4f60\u597d\u5417&quot;,&quot;key2&quot;:&quot;value2&quot;}
</pre></pre></p>
<p>If you decoded it back to Java,key1 value will return &#8220;u4f60u597du5417&#8243; instead.This is so wrong.</p>
<p>How to solve it?<br />
We can use <b>mb_convert_encoding</b> function in PHP.<br />
Let&#8217;s make some changes to our code.</p>
<p><pre><pre class="brush: php">
$aValue = array(&quot;key1&quot;=&amp;gt;mb_convert_encoding(&quot;你好吗&quot;, &quot;UTF-8&quot;),&quot;key2&quot;=&amp;gt;&quot;value2&quot;);
$sEncodedValue = json_encode($aValue);
</pre></pre></p>
<p>By doing so,values are stored in database in unicode format,and you can decode it back in Java.</p>
<p>Happy coding. <img src='http://just-thor.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://just-thor.com/2009/09/28/php-communicate-with-java-using-json-unicode-version/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Say hello to Java and Scala</title>
		<link>http://just-thor.com/2009/08/30/say-hello-to-java-and-scala/</link>
		<comments>http://just-thor.com/2009/08/30/say-hello-to-java-and-scala/#comments</comments>
		<pubDate>Sun, 30 Aug 2009 14:09:39 +0000</pubDate>
		<dc:creator>Thor</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://just-thor.com/?p=35</guid>
		<description><![CDATA[Let&#8217;s write a legendary &#8220;Hello world&#8221; program in both Java and Scala language. This is how we do in Java. public class Hello { public static void main(String[] args) { [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s write a legendary &#8220;Hello world&#8221; program in both Java and Scala language.</p>
<p>This is how we do in Java.<br />
<pre><pre class="brush: java">
public class Hello {
 public static void main(String[] args) {
&nbsp;&nbsp;System.out.println(&quot;Hello world&quot;);
 }
}</pre></pre><br />
and this is how we do in Scala<br />
<pre><pre class="brush: scala">object Hello {
 def main(args : Array[String]) = println(&quot;Hello world&quot;)
}</pre></pre><br />
So when you execute both code,they actually return the same result.What about printing arguments instead?</p>
<p>Java style<br />
<pre><pre class="brush: java">public class HelloArgument {
 public static void main(String[] args) {
&nbsp;&nbsp;for (String arg : args) {
&nbsp;&nbsp; System.out.println(&quot;Hello, &quot; + arg);
&nbsp;&nbsp;}
 }
}</pre></pre></p>
<p>Scala style<br />
<pre><pre class="brush: scala">object HelloArgument {
 def main(args : Array[String]) = args.foreach(arg =&amp;gt; println(&quot;Hello, &quot; + arg))
}</pre></pre></p>
<p>Pretty impressive,huh?Comparing Java,we are actually writing less and more readable code in Scala,and the results are the same.</p>
<p>Happy coding. <img src='http://just-thor.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://just-thor.com/2009/08/30/say-hello-to-java-and-scala/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
