<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>The Programmer Man</title>
	
	<link>http://theprogrammerman.com</link>
	<description />
	<lastBuildDate>Thu, 27 Oct 2011 14:51:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/TheProgrammerMan" /><feedburner:info uri="theprogrammerman" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Problem 24 in Scala</title>
		<link>http://feedproxy.google.com/~r/TheProgrammerMan/~3/MQykjjQ68Uk/</link>
		<comments>http://theprogrammerman.com/2011/10/27/problem-24-in-scala/#comments</comments>
		<pubDate>Thu, 27 Oct 2011 14:43:23 +0000</pubDate>
		<dc:creator>bibix</dc:creator>
				<category><![CDATA[project euler]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://theprogrammerman.com/?p=156</guid>
		<description><![CDATA[So, it&#8217;s more than two years since I started to use Groovy. At the beginning I solved couple Project Euler problems and moved to scripting. I still love Groovy, but couple months ago I started to look at Scala. What can I say about it? IT IS AWESOME Until I started using it, I was [...]]]></description>
			<content:encoded><![CDATA[<p>So, it&#8217;s more than two years since I started to use Groovy.<br />
At the beginning I solved couple Project Euler problems and moved to scripting. I still love Groovy, but couple months ago I started to look at Scala. What can I say about it?</p>
<p>IT IS AWESOME</p>
<p>Until I started using it, I was thinking that dynamic typing is just a hidden cost of all the features I get with Groovy. Errors at run-time were painful, but I quickly learned the patterns to follow when developing dynamically typed code.</p>
<p>With Scala I found out that static typing can be used together with all those higher order functions, functional programming etc. And it makes me much more comfortable.</p>
<p>I approached Scala the usual way. I solved first problem from Project Euler. It worked fine, but I didn&#8217;t like the style. There was too much Java in my Scala code and I knew it can look better, more functional. And the only functional language I had been using so far was Haskell.<br />
So, I took a Haskell tutorial from <a href="http://en.wikibooks.org/wiki/Haskell" title="http://en.wikibooks.org/wiki/Haskell" target="_blank"></a> and started to look for similarities in Scala. Rewriting Project Euler&#8217;s solutions in functional style was a lot of fun and training for my brain. Now it&#8217;s more natural.</p>
<p>Since I posted solution for the Problem 024 in Groovy. Here&#8217;s one in Scala:</p>
<pre class="brush: scala; title: ; notranslate">
object Problem024 extends App {
  val nums = (0 until 10).toList.permutations
  val result = nums drop(999999) next() map(_.toString) reduce(_+_)
  Console.println(result)
}
</pre>
<p>It&#8217;s a simple bruteforce. What is beautiful about it? The <code>List.permutations</code> method returns <code>Iterator</code>. In Groovy similar method returns a <code>Set</code> which makes it almost useless. There&#8217;s an alternative method in Groovy <code>Collection.eachPermutation</code>, but it takes a closure as a parameter. The third option is using <code>PermutationGenerator</code> which returns an <code>Iterator</code>, which I didn&#8217;t know of two years ago.</p>
<p>What I see is that Scala&#8217;s type system is really well thought. If there are or were any glitches, they were quickly fixed (from small version to small version). It&#8217;s not <del>four</del> two years development cycle like in case of Java.</p>
<p>So far, I&#8217;ve solved more than 40 problems in Project Euler. You can see sources at <a href="https://github.com/bibix/euler" title="github.com/bibix/euler" target="_blank">https://github.com/bibix/euler</a>. And many of them are one-liners thanks to Scala.</p>
<p>And you can always <a href="http://www.twitter.com/bibixdev">follow me on Twitter</a></p>
<img src="http://feeds.feedburner.com/~r/TheProgrammerMan/~4/MQykjjQ68Uk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://theprogrammerman.com/2011/10/27/problem-24-in-scala/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://theprogrammerman.com/2011/10/27/problem-24-in-scala/</feedburner:origLink></item>
		<item>
		<title>Gradle snippets: Modularize your scripts</title>
		<link>http://feedproxy.google.com/~r/TheProgrammerMan/~3/RTnl_UY_8Lw/</link>
		<comments>http://theprogrammerman.com/2011/03/07/gradle-snippets-modularize-your-scripts/#comments</comments>
		<pubDate>Mon, 07 Mar 2011 22:28:48 +0000</pubDate>
		<dc:creator>bibix</dc:creator>
				<category><![CDATA[gradle snippets]]></category>
		<category><![CDATA[build configuration]]></category>
		<category><![CDATA[groovy]]></category>

		<guid isPermaLink="false">http://theprogrammerman.com/?p=146</guid>
		<description><![CDATA[Build files tend to grow in time. New tasks are added, some customizations are made and you end up with several pages of groovy code. Gradle is based on Groovy, so many constructs can be really simplified, but often it&#8217;s not enough. That&#8217;s where Gradle comes in. You can split your build files into logical [...]]]></description>
			<content:encoded><![CDATA[<p>Build files tend to grow in time. New tasks are added, some customizations are made and you end up with several pages of groovy code. Gradle is based on Groovy, so many constructs can be really simplified, but often it&#8217;s not enough. That&#8217;s where Gradle comes in. You can split your build files into logical pieces:</p>
<pre class="brush: groovy; title: ; notranslate">
// in build.gradle
apply from: &quot;libraries.gradle&quot;
apply from: &quot;subprojectsTasks.gradle&quot;
</pre>
<pre class="brush: groovy; title: ; notranslate">
// in libraries.gradle
versions = [
  abc: &quot;1.6&quot;
]

libraries = [
  abc: &quot;org.abc:abc:${versions.abc}&quot;
]
</pre>
<pre class="brush: groovy; title: ; notranslate">
// in subprojetsTasks.gradle
subprojects {
  apply plugin:&quot;java&quot;

  dependencies {
    compile libraries.abc
  }
}
</pre>
<p>And that&#8217;s it. You can safely split your build file into logical parts. </p>
<img src="http://feeds.feedburner.com/~r/TheProgrammerMan/~4/RTnl_UY_8Lw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://theprogrammerman.com/2011/03/07/gradle-snippets-modularize-your-scripts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://theprogrammerman.com/2011/03/07/gradle-snippets-modularize-your-scripts/</feedburner:origLink></item>
		<item>
		<title>Gradle snippets: extract your dependencies</title>
		<link>http://feedproxy.google.com/~r/TheProgrammerMan/~3/aNBkR5jpmwA/</link>
		<comments>http://theprogrammerman.com/2011/01/20/gradle-snippets-extract-your-dependencies/#comments</comments>
		<pubDate>Thu, 20 Jan 2011 22:51:32 +0000</pubDate>
		<dc:creator>bibix</dc:creator>
				<category><![CDATA[gradle snippets]]></category>
		<category><![CDATA[build configuration]]></category>
		<category><![CDATA[dependencies]]></category>
		<category><![CDATA[groovy]]></category>

		<guid isPermaLink="false">http://theprogrammerman.com/?p=138</guid>
		<description><![CDATA[I believe you&#8217;ll find this in every gradle project you can download from the net. Since it&#8217;s groovy it&#8217;s best to just declare a map with your external dependencies in root project. You can then use it in your subprojects just by accessing a key from map. Let&#8217;s see a sample configuration: You can see [...]]]></description>
			<content:encoded><![CDATA[<p>I believe you&#8217;ll find this in every gradle project you can download from the net. Since it&#8217;s groovy it&#8217;s best to just declare a map with your external dependencies in root project. You can then use it in your subprojects just by accessing a key from map.</p>
<p>Let&#8217;s see a sample configuration:</p>
<pre class="brush: groovy; title: ; notranslate">
// def versions would create script local variable
versions = [
        camel: &quot;2.5.0&quot;
]

libraries = [
        camel: [group:&quot;org.apache.camel&quot;, name:&quot;camel-core&quot;, version:versions.camel],
        camelBindy: &quot;org.apache.camel:camel-bindy:${versions.camel}&quot;
]
</pre>
<p>You can see two maps actually: <em>versions</em> and <em>variables</em>.<br />
First one defines versions used for particular libraries. Since camel version is used in multiple places, you can just extract it and keep outside the libraries definitions.<br />
<em>Libraries</em> is a map of our dependencies.</p>
<p>Usage is also simple:</p>
<pre class="brush: groovy; title: ; notranslate">
// somewhere in root project or subprojects
dependencies {
  compile libraries.camel
  compile libraries.camelBindy
}
</pre>
<img src="http://feeds.feedburner.com/~r/TheProgrammerMan/~4/aNBkR5jpmwA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://theprogrammerman.com/2011/01/20/gradle-snippets-extract-your-dependencies/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://theprogrammerman.com/2011/01/20/gradle-snippets-extract-your-dependencies/</feedburner:origLink></item>
		<item>
		<title>Gradle snippets: Rename your subprojects’ build files</title>
		<link>http://feedproxy.google.com/~r/TheProgrammerMan/~3/34bQq_9-dlw/</link>
		<comments>http://theprogrammerman.com/2010/11/28/gradle-snippets-rename-your-subprojects-build-files/#comments</comments>
		<pubDate>Sun, 28 Nov 2010 21:18:37 +0000</pubDate>
		<dc:creator>bibix</dc:creator>
				<category><![CDATA[gradle snippets]]></category>
		<category><![CDATA[build configuration]]></category>
		<category><![CDATA[gradle]]></category>
		<category><![CDATA[groovy]]></category>

		<guid isPermaLink="false">http://theprogrammerman.com/?p=131</guid>
		<description><![CDATA[It&#8217;s good to have project separated into modules. It encourages separation of functionalities and simplifies code. Fortunately gradle supports multiple projects very well. Having a project with 2 modules like this: - awesome-project-root -- build.gradle -- settings.gradle -+ persistence ---- build.gradle -+ processing ---- build.gradle Next thing is to add to settings.gradle several lines with [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s good to have project separated into modules. It encourages separation of functionalities and simplifies code. Fortunately gradle supports multiple projects very well.<br />
Having a project with 2 modules like this:</p>
<pre>
- awesome-project-root
-- build.gradle
-- settings.gradle
-+ persistence
---- build.gradle
-+ processing
---- build.gradle
</pre>
<p>Next thing is to add to <em>settings.gradle</em> several lines with subprojects:</p>
<pre class="brush: groovy; title: build.gradle; notranslate">
include &quot;persistence&quot;
include &quot;processing&quot;
</pre>
<p>Since now you have 3 <em>build.gradle</em> files in your repository. They are configured as subprojects. You may read more about subprojects in <a href="http://gradle.org/0.9-rc-3/docs/userguide/multi_project_builds.html">gradle&#8217;s documentation</a>. It may be confusing when your projects grows up and you&#8217;ll have more of them.</p>
<p>Really nice thing I&#8217;ve found in hibernate&#8217;s configuration (see <a href="https://github.com/hibernate/hibernate-core">https://github.com/hibernate/hibernate-core</a>), is renaming of all these build.gradle files.</p>
<pre class="brush: groovy; title: settings.gradle; notranslate">
rootProject.children.each { project -&gt;
    project.buildFileName = &quot;${project.name}.gradle&quot;
    assert project.projectDir.isDirectory()
    assert project.buildFile.isFile()
}
</pre>
<p>This little code snippet lets gradle know that your project&#8217;s structure will be:</p>
<pre>
awesome-project-root
-- build.gradle
-- settings.gradle
-+ persistence
---- <strong><em>persistence.gradle</em></strong>
-+ processing
---- <strong><em>processing.gradle</em></strong>
</pre>
<p>And this really simplifies navigation. I think it&#8217;d be nice if gradle supported both naming conventions.</p>
<img src="http://feeds.feedburner.com/~r/TheProgrammerMan/~4/34bQq_9-dlw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://theprogrammerman.com/2010/11/28/gradle-snippets-rename-your-subprojects-build-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://theprogrammerman.com/2010/11/28/gradle-snippets-rename-your-subprojects-build-files/</feedburner:origLink></item>
		<item>
		<title>It’s all about dynamic</title>
		<link>http://feedproxy.google.com/~r/TheProgrammerMan/~3/e2CA7G2nT-o/</link>
		<comments>http://theprogrammerman.com/2010/02/22/its-all-about-dynamic/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 20:08:48 +0000</pubDate>
		<dc:creator>bibix</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[design pattern]]></category>
		<category><![CDATA[groovy]]></category>

		<guid isPermaLink="false">http://theprogrammerman.com/?p=99</guid>
		<description><![CDATA[In previous post I mentioned about the visitor pattern and how to use it in Java. I also use groovy in my projects and there&#8217;s a much better way to solve the problem with processing collections. I have found a site about shortcomings of the visitor pattern. The proposed solution in Nice programming language are, [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://theprogrammerman.com/2010/02/14/visitor-pattern-in-java/">previous post</a> I mentioned about the visitor pattern and how to use it in Java. I also use groovy in my projects and there&#8217;s a much better way to solve the problem with processing collections.</p>
<p>I have found a site about <a href="http://nice.sourceforge.net/visitor.html">shortcomings of the visitor pattern</a>.<br />
The proposed solution in <a href="http://nice.sourceforge.net/">Nice</a> programming language are, so called, multimethods.<br />
I asked myself if I could use similar solution in groovy. The answer is yes, fortunately. Multimethods (or multiple dispatch) can be used in groovy. </p>
<p>In groovy you don&#8217;t have to implement visitor pattern and extend your domain objects. The following code will work just as you would expect:</p>
<pre class="brush: groovy; title: ; notranslate">
// Base class
class Feature {
    String name

    public String toString() {
        return &quot;Feature{&quot; +
                &quot;name='&quot; + name + '\'' +
                '}';
    }
}

class BooleanFeature extends Feature{
    boolean value

    public String toString() {
        return &quot;BooleanFeature{&quot; +
                &quot;value=&quot; + value +
                '}';
    }
}

class StringFeature extends Feature{
    String value

    public String toString() {
        return &quot;StringFeature{&quot; +
                &quot;value='&quot; + value + '\'' +
                '}';
    }
}

// Class that processes Feature and its descendants
class Multimethod {
    def printFeature(Feature feature) {
        println &quot;printFeature: $feature&quot;
    }

    def printFeature(BooleanFeature feature) {
        println &quot;printBooleanFeature: $feature&quot;
    }

    def printFeature(StringFeature feature) {
        println &quot;printStringFeature: $feature&quot;
    }
}

Feature feature = new Feature(name:'feature')
Feature booleanFeature = new BooleanFeature(name:'booleanFeature', value:true)
Feature stringFeature = new StringFeature(name:'stringFeature', value:'Hello world')
List&lt;Feature&gt; features = [feature, booleanFeature, stringFeature, otherFeature]

Multimethod multimethod = new Multimethod()

features.each { multimethod.printFeature it}
</pre>
<p>The output is:</p>
<pre class="brush: plain; title: ; notranslate">
printFeature: Feature{name='feature'}
printBooleanFeature: BooleanFeature{value=true}
printStringFeature: StringFeature{value='Hello world'}
</pre>
<p>You can see the <em>Feature</em> class and some simple hierarchy. They represent domain classes. The <em>Multimethod</em> class is our processor. It would have to be a Visitor in java, in order to correctly process the collection of <em>Feature</em> elements. It&#8217;s not the case in groovy, as it dynamically resolves the type of parameters during runtime. </p>
<p>It saves a lot of coding. Now there&#8217;s no need to create class hierarchy only to process a collection properly. This also means, that there&#8217;s no need to overload a method for all classes that extend the <em>Feature</em> class.</p>
<img src="http://feeds.feedburner.com/~r/TheProgrammerMan/~4/e2CA7G2nT-o" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://theprogrammerman.com/2010/02/22/its-all-about-dynamic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://theprogrammerman.com/2010/02/22/its-all-about-dynamic/</feedburner:origLink></item>
		<item>
		<title>Visitor pattern in Java</title>
		<link>http://feedproxy.google.com/~r/TheProgrammerMan/~3/73XfnAtq8nc/</link>
		<comments>http://theprogrammerman.com/2010/02/14/visitor-pattern-in-java/#comments</comments>
		<pubDate>Sun, 14 Feb 2010 22:29:01 +0000</pubDate>
		<dc:creator>bibix</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[design pattern]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://theprogrammerman.com/?p=102</guid>
		<description><![CDATA[This is a really nice and simple pattern. Imagine having a collection of objects. Let&#8217;s have a class Feature with single method String getName(). Of course the class hierarchy grows in time and here come two additional features: BooleanFeature and StringFeature. They override the original getName() behaviour. And it looks like that: That&#8217;s a common [...]]]></description>
			<content:encoded><![CDATA[<p>This is a really nice and simple pattern. Imagine having a collection of objects. Let&#8217;s have a class <code>Feature</code> with single method <code>String getName()</code>. Of course the class hierarchy grows in time and here come two additional features: <code>BooleanFeature</code> and <code>StringFeature</code>. They override the original <code>getName()</code> behaviour.<br />
And it looks like that:</p>
<pre class="brush: java; title: ; notranslate">
class Feature {
    public String getName() {
        return &quot;Feature&quot;;
    }
}

class BooleanFeature extends Feature{

    @Override
    public String getName() {
        return &quot;BooleanFeature&quot;;
    }
}

class StringFeature extends Feature{
    @Override
    public String getName() {
        return &quot;StringFeature&quot;;
    }
}
</pre>
<p>That&#8217;s a common hierarchy. I&#8217;m sure you&#8217;ve seen bigger trees.</p>
<p>So, you get a list of Feature objects and you know that some of them are BooleanFeatures, others are StringFeatures. Something similar to:</p>
<pre class="brush: java; title: ; notranslate">
List&lt;Feature&gt; features = new ArrayList&lt;Feature&gt;() {{
    add(new Feature());
    add(new BooleanFeature());
    add(new StringFeature());
}};
</pre>
<p>Nothing fancy. Three elements, each of different type. Now, you&#8217;re to process each element in different way. It&#8217;s java, it can&#8217;t be difficult. The processing is the <code>printIt(...)</code> method, which prints the type it processes and the name returned by <code>getName()</code>.</p>
<pre class="brush: java; title: ; notranslate">
class Printer {
    public void printIt(Feature feature) {
        System.out.println(&quot;Printing Feature: &quot;+feature.getName());
    }

    public void printIt(BooleanFeature feature) {
        System.out.println(&quot;Printing BooleanFeature: &quot;+feature.getName());
    }

    public void printIt(StringFeature feature) {
        System.out.println(&quot;Printing StringFeature: &quot;+feature.getName());
    }
}

public class PrintNames {
    public static void main(String[] args) {
        List&lt;Feature&gt; features = new ArrayList&lt;Feature&gt;() {{
            add(new Feature());
            add(new BooleanFeature());
            add(new StringFeature());
        }};

        Printer printer = new Printer();
        for(Feature feature : features) {
            printer.printIt(feature);
        }
    }
}
</pre>
<p>Logic is simple, but does it produce what we expect? The expected output looks like this:</p>
<pre class="brush: plain; title: ; notranslate">
Printing Feature: Feature
Printing BooleanFeature: BooleanFeature
Printing StringFeature: StringFeature
</pre>
<p>and actual result:</p>
<pre class="brush: plain; title: ; notranslate">
Printing Feature: Feature
Printing Feature: BooleanFeature
Printing Feature: StringFeature
</pre>
<p>WHOAAA, the override worked fine, but what happened to the printed types? It looks like the <code><strong>printIt(Feature feature)</strong></code> has been called for each element. What about the other two methods?</p>
<p>If you have been playing java for some time, you know why that happened. Java resolves types at compile time. It means that it sees a list of <code>Feature</code> elements. During compilation, <code>Feature</code> is the only known type kept in the list, so the <code>printIt(Feature feature)</code> method is being used. Advantage of this is faster execution, as there are no type checks during runtime. Above you can see a disadvantage of compile-time type resolution.</p>
<p>Here starts usage of <code>instanceof</code> operator. It hides in many forms. You can create a single method composed of consecutive if-elses and mentioned instanceof checking or create some external class hierarchy that processes Feature in per-subclass manner (using instanceof <img src='http://theprogrammerman.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ), etc. </p>
<p>There&#8217;s one solution, which doesn&#8217;t introduce the usage of <code>instanceof</code> operator. It&#8217;s the Visitor Pattern. How does it work? Well, the common ancestor (<code>Feature</code> in this case) and all its children have to implement function <code>void accept(Visitor visitor></code>. The implementation is a one-liner: <code>visitor.visit(this)</code>. And it&#8217;s the same for each class. </p>
<pre class="brush: java; title: ; notranslate">
class Feature {
    public String getName() {
        return &quot;Feature&quot;;
    }

    public void accept(Visitor visitor) {
        visitor.visit(this);
    }
}

class BooleanFeature extends Feature{
    @Override
    public String getName() {
        return &quot;BooleanFeature&quot;;
    }

    @Override
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }
}

class StringFeature extends Feature {
    @Override
    public String getName() {
        return &quot;StringFeature&quot;;
    }

    @Override
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }
}
</pre>
<p>The <code>Visitor</code> interface has three <code>visit</code> methods &#8211; one for each type it can visit. </p>
<pre class="brush: java; title: ; notranslate">
interface Visitor {
    void visit(Feature feature);
    void visit(BooleanFeature feature);
    void visit(StringFeature feature);
}
</pre>
<p>All magic is behind this. The <code>accept(Visitor visitor)</code> implementation in each <code>...Feature</code> calls the <code>visitor.visit(this)</code> method. During compilation, the type of <em>this</em> reference is known in each element, so the compiler can link it to correct method in the <code>Visitor</code> class. </p>
<p>The <code>Printer</code> class just implements the <code>Visitor</code> interface. </p>
<pre class="brush: java; title: ; notranslate">
class Printer implements Visitor {
    public void visit(Feature feature) {
        System.out.println(&quot;Printing Feature: &quot; + feature.getName());
    }

    public void visit(BooleanFeature feature) {
        System.out.println(&quot;Printing BooleanFeature: &quot; + feature.getName());
    }

    public void visit(StringFeature feature) {
        System.out.println(&quot;Printing StringFeature: &quot; + feature.getName());
    }
}

public class PrintNames {
    public static void main(String[] args) {
        List&lt;Feature&gt; features = new ArrayList&lt;Feature&gt;() {{
            add(new Feature());
            add(new BooleanFeature());
            add(new StringFeature());
        }};

        Printer printer = new Printer();
        for(Feature feature : features) {
            feature.accept(printer);
        }
    }
}
</pre>
<p>And the output is as expected:</p>
<pre class="brush: plain; title: ; notranslate">
Printing Feature: Feature
Printing BooleanFeature: BooleanFeature
Printing StringFeature: StringFeature
</pre>
<p>What&#8217;s worth noticing is that in opposite to previous calls, now the <code>feature</code> <em>accept</em>s the <code>printer</code> object. </p>
<p>Thanks to the Visitor Pattern, you can extend your classes with additional features. The main disadvantage is that any changes in class hierarchy (eg. introducing a class <code>IntegerFeature</code>), result in adding new methods to <code>Visitor</code> interface and all classes that implement it. But you can always create a default/abstract implementation: <code>DefaultVisitor</code> that will be a father for all implementations. </p>
<p>Think of it next time you start processing a collection using the <code>instanceof</code> checking.</p>
<img src="http://feeds.feedburner.com/~r/TheProgrammerMan/~4/73XfnAtq8nc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://theprogrammerman.com/2010/02/14/visitor-pattern-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://theprogrammerman.com/2010/02/14/visitor-pattern-in-java/</feedburner:origLink></item>
		<item>
		<title>Speed up development in groovy</title>
		<link>http://feedproxy.google.com/~r/TheProgrammerMan/~3/s2ben_kn8lI/</link>
		<comments>http://theprogrammerman.com/2010/01/17/speed-up-development-in-groovy/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 09:46:33 +0000</pubDate>
		<dc:creator>bibix</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[groovy]]></category>

		<guid isPermaLink="false">http://theprogrammerman.com/?p=95</guid>
		<description><![CDATA[To take full advantage of groovy it&#8217;s good to know additional methods extending standard java objects. I often visit groovy jdk, to see what actually is new in groovy. Operations on collections really speed up the work. In jdk you can see what operators are available for particular objects. For reference of methods used to [...]]]></description>
			<content:encoded><![CDATA[<p>To take full advantage of groovy it&#8217;s good to know additional methods extending standard java objects. I often visit <a href="http://groovy.codehaus.org/groovy-jdk/">groovy jdk</a>, to see what actually is new in groovy.</p>
<p>Operations on collections really speed up the work. In jdk you can see what operators are available for particular objects. For reference of methods used to override operators go here: <a href="http://groovy.codehaus.org/groovy-jdk/">http://groovy.codehaus.org/Operator+Overloading</a>. </p>
<img src="http://feeds.feedburner.com/~r/TheProgrammerMan/~4/s2ben_kn8lI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://theprogrammerman.com/2010/01/17/speed-up-development-in-groovy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://theprogrammerman.com/2010/01/17/speed-up-development-in-groovy/</feedburner:origLink></item>
		<item>
		<title>Intellij Language Injection</title>
		<link>http://feedproxy.google.com/~r/TheProgrammerMan/~3/4Xrgtc9oF5w/</link>
		<comments>http://theprogrammerman.com/2009/12/14/intellij-language-injection/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 22:20:52 +0000</pubDate>
		<dc:creator>bibix</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[intellij]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://theprogrammerman.com/?p=91</guid>
		<description><![CDATA[Pressing Alt+Enter in Intellij in random places is sometimes beneficial. Today I&#8217;ve found a feature which has been built in Intellij since version 6. So, Alt+Enter on String and I could inject a language&#8230; Nothing popped up in my mind. inject&#8230; language&#8230; a list with languages is displayed&#8230; chose Java and I got Java validation [...]]]></description>
			<content:encoded><![CDATA[<p>Pressing Alt+Enter in Intellij in random places is sometimes beneficial. Today I&#8217;ve found a feature which has been built in Intellij since version 6.<br />
So, <strong>Alt</strong>+<strong>Enter</strong> on <code>String</code> and I could inject a language&#8230; Nothing popped up in my mind.<br />
inject&#8230; language&#8230; a list with languages is displayed&#8230; chose Java and I got Java validation in String literal.</p>
<p>That&#8217;s good. You can read more about it at <a href="http://www.jetbrains.net/confluence/display/CONTEST/IntelliLang">JetBrains Confluence site</a>.</p>
<img src="http://feeds.feedburner.com/~r/TheProgrammerMan/~4/4Xrgtc9oF5w" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://theprogrammerman.com/2009/12/14/intellij-language-injection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://theprogrammerman.com/2009/12/14/intellij-language-injection/</feedburner:origLink></item>
		<item>
		<title>How to compile &amp; run JavaFX using Intellij</title>
		<link>http://feedproxy.google.com/~r/TheProgrammerMan/~3/sz6GK0TXm6g/</link>
		<comments>http://theprogrammerman.com/2009/11/30/how-to-compile-run-javafx-using-intellij/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 20:04:15 +0000</pubDate>
		<dc:creator>bibix</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[intellij]]></category>
		<category><![CDATA[JavaFX]]></category>

		<guid isPermaLink="false">http://theprogrammerman.com/?p=70</guid>
		<description><![CDATA[As recently I use Intellij most of the time due to fabulous Groovy support, I would like to use it also to play with JavaFX. There&#8217;s no planned support yet, so I played around with javafxc and javafx tools. In Intellij there&#8217;s only support for syntax highlighting defined in Settings-&#62;File Types. There are two ways [...]]]></description>
			<content:encoded><![CDATA[<p>As recently I use Intellij most of the time due to fabulous Groovy support, I would like to use it also to play with JavaFX. There&#8217;s no planned support yet, so I played around with <code>javafxc</code> and <code>javafx</code> tools.</p>
<p>In Intellij there&#8217;s only support for syntax highlighting defined in Settings-&gt;File Types. There are two ways you can compile your files. First one is using <em>External Tools</em>. It&#8217;s good if you would like to compile only one file at a time. The second solution is creating an ant script.</p>
<p>I think that better solution is using ant build file to compile all *.fx files in your module. I used build file posted at <a href="http://stackoverflow.com/questions/499433/build-javafx-project-without-netbeans/505708#505708">stackoverflow.com</a>:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;?xml version=&quot;1.1&quot;?&gt;

&lt;project name=&quot;JavaFx&quot; default=&quot;compile&quot; basedir=&quot;.&quot;&gt;
    &lt;property environment=&quot;env&quot;/&gt;
    &lt;property name=&quot;java.home&quot; value=&quot;${env.JAVA_HOME}&quot;/&gt;
    &lt;property name=&quot;jfx.home&quot; value=&quot;${JAVAFX_HOME}&quot;/&gt;
    &lt;path id=&quot;compile.classpath&quot;&gt;
        &lt;fileset dir=&quot;${java.home}/lib&quot;&gt;
            &lt;include name=&quot;**/*.jar&quot;/&gt;
        &lt;/fileset&gt;
        &lt;fileset dir=&quot;${jfx.home}/lib&quot;&gt;
            &lt;include name=&quot;**/*.jar&quot;/&gt;
        &lt;/fileset&gt;
    &lt;/path&gt;

    &lt;taskdef classname=&quot;com.sun.tools.javafx.ant.JavaFxAntTask&quot; name=&quot;javafxc&quot;&gt;
        &lt;classpath refid=&quot;compile.classpath&quot;/&gt;
    &lt;/taskdef&gt;

    &lt;target name=&quot;compile&quot;&gt;
        &lt;javafxc srcdir=&quot;${src}&quot; destdir=&quot;${out}&quot; includes=&quot;**/*.fx&quot; executable=&quot;${jfx.home}/bin/javafxc.exe&quot;&gt;
            &lt;classpath refid=&quot;compile.classpath&quot;/&gt;
        &lt;/javafxc&gt;
    &lt;/target&gt;
&lt;/project&gt;
</pre>
<p>Works perfectly. </p>
<p>Running is also simple. You need to create new External Tool for <code>javafx.exe</code>.<br />
Go to Settings->External Tools and press Add button. After filling <em>Name</em> and <em>Group</em> there are three more fields needed:</p>
<ul>
<li><strong>Program</strong>:
<pre class="brush: plain; light: true; title: ; notranslate">path to your javafx.exe</pre>
</li>
<li><strong>Parameters</strong>:
<pre class="brush: plain; light: true; title: ; notranslate"> -cp &quot;$Classpath$&quot; $FilePackage$.$FileNameWithoutExtension$</pre>
</li>
<li><strong>Working directory</strong>:
<pre class="brush: plain; light: true; title: ; notranslate">$OutputPath$</pre>
</li>
</ul>
<p>The main disadvantage is that your fx file has to be placed in some package, but using packages is encouraged anyway, so it shouldn&#8217;t be a big issue.</p>
<p>You can run now your compiled fx class by right clicking on it and selecting external tool you just added.</p>
<img src="http://feeds.feedburner.com/~r/TheProgrammerMan/~4/sz6GK0TXm6g" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://theprogrammerman.com/2009/11/30/how-to-compile-run-javafx-using-intellij/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://theprogrammerman.com/2009/11/30/how-to-compile-run-javafx-using-intellij/</feedburner:origLink></item>
		<item>
		<title>Using the syntaxhighlighter with the Pyrmont theme</title>
		<link>http://feedproxy.google.com/~r/TheProgrammerMan/~3/vZNmu5Ta4iY/</link>
		<comments>http://theprogrammerman.com/2009/11/15/using-syntaxhighlighter-with-pyrmont-theme/#comments</comments>
		<pubDate>Sun, 15 Nov 2009 19:21:45 +0000</pubDate>
		<dc:creator>bibix</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://theprogrammerman.com/?p=53</guid>
		<description><![CDATA[I had a problem with displaying correctly formatted code with this theme. The syntaxhighlighter decorates each token with &#60;code&#62; tag. It works fine unless something else defines style for code also. The theme does that. As the plugin is my primary formatter for the code, I just removed the style for the code tag from [...]]]></description>
			<content:encoded><![CDATA[<p>I had a problem with displaying correctly formatted code with this theme. The <a href="http://alexgorbatchev.com/wiki/SyntaxHighlighter">syntaxhighlighter </a>decorates each token with &lt;code&gt; tag. It works fine unless something else defines style for code also. The theme does that. As the plugin is my primary formatter for the code, I just removed the style for the <em>code</em> tag from <em>style.css</em> file in <em>pyrmont-v2</em> directory.<br />
The removed code looks like that now:</p>
<pre class="brush: css; title: ; notranslate">
 /*div#main div.post div.entry code{*/
 /*font-family: &quot;Courier New&quot;, mono;*/
 /*background-color: #181818;*/
 /*border-left: 1px solid #2a2e2f;*/
 /*color: #197b30;*/
 /*padding: 5px 10px;*/
 /*display: block;*/
 /*}*/
</pre>
<p>Now any wp plugin based on syntaxhighlighter should work fine.</p>
<img src="http://feeds.feedburner.com/~r/TheProgrammerMan/~4/vZNmu5Ta4iY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://theprogrammerman.com/2009/11/15/using-syntaxhighlighter-with-pyrmont-theme/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://theprogrammerman.com/2009/11/15/using-syntaxhighlighter-with-pyrmont-theme/</feedburner:origLink></item>
	</channel>
</rss>

