<?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>Transylvania JUG</title>
	<atom:link href="http://www.transylvania-jug.org/feed" rel="self" type="application/rss+xml" />
	<link>http://www.transylvania-jug.org</link>
	<description>Welcome to the Transylvania Java User Group</description>
	<lastBuildDate>Sat, 17 Mar 2012 21:08:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Transylvania JUG Meeting #37: JBoss AS 7</title>
		<link>http://www.transylvania-jug.org/archives/420</link>
		<comments>http://www.transylvania-jug.org/archives/420#comments</comments>
		<pubDate>Sat, 17 Mar 2012 21:08:12 +0000</pubDate>
		<dc:creator>Gabriel Pop</dc:creator>
				<category><![CDATA[Transylvania JUG]]></category>

		<guid isPermaLink="false">http://www.transylvania-jug.org/?p=420</guid>
		<description><![CDATA[Transylvania JUG&#8217;s 37th meeting took place on the 13th of March, our guest was Kabir Khan from JBoss. He gave us an update on the current state of JBoss 7. The slides are here If there&#8217;s one thing you need &#8230; <a href="http://www.transylvania-jug.org/archives/420">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Transylvania JUG&#8217;s 37th meeting took place on the 13th of March, our guest was <a href="http://planet.jboss.org/view/feed.seam?name=kabir&#038;from=0" title="Kabir Khan">Kabir Khan</a> from JBoss.<br />
He gave us an update on the current state of JBoss 7.<br />
The slides are <a href='http://www.transylvania-jug.org/wp-content/uploads/2012/03/TransylvaniaJUG2.ppt'>here</a></p>
<p>If there&#8217;s one thing you need to know about JBoss AS 7 is that it&#8217;s extremely fast. You definetly need to have a look at it if you need a container for your next java project.</p>
<p>The EE 6 demo Kabir did was mainly a combination of a few of the quickstarts, so he could introduce the concepts gradually using the same application. The quickstarts can be found <a href="http://www.jboss.org/jbossas/downloads">here</a> and some of them are explained in much more detail at <a href="http://www.jboss.org/jbossas/docs/7-x">this page </a> in the &#8220;Getting Started Developing Applications Guide&#8221;.</p>
<p>The archetype for creating the html 5 stuff can be found <a href="https://community.jboss.org/wiki/GetStartedWithHTML5MobileWebDevelopmentWithJBoss">here</a>. There is a link to it from the  <a href="http://www.jboss.org/aerogear">aerogear project</a><br />
Meeting was sponsored by Betfair Romania.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.transylvania-jug.org/archives/420/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Enum puzzler</title>
		<link>http://www.transylvania-jug.org/archives/404</link>
		<comments>http://www.transylvania-jug.org/archives/404#comments</comments>
		<pubDate>Wed, 14 Mar 2012 14:22:31 +0000</pubDate>
		<dc:creator>cristian.lungu</dc:creator>
				<category><![CDATA[Transylvania JUG]]></category>

		<guid isPermaLink="false">http://www.transylvania-jug.org/?p=404</guid>
		<description><![CDATA[Let&#8217;s suppose we have the following code: enum Case {     CASE_ONE,     CASE_TWO,     CASE_THREE;          private static final int counter;     private int valueDependsOnCounter;          static {   &#8230; <a href="http://www.transylvania-jug.org/archives/404">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div>Let&#8217;s suppose we have the following code:</div>
<div><span style="font-family: 'courier new', monospace">enum Case {</span></div>
<div><span style="font-family: 'courier new', monospace">    CASE_ONE,</span></div>
<div><span style="font-family: 'courier new', monospace">    CASE_TWO,</span></div>
<div><span style="font-family: 'courier new', monospace">    CASE_THREE;</span></div>
<div><span style="font-family: 'courier new', monospace">    </span></div>
<div><span style="font-family: 'courier new', monospace">    private static final int counter;</span></div>
<div><span style="font-family: 'courier new', monospace"><br />
</span></div>
<div><span style="font-family: 'courier new', monospace">    private int valueDependsOnCounter;</span></div>
<div><span style="font-family: 'courier new', monospace">    </span></div>
<div><span style="font-family: 'courier new', monospace">    static {</span></div>
<div><span style="font-family: 'courier new', monospace">        int sum = 0;</span></div>
<div><span style="font-family: 'courier new', monospace">        for(int i = 0; i&lt;10; i++) {</span></div>
<div><span style="font-family: 'courier new', monospace">            sum +=i;</span></div>
<div><span style="font-family: 'courier new', monospace">        }</span></div>
<div><span style="font-family: 'courier new', monospace">        </span></div>
<div><span style="font-family: 'courier new', monospace">        counter = sum;</span></div>
<div><span style="font-family: 'courier new', monospace">    }</span></div>
<div><span style="font-family: 'courier new', monospace">    </span></div>
<div><span style="font-family: 'courier new', monospace">    Case() {</span></div>
<div><span style="font-family: 'courier new', monospace">        this.valueDependsOnCounter = counter*counter;</span></div>
<div><span style="font-family: 'courier new', monospace">    }</span></div>
<div><span style="font-family: 'courier new', monospace">}</span></div>
<div></div>
<div></div>
<p></p>
<div>What do you think is the result of compiling and running the code?</div>
<div>
<ol>
<li>Compiler error</li>
<li>Runtime error</li>
<li>Runs ok but valueDependsOnCounter has a strange value</li>
<li>Runs ok</li>
</ol>
</div>
<p></p>
<div>Give it a second of thought. (Spoiler block) The answer is the 8th letter in the following sequence: bdcadcbabcad.</div>
<p></p>
<div>To shed a light on this it&#8217;s neccesary to review the following:</div>
<p></p>
<div>A. The order of static initalization inside a class:</div>
<div>
<ol>
<li>static viaribales in the order they apear</li>
<li>static blocks in the order they apear</li>
<li>instance variables in the order they appear</li>
<li>constructors</li>
</ol>
</div>
<p></p>
<div>B. The order of constructor calling (this aplies to the statics as well):</div>
<div>
<ol>
<li>super classes</li>
<li>local class</li>
</ol>
</div>
<p></p>
<div>C. The way in which a enum object is represented in java:</div>
<div></div>
<p></p>
<div>       1) An enums of name E is a class that among others, has an<strong> *implicit*</strong> static final field named n of type <span style="font-family: 'courier new', monospace">E</span> for every member of the enum. More specificaly, the <span style="font-family: 'courier new', monospace">Case</span> class could be written in the following way:</div>
<p></p>
<div><span style="font-family: 'courier new', monospace">    enum Case {</span></div>
<div><span style="font-family: 'courier new', monospace">        public static final Case CASE_ONE;</span></div>
<div><span style="font-family: 'courier new', monospace">        public static final Case CASE_TWO;</span></div>
<div><span style="font-family: 'courier new', monospace">        public static final Case CASE_THREE;</span></div>
<div><span style="font-family: 'courier new', monospace">        </span></div>
<div><span style="font-family: 'courier new', monospace">        &#8230;</span></div>
<div><span style="font-family: 'courier new', monospace">        }</span></div>
<p></p>
<div>    2) The above members apear in the order they are declared and are located above all the other static members of the enum (that means they are the first ones to be initialized).</div>
<div>    3) The enum constant is said to be created when the corresponding field is initialized.</div>
<div>So the compiler gives an error something like<span style="font-family: 'courier new', monospace"> &#8221;It is illegal to access static member counter from enum or instance initializer.&#8221;</span>. This is because the order in which the enums are initialized:</div>
<div></div>
<p></p>
<div>    1) <span style="font-family: 'courier new', monospace">public static final Case CASE_ONE;</span></div>
<div>    2) <span style="font-family: 'courier new', monospace">public static final Case CASE_TWO;</span></div>
<div>    3) <span style="font-family: 'courier new', monospace">public static final Case CASE_THREE;</span></div>
<div>    4) <span style="font-family: 'courier new', monospace">public static final counter;</span></div>
<div>    5) <span style="font-family: 'courier new', monospace">static {</span></div>
<div><span style="font-family: 'courier new', monospace">        ..</span></div>
<div><span style="font-family: 'courier new', monospace">        counter = something;</span></div>
<div><span style="font-family: 'courier new', monospace">        }     </span></div>
<div><span style="font-family: 'courier new', monospace">        </span></div>
<div>    6) <span style="font-family: 'courier new', monospace">Case() {</span></div>
<div><span style="font-family: 'courier new', monospace">            this.valueDependsOnCounter = counter*counter;</span></div>
<div><span style="font-family: 'courier new', monospace">        }</span></div>
<div></div>
<p></p>
<div>The first thing that needs to be done is init the CASE_ONE but that would have to call the Case() constructor which in turn depends on the counter which is only initialized in the static {} block (but which hasn&#8217;t been executed yet). Now accessing a static from a constructor would be a huge limitation but this is what this flow somehow suggest, that you cannot use statics in a constructor of an enum. Luckly, this is not quite right. What the error is actually trying to tell us is that<span style="font-family: 'courier new', monospace"> &#8221;It is a compile-time error to reference a static field of an enum type that is not a <strong>*compile-time constant*</strong> from constructors, instance initializer blocks, or instance variable initializer expressions of that type.&#8221;</span>. The compiler does in fact allow access to statics fields in a enum constructor but only for those that it can compute staticaly (as an optimization mechanism). If we had:</div>
<div style="padding-left: 30px"><span style="font-family: 'courier new', monospace">enum Case {</span></div>
<div style="padding-left: 30px"><span style="font-family: 'courier new', monospace">    CASE_ONE,</span></div>
<div style="padding-left: 30px"><span style="font-family: 'courier new', monospace">    CASE_TWO,</span></div>
<div style="padding-left: 30px"><span style="font-family: 'courier new', monospace">    CASE_THREE;</span></div>
<div style="padding-left: 30px"><span style="font-family: 'courier new', monospace">    </span></div>
<div style="padding-left: 30px"><span style="font-family: 'courier new', monospace">    private static final int counter = 0;</span></div>
<div style="padding-left: 30px"><span style="font-family: 'courier new', monospace"><br />
</span></div>
<div style="padding-left: 30px"><span style="font-family: 'courier new', monospace">    private int valueDependsOnCounter;    </span></div>
<div style="padding-left: 30px"><span style="font-family: 'courier new', monospace">    </span></div>
<div style="padding-left: 30px"><span style="font-family: 'courier new', monospace">    Case() {</span></div>
<div style="padding-left: 30px"><span style="font-family: 'courier new', monospace">        this.valueDependsOnCounter = counter*counter;</span></div>
<div style="padding-left: 30px"><span style="font-family: 'courier new', monospace">    }</span></div>
<div style="padding-left: 30px"><span style="font-family: 'courier new', monospace">}</span></div>
<div></div>
<p></p>
<div>, all would have been fine since the compiler could have predicted the initalization of counter, use it in the constructor, build the enum instance, and assign it to the static final <span style="font-family: 'courier new', monospace">CASE_ONE</span> variable.</div>
<div>But since counter depends on some hard to predict computation, an error is raised.</div>
<p></p>
<div>There are two solutions for this problem, in order to still have the code work:</div>
<div></div>
<p></p>
<div>        1) Put the statics that you need in a nested class and access them from there:</div>
<div><span style="font-family: 'courier new', monospace">class Nested {</span></div>
<div><span style="font-family: 'courier new', monospace">    private static final int counter;</span></div>
<div><span style="font-family: 'courier new', monospace">    static {</span></div>
<div><span style="font-family: 'courier new', monospace">        int sum = 0;</span></div>
<div><span style="font-family: 'courier new', monospace">        for(int i = 0; i&lt;10; i++) {</span></div>
<div><span style="font-family: 'courier new', monospace">            sum +=i;</span></div>
<div><span style="font-family: 'courier new', monospace">        }</span></div>
<div><span style="font-family: 'courier new', monospace">        </span></div>
<div><span style="font-family: 'courier new', monospace">        counter = sum;</span></div>
<div><span style="font-family: 'courier new', monospace">    }</span></div>
<div><span style="font-family: 'courier new', monospace">  }</span></div>
<div><span style="font-family: 'courier new', monospace">  </span></div>
<div><span style="font-family: 'courier new', monospace">enum Case {</span></div>
<div><span style="font-family: 'courier new', monospace">    CASE_ONE,</span></div>
<div><span style="font-family: 'courier new', monospace">    CASE_TWO,</span></div>
<div><span style="font-family: 'courier new', monospace">    CASE_THREE;</span></div>
<div><span style="font-family: 'courier new', monospace">    </span></div>
<div><span style="font-family: 'courier new', monospace">    private static final int counter;</span></div>
<div><span style="font-family: 'courier new', monospace"><br />
</span></div>
<div><span style="font-family: 'courier new', monospace">    private int valueDependsOnCounter;        </span></div>
<div><span style="font-family: 'courier new', monospace">    </span></div>
<div><span style="font-family: 'courier new', monospace">    Case() {</span></div>
<div><span style="font-family: 'courier new', monospace">        this.valueDependsOnCounter = Nested.counter*Nested.counter;</span></div>
<div><span style="font-family: 'courier new', monospace">    }</span></div>
<div><span style="font-family: 'courier new', monospace">}  </span></div>
<p></p>
<div>    2) Initialize in a static block not in the constructor (recomended):</div>
<div><span style="font-family: 'courier new', monospace">enum Case {</span></div>
<div><span style="font-family: 'courier new', monospace">    CASE_ONE,</span></div>
<div><span style="font-family: 'courier new', monospace">    CASE_TWO,</span></div>
<div><span style="font-family: 'courier new', monospace">    CASE_THREE;</span></div>
<div><span style="font-family: 'courier new', monospace">    </span></div>
<div><span style="font-family: 'courier new', monospace">    private static final int counter;</span></div>
<div><span style="font-family: 'courier new', monospace"><br />
</span></div>
<div><span style="font-family: 'courier new', monospace">    private int valueDependsOnCounter;        </span></div>
<div><span style="font-family: 'courier new', monospace">  </span></div>
<div><span style="font-family: 'courier new', monospace">    static {</span></div>
<div><span style="font-family: 'courier new', monospace">        int sum = 0;</span></div>
<div><span style="font-family: 'courier new', monospace">        for(int i = 0; i&lt;10; i++) {</span></div>
<div><span style="font-family: 'courier new', monospace">            sum +=i;</span></div>
<div><span style="font-family: 'courier new', monospace">        }</span></div>
<div><span style="font-family: 'courier new', monospace">        </span></div>
<div><span style="font-family: 'courier new', monospace">        counter = sum;</span></div>
<div><span style="font-family: 'courier new', monospace">        </span></div>
<div><span style="font-family: 'courier new', monospace">        for(Case c : Case.values()) {</span></div>
<div><span style="font-family: 'courier new', monospace">            c.valueDependsOnCounter = counter*counter;</span></div>
<div><span style="font-family: 'courier new', monospace">        }</span></div>
<div><span style="font-family: 'courier new', monospace">    }</span></div>
<div><span style="font-family: 'courier new', monospace">}</span></div>
<div></div>
<p></p>
<div>The exception discussed is even specified in the JAVA specification document [2].</div>
<p></p>
<p>[Originally hosted at: <a title="http://www.scientixs.com/2011/07/enum-puzzler/" href="http://www.scientixs.com/2011/07/enum-puzzler/">www.scientixs.com</a>]</p>
<div>References:</div>
<div> [1]  http://www.jroller.com/ethdsy/entry/static_fields_in_enum</div>
<div> [2]  http://java.sun.com/docs/books/jls/third_edition/html/classes.html#301020</div>
]]></content:encoded>
			<wfw:commentRss>http://www.transylvania-jug.org/archives/404/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Automaton implementation in Java</title>
		<link>http://www.transylvania-jug.org/archives/396</link>
		<comments>http://www.transylvania-jug.org/archives/396#comments</comments>
		<pubDate>Wed, 14 Mar 2012 14:13:46 +0000</pubDate>
		<dc:creator>cristian.lungu</dc:creator>
				<category><![CDATA[Transylvania JUG]]></category>

		<guid isPermaLink="false">http://www.transylvania-jug.org/?p=396</guid>
		<description><![CDATA[This post will address the problem of implementing finite states machines into java. If you don&#8217;t know what FSM are or where can be used you may be keen on reading this, this and this. If you ever found yourself in the situation &#8230; <a href="http://www.transylvania-jug.org/archives/396">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This post will address the problem of implementing finite states machines into java. If you don&#8217;t know what FSM are or where can be used you may be keen on reading <a title="http://en.wikipedia.org/wiki/Automata_theory" href="http://en.wikipedia.org/wiki/Automata_theory">this</a>, <a title="http://en.wikipedia.org/wiki/Finite-state_machine" href="http://en.wikipedia.org/wiki/Finite-state_machine">this</a> and <a title="http://en.wikipedia.org/wiki/Automata-based_programming" href="http://en.wikipedia.org/wiki/Automata-based_programming">this</a>.</p>
<p>If you ever found yourself in the situation of using a FSM on your design you have probably started to write classes for every state which implemented the same interface. A good design might have been:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">interface</span> State <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">class</span> State_1 <span style="color: #000000; font-weight: bold;">implements</span> State <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">class</span> State_2 <span style="color: #000000; font-weight: bold;">implements</span> State <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
...</pre></div></div>

<p>And you probably had a class that managed these states and the transitions between them, and another that implemented the Context of the FSM (the input band strip) and another interface for the starting states and one more for the end states and so forth. A lot of classes spread out in a lot of files that makes you to quickly lose track of them.</p>
<p>There is another way: using enums.</p>
<p>Enums are essentially list of classes, and each member of the enum may have a different implementation. suppose we have the following state machine that we want to implement:</p>
<pre>Init -('a')-&gt; A
Init -(else)-&gt; Fail
A -('b')-&gt; B
A -('c')-&gt; C
A -('a')-&gt; A
A -('')-&gt; End
A -(else)-&gt; Fail
B -('c')-&gt; C
B -('b')-&gt; B
B -('')-&gt; End
B -(else)-&gt; Fail
C -('c')-&gt; C
C -('')-&gt; End
C -(else)-&gt; Fail</pre>
<p>This FSM will validate the following regexp: ^(a+)(b*)(c*)$. We can write the states as elements of the enum State as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">interface</span> State <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> State next<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Input <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> input<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> current<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> Input<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> input<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;">input</span> <span style="color: #339933;">=</span> input<span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">char</span> read<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> input.<span style="color: #006633;">charAt</span><span style="color: #009900;">&#40;</span>current<span style="color: #339933;">++</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;">enum</span> States <span style="color: #000000; font-weight: bold;">implements</span> State <span style="color: #009900;">&#123;</span>
    Init <span style="color: #009900;">&#123;</span>
        @Override
        <span style="color: #000000; font-weight: bold;">public</span> State next<span style="color: #009900;">&#40;</span>Input word<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>word.<span style="color: #006633;">read</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>
                <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">'a'</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">return</span> A<span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">default</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">return</span> Fail<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>,
    A <span style="color: #009900;">&#123;</span>
        @Override
        <span style="color: #000000; font-weight: bold;">public</span> State next<span style="color: #009900;">&#40;</span>Input word<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>word.<span style="color: #006633;">read</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>
                <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">'a'</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">return</span> A<span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">'b'</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">return</span> B<span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">'c'</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">return</span> C<span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">default</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">return</span> Fail<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>,
    B <span style="color: #009900;">&#123;</span>
        @Override
        <span style="color: #000000; font-weight: bold;">public</span> State next<span style="color: #009900;">&#40;</span>Input word<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>word.<span style="color: #006633;">read</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>
                <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">'b'</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">return</span> B<span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">'c'</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">return</span> C<span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">default</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">return</span> Fail<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>,
    C <span style="color: #009900;">&#123;</span>
        @Override
        <span style="color: #000000; font-weight: bold;">public</span> State next<span style="color: #009900;">&#40;</span>Input word<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>word.<span style="color: #006633;">read</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>
                <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">'c'</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">return</span> C<span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">default</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">return</span> Fail<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>,
    Fail <span style="color: #009900;">&#123;</span>
        @Override
        <span style="color: #000000; font-weight: bold;">public</span> State next<span style="color: #009900;">&#40;</span>Input word<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
               <span style="color: #000000; font-weight: bold;">return</span> Fail<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> State next<span style="color: #009900;">&#40;</span>Input word<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>What we&#8217;ve done is we&#8217;ve defined the transitions of every state inside each enum. Each transition returns a new state and as such we can cycle through them more efficiently:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">State s<span style="color: #339933;">;</span>
Input in <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Input<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;aabbbc&quot;</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>s <span style="color: #339933;">=</span> States.<span style="color: #006633;">Init</span><span style="color: #339933;">;</span> s <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> s <span style="color: #339933;">!=</span> States.<span style="color: #006633;">Fail</span><span style="color: #339933;">;</span> s <span style="color: #339933;">=</span> s.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span>in<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>s <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">printlin</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Valid!&quot;</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;">else</span> <span style="color: #009900;">&#123;</span><span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Failed&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>At this point, we have either validated the string or failed. It is a simple and elegant design.</p>
<p>We could further improve the implementation by separating the final states from the main ones as to simplify the exit condition of the traversal. We will define another interface called FinalState and a second enum that would contain the desired exit states (changing also the States enum accordingly):</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">interface</span> FinalState <span style="color: #000000; font-weight: bold;">extends</span> State <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">enum</span> FinalStates <span style="color: #000000; font-weight: bold;">implements</span> FinalState <span style="color: #009900;">&#123;</span>
    Fail <span style="color: #009900;">&#123;</span>
        @Override
        <span style="color: #000000; font-weight: bold;">public</span> State next<span style="color: #009900;">&#40;</span>Input word<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
               <span style="color: #000000; font-weight: bold;">return</span> Fail<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>,
    Ok <span style="color: #009900;">&#123;</span>
        @Override
        <span style="color: #000000; font-weight: bold;">public</span> State next<span style="color: #009900;">&#40;</span>Input word<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
               <span style="color: #000000; font-weight: bold;">return</span> End<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As such, the traversal will be somewhat simplified:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>s <span style="color: #339933;">=</span> States.<span style="color: #006633;">Init</span><span style="color: #339933;">;</span> <span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>s <span style="color: #000000; font-weight: bold;">instanceof</span> FinalState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> s <span style="color: #339933;">=</span> s.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span>in<span style="color: #009900;">&#41;</span><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;">switch</span><span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">case</span> Fail<span style="color: #339933;">:</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">printlin</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Valid!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">case</span> Ok<span style="color: #339933;">:</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Failed&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">default</span><span style="color: #339933;">:</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Undefined&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The advantage is that (besides a simpler traversal) we could specify more than two final states. Most of the times, a FSM will have more than one exit point so this last improvement is recommended for the long-term. You can also place all these enums and interfaces inside the same source file and have the whole logic in one place rather than surf through multiple tabs in order to understand how the flow works.</p>
<p>As a conclusion, using enums makes for a more compact and meaningful implementation of a FSM. You have all the logic in one file and all traversal is painless. You will also have a much more easier debugging experience since the name of the states in which you&#8217;ve translated will appear in the debugger ( the variable s will change its value accordingly, bearing the name of the new state) rather have to figure out what class you have just jumped to. All in all I think it&#8217;s a good technique to have in mind.</p>
<p>[Originally hosted at: <a title="http://www.scientixs.com/2011/08/automation-implementation-in-java/" href="http://www.scientixs.com/2011/08/automation-implementation-in-java/">www.scientixs.com</a>]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.transylvania-jug.org/archives/396/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Deserializing arbitrary messages with Google Protcol Buffers</title>
		<link>http://www.transylvania-jug.org/archives/365</link>
		<comments>http://www.transylvania-jug.org/archives/365#comments</comments>
		<pubDate>Sat, 03 Mar 2012 20:25:21 +0000</pubDate>
		<dc:creator>attila.balazs</dc:creator>
				<category><![CDATA[Transylvania JUG]]></category>
		<category><![CDATA[espresso-shots]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[protobuf]]></category>
		<category><![CDATA[tips'n'tricks]]></category>

		<guid isPermaLink="false">http://www.transylvania-jug.org/?p=365</guid>
		<description><![CDATA[Google Protocol Buffers (shortened protobuf) is data (de)serialization with good speed, size and cross-platform support (it supports Java, C++ and Python out of the box). It can also handle different versions of the same message (as in: old and new &#8230; <a href="http://www.transylvania-jug.org/archives/365">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.transylvania-jug.org/wp-content/uploads/2012/03/235030820_27e32eb23b_o.jpg"><img src="http://www.transylvania-jug.org/wp-content/uploads/2012/03/235030820_27e32eb23b_o-300x240.jpg" alt="" width="300" height="240" class="alignright size-medium wp-image-366" /></a></p>
<p><a href="http://code.google.com/apis/protocolbuffers/">Google Protocol Buffers</a> (shortened protobuf) is data (de)serialization with good speed, size and cross-platform support (it supports Java, C++ and Python out of the box). It can also handle different versions of the same message (as in: old and new code running side-by-side) if its rules are followed. There is one cornercase which it doesn&#8217;t handle well: parsing of arbitrary messages (one example where you might need this would be a logging daemon which sits on your network and logs all the messages going by for later debugging). The reason for this is that in protobuf data travels without its metadata (ie. the description of the message format &#8211; number of fields, their types, etc) and it supposes that the metadata is present on both ends (it can be different versions of the metadata though).</p>
<p>To parse an arbitrary message you need access to its metadata, there is no way around it. What I would like to present in this post is a simplification of the <a href="http://stackoverflow.com/questions/4035027/generic-parsing-of-pb-in-java">usual solution</a> (generating a &#8220;.desc&#8221; file &#8211; which involves). The solution is completely runtime and no extra build steps are needed after the initial setup. This is how it works:</p>
<p>We set up a generic &#8220;Metadata&#8221; structure. This is the only thing which is needed to be shared between different communicating parties:
<pre><code>package tutorial;

option java_package = "com.example.protoctest";
option java_outer_classname = "MetadataProtos";

message Metadata {
  required bytes field_descriptor = 1;
  optional int32 field_descriptor_idx = 2;

  repeated Metadata dependencies = 3;
}
</code></pre>
<p>Next, we populate this structure for the message we want to send. You can look at <a href="https://code.google.com/p/hype-free/source/browse/trunk/protoc-test/src/test/java/com/example/protoctest/ArbitraryProtoMessageTest.java#48">the actual code</a> for details. The most important aspect is that this needs to be done recursively since protobuf allows for the nesting of types (although not for inheritance).</p>
<p>Now, we can ship over the metadata and data to the client. Probably each communication channel/topic has a limited set of message types it can contain, and as such you won&#8217;t need to waste bandwidth with the metadata before or after each message (you can send it once per channel, or maybe every time a new client joins or the message version changes, etc).</p>
<p>At the client you can deserialize the metadata and using it together with <a href="http://code.google.com/apis/protocolbuffers/docs/reference/java/com/google/protobuf/DynamicMessage.html">DynamicMessage</a> reconstruct the contents of the message. Again, you can see the full details <a href="https://code.google.com/p/hype-free/source/browse/trunk/protoc-test/src/test/java/com/example/protoctest/ArbitraryProtoMessageTest.java#27">in the code</a>.</p>
<p>Hope this helps somebody facing a similar need! The code can also be used as a reference for <a href="https://code.google.com/p/hype-free/source/browse/trunk/protoc-test/pom.xml">how to use the maven-protoc plugin</a>. Finally a word of caution: make sure that your proto compiler (protoc) and the runtime libraries used match as closely as possible in version (for example protc 2.4.0 and protobuf-java 2.4.1 is ok, but 2.2.0 wouldn&#8217;t), otherwise you will get some really hard to track down compile time / runtime errors.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.transylvania-jug.org/archives/365/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>#qconlondon registration &#8211; 100&#8230;</title>
		<link>http://www.transylvania-jug.org/archives/347</link>
		<comments>http://www.transylvania-jug.org/archives/347#comments</comments>
		<pubDate>Mon, 16 Jan 2012 20:07:37 +0000</pubDate>
		<dc:creator>Gabriel Pop</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.transylvania-jug.org/archives/347</guid>
		<description><![CDATA[#qconlondon registration &#8211; 100 pounds discount for transylvania jug members &#8211; check your emails]]></description>
			<content:encoded><![CDATA[<p>#qconlondon registration &#8211; 100 pounds discount for transylvania jug members &#8211; check your emails</p>
]]></content:encoded>
			<wfw:commentRss>http://www.transylvania-jug.org/archives/347/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

