<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6252922358877820177</id><updated>2011-10-03T15:43:26.881+02:00</updated><category term='dependency injection'/><category term='idea'/><category term='scala'/><category term='jdbc'/><category term='milestone'/><category term='analyzer'/><category term='qi4j'/><category term='java'/><category term='heap'/><category term='patterns'/><category term='development'/><category term='nullobject'/><category term='strategy'/><category term='maven'/><category term='mat'/><category term='memory'/><category term='concurrency'/><category term='book'/><category term='exceptions'/><category term='techtip'/><category term='sql'/><category term='spring'/><category term='parallel'/><category term='eclipse'/><category term='collections'/><category term='ubuntu'/><category term='architecture'/><category term='plugins'/><category term='subversion'/><category term='google'/><title type='text'>On the way to better software</title><subtitle type='html'>My notes about software development</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://www.krzysztofbialek.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default'/><link rel='alternate' type='text/html' href='http://www.krzysztofbialek.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Krzysztof Białek</name><uri>http://www.blogger.com/profile/00013320014264918228</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://2.bp.blogspot.com/-m7eO378Pvbk/TV7eBsTjBuI/AAAAAAAABY8/gS1BgZctvJM/s220/kb.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>20</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6252922358877820177.post-1738047519690109951</id><published>2011-02-24T22:26:00.004+01:00</published><updated>2011-02-27T16:17:56.497+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='collections'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Scala collections: Filtering each n-th element - Part 2</title><content type='html'>If you read my previous &lt;a href="/2011/02/scala-collections-filtering-each-n-th.html"&gt;post&lt;/a&gt; you know that I'm trying to find the best implementation for filtering each n-th element from a collection. Since then I found two more possible implementations:
&lt;ol&gt;
&lt;li&gt;Loop - Reverse&lt;br/&gt;
This implementation is faster than other loop-based implementations. It benefits from replacing append :+ with concatenation :: 
&lt;pre class="brush: scala"&gt;
def filterUsingLoopReversed[A](in: Iterable[A], step: Int, offset: Int): Iterable[A] = {
  var out = List.empty[A]
  val it = in.iterator
  for (i &lt;- 0 to in.size - 1) {
    if (i % step == offset) out = it.next :: out
    else it.next
  }
  out.reverse
}
&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;Range - Map&lt;br/&gt;
This is simply the best implementation I have. It's not only concise but also very fast. The drawback is that it requires the input collection to be an instance of Seq
&lt;pre class="brush: scala"&gt;
def filterUsingMap[A](in: Iterable[A], step: Int, offset: Int): Iterable[A] = {
  val seq = in.toSeq
  Range(offset, in.size, step).map(seq(_))
}
&lt;/pre&gt;
&lt;b&gt;Edit:&lt;/b&gt; Peter suggested that the perfomance of this solution depends on the implementation of the input collection. To mitigate this I've decided to convert input collection to IndexedSeq which is supposed to guarantee &lt;a href="http://www.scala-lang.org/api/current/scala/collection/immutable/IndexedSeq.html"&gt;near constant-time element access&lt;/a&gt;. The performance is a bit worse because of the extra conversion time, though it's still the fastest solution I found
&lt;pre class="brush: scala"&gt;
def filterUsingMap[A](in: Iterable[A], step: Int, offset: Int): Iterable[A] = {
  val seq = in.toIndexedSeq
  Range(offset, in.size, step).map(seq(_))
}
&lt;/pre&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;br/&gt;
Benchmark results:
&lt;br/&gt;Average execution time of 100 executions
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-PODOle23Zlg/TWbHdga1E9I/AAAAAAAABco/P4tnSAkk4eQ/s1600/filtering-nth-element-performance2.png" imageanchor="1" style="margin-left:1em; margin-right:1em"&gt;&lt;img border="0" height="337" width="400" src="http://4.bp.blogspot.com/-PODOle23Zlg/TWbHdga1E9I/AAAAAAAABco/P4tnSAkk4eQ/s400/filtering-nth-element-performance2.png" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br/&gt;
Range-Map (light green) is comparable to Loop-Buffered (dark green) when filtering small-mid collection (&lt;10000 elements). And there is no better choice than Range-Map to filter big collections (&gt;10000 elements).&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6252922358877820177-1738047519690109951?l=www.krzysztofbialek.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.krzysztofbialek.com/feeds/1738047519690109951/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6252922358877820177&amp;postID=1738047519690109951' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/1738047519690109951'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/1738047519690109951'/><link rel='alternate' type='text/html' href='http://www.krzysztofbialek.com/2011/02/scala-collections-filtering-each-n-th_24.html' title='Scala collections: Filtering each n-th element - Part 2'/><author><name>Krzysztof Białek</name><uri>http://www.blogger.com/profile/00013320014264918228</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://2.bp.blogspot.com/-m7eO378Pvbk/TV7eBsTjBuI/AAAAAAAABY8/gS1BgZctvJM/s220/kb.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/-PODOle23Zlg/TWbHdga1E9I/AAAAAAAABco/P4tnSAkk4eQ/s72-c/filtering-nth-element-performance2.png' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6252922358877820177.post-7840865291529952480</id><published>2011-02-19T00:13:00.016+01:00</published><updated>2011-02-23T23:02:45.617+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='collections'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Scala collections: Filtering each n-th element</title><content type='html'>Recently I had to process huge log files. I found it to be a great opportunity to check my Scala language skills. One of the problems I had to figure out was filtering every n-th element from iterable collection. This is supposed to be a trivial task, but well, after thinking for a while I had five competitive implementations ready. Which one should I pick? Perhaps the most concise one. To make the choice easier I've decided to measure how performant they are.
&lt;br /&gt;
Filtering implementations:
&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;Filter using groups
This is my favorite one, the cleanest implementation.
&lt;pre class="brush: scala" style="white-space: normal;"&gt;def filterUsingGroups[A](in: Iterable[A], step: Int, offset: Int): Iterable[A] =
  in.drop(offset).grouped(step).map(_.head).toIterable&lt;/pre&gt;
How it works:
&lt;pre&gt;
in = (1,2,3,4,5,6,7,8,9,10), step=3, offset=2
drop(2) =&amp;gt; (3,4,5,6,7,8,9,10)
grouped(3) =&amp;gt; ((3,4,5),(6,7,8),(9,10))
map(_.head) =&amp;gt; (3,6,9)
&lt;/pre&gt;
&lt;/li&gt;

&lt;li&gt;Filter using index
&lt;pre class="brush: scala" style="white-space: normal;"&gt;def filterUsingIndex[A](in: Iterable[A], step: Int, offset: Int): Iterable[A] =
  in.zipWithIndex.filter(_._2 % step == offset).unzip._1
&lt;/pre&gt;
How it works:
&lt;pre&gt;
in = (1,2,3,4,5,6,7,8,9,10), step=3, offset=2
zipWithIndex =&amp;gt; ((1,0),(2,1),....,(10,9))
filter(_._2 % 3 == 2) =&amp;gt; ((3,2), (6,5), (9,8))
unzip._1 =&amp;gt; (3,6,9)
&lt;/pre&gt;
&lt;/li&gt;

&lt;li&gt;Filter using recursion
&lt;pre class="brush: scala" style="white-space: normal;"&gt;def filterUsingRecursion[A](in: Iterable[A], step: Int, offset: Int): Iterable[A] = {
  if (in.size &amp;gt;= step) {
    val (l, r) = in.splitAt(step)
    List(l.drop(offset).head) ++ filterUsingRecursion(r, step, offset)
  } else List.empty[A]
}
&lt;/pre&gt;
How it works:
&lt;pre&gt;
in = (1,2,3,4,5,6,7,8,9,10), step=3, offset=2
splitAt(3) =&amp;gt; l=(1,2,3); r=(4,5,6,7,8,9,10)
l.drop(2).head == 3 =&amp;gt; List(3) ++ filterUsingRecursion((4,5,6,7,8,9,10), 3, 2)
&lt;/pre&gt;
&lt;/li&gt;

&lt;li&gt;Filter using loop (unbuffered)
&lt;pre class="brush: scala" style="white-space: normal;"&gt;def filterUsingLoop[A](in: Iterable[A], step: Int, offset: Int): Iterable[A] = {
  var out = List.empty[A]
  val it = in.iterator
  for (i &amp;lt;- 0 to in.size - 1) {
    if (i % step == offset) out = out :+ it.next
    else it.next
  }
  out
}
&lt;/pre&gt;
&lt;/li&gt;

&lt;li&gt;Filter using loop (buffered)
&lt;pre class="brush: scala" style="white-space: normal;"&gt;def filterUsingLoopWithBuffer[A](in: Iterable[A], step: Int, offset: Int): Iterable[A] = {
  val out = collection.mutable.ListBuffer.empty[A]
  val it = in.iterator
  for (i &amp;lt;- 0 to in.size - 1) {
    if (i % step == offset) out += it.next
    else it.next
  }
  out
}
&lt;/pre&gt;
&lt;/li&gt;

&lt;li&gt;Filter using tail recursion (added later)
&lt;pre class="brush: scala" style="white-space: normal;"&gt;def filterUsingTailRecursion[A](in: Iterable[A], step: Int, offset: Int): Iterable[A] = {
  def filter(acc: ListBuffer[A], in: Iterable[A]): ListBuffer[A] = {
    in.splitAt(step) match {
      case (l, r) if (l.size &amp;gt;= offset) =&amp;gt; filter(acc += l.drop(offset).head, r)
      case _ =&amp;gt; acc
    }
  }
  filter(ListBuffer.empty[A], in)
}
&lt;/pre&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
In my benchmark I've called each method 10 times in a row and measured average execution time (using parameters step=3, offset=2).  When benchmarking I call foreach on the result of each execution of the method under test. I do this to materialize returned collection if it would be backed by an iterator. 
&lt;/p&gt;
&lt;pre class="brush: scala" style="white-space: normal;"&gt;def benchmark[A](fn: =&amp;gt; Iterable[A]): Double = {
  fn // warm-up
  val times = 10
  val start = System.currentTimeMillis
  for (i &amp;lt;- 1 to times) fn.foreach(_ =&amp;gt; ())
  val stop = System.currentTimeMillis
  (stop - start).toDouble / times
}
&lt;/pre&gt;
The chart below shows the results I've collected. &lt;a href="http://2.bp.blogspot.com/-hdvVCquNvYs/TWLk-qr7VJI/AAAAAAAABaM/m_vn89j8oyU/s1600/filtering-nth-element-performance.png" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"&gt;&lt;img alt="" border="0" id="BLOGGER_PHOTO_ID_5576271053899650194" src="http://2.bp.blogspot.com/-hdvVCquNvYs/TWLk-qr7VJI/AAAAAAAABaM/m_vn89j8oyU/s400/filtering-nth-element-performance.png" style="cursor: pointer; display: block; height: 339px; margin: 0px auto 10px; text-align: center; width: 400px;" /&gt;&lt;/a&gt;Conclusions: 
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Buffered loop is the fastest implementation&lt;/li&gt;
&lt;li&gt;Index is also quite good&lt;/li&gt;
&lt;li&gt;Recursion is working pretty well with small collections but fails to process huge collections (java.lang.StackOverflowError)&lt;/li&gt;
&lt;li&gt;Unbuffered loop fails to process huge collections because of intesive data copying&lt;/li&gt;
&lt;li&gt;Groups give worst performance&lt;/li&gt;
&lt;li&gt;Tail recursion works very well with huge collections&lt;/li&gt;
&lt;/ul&gt;
Sadly the implementation using groups is not the most performant although it's clean and concise. Anyway it's good enough when processing less than 1000 elements. That makes it quite good candidate for initial implementation.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6252922358877820177-7840865291529952480?l=www.krzysztofbialek.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.krzysztofbialek.com/feeds/7840865291529952480/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6252922358877820177&amp;postID=7840865291529952480' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/7840865291529952480'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/7840865291529952480'/><link rel='alternate' type='text/html' href='http://www.krzysztofbialek.com/2011/02/scala-collections-filtering-each-n-th.html' title='Scala collections: Filtering each n-th element'/><author><name>Krzysztof Białek</name><uri>http://www.blogger.com/profile/00013320014264918228</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://2.bp.blogspot.com/-m7eO378Pvbk/TV7eBsTjBuI/AAAAAAAABY8/gS1BgZctvJM/s220/kb.jpg'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-hdvVCquNvYs/TWLk-qr7VJI/AAAAAAAABaM/m_vn89j8oyU/s72-c/filtering-nth-element-performance.png' height='72' width='72'/><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6252922358877820177.post-5955100165806474523</id><published>2010-07-15T22:10:00.005+02:00</published><updated>2010-07-15T22:21:04.643+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='idea'/><category scheme='http://www.blogger.com/atom/ns#' term='techtip'/><category scheme='http://www.blogger.com/atom/ns#' term='scala'/><title type='text'>Tech Tip: How to use fast scala compiler (fsc) from IntelliJ IDEA</title><content type='html'>&lt;div xmlns='http://www.w3.org/1999/xhtml'&gt;&lt;p&gt;Using fsc from IDEA is supposed to be trivial task. IDEA provides dedicated fsc "Run Configuration" out-of-the-box. Unfortunately in reality things are getting more complicated. Scala build in IDEA is hanging more or less often making it not usable.&lt;/p&gt;&lt;p&gt;It took me several hours to figure out what is going on there. I was trying with dozen of switches without success. Finally I've found this simple answer and here it is.&lt;/p&gt;&lt;p&gt;When compiler server is starting it opens random port to listen for commands from compiler clients. Obviously the clients must know that port number. To communicate that server is creating file named as the port number. The file is created under JVM temporary directory ${java.io.tmpdir}/scala-devel/ ... (/tmp/scala-devel/{username}/scalac-compile-server-port/ at my linux box). Problems begin if there are some old unused files left in that directory. So the solution is easy - delete ${java.io.tmpdir}/scala-devel/ before starting compiler server.&lt;/p&gt;I've created simple bash script to do that. I simply use it instead of starting fsc from IDEA.
&lt;script type="syntaxhighlighter" class="brush: bash"&gt;
#!/bin/bash

M2_REPO=~/.m2/repository
SCALA_VERSION=2.8.0

rm -rf /tmp/scala-devel

$JAVA_HOME/bin/java -Xmx512m -verbose \
-classpath $M2_REPO/org/scala-lang/scala-library/$SCALA_VERSION/scala-library-$SCALA_VERSION.jar:$M2_REPO/org/scala-lang/scala-compiler/$SCALA_VERSION/scala-compiler-$SCALA_VERSION.jar \
scala.tools.nsc.CompileServer
&lt;/script&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6252922358877820177-5955100165806474523?l=www.krzysztofbialek.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.krzysztofbialek.com/feeds/5955100165806474523/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6252922358877820177&amp;postID=5955100165806474523' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/5955100165806474523'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/5955100165806474523'/><link rel='alternate' type='text/html' href='http://www.krzysztofbialek.com/2010/07/tech-tip-how-to-use-fast-scala-compiler.html' title='Tech Tip: How to use fast scala compiler (fsc) from IntelliJ IDEA'/><author><name>Krzysztof Białek</name><uri>http://www.blogger.com/profile/00013320014264918228</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://2.bp.blogspot.com/-m7eO378Pvbk/TV7eBsTjBuI/AAAAAAAABY8/gS1BgZctvJM/s220/kb.jpg'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6252922358877820177.post-819624387958747198</id><published>2009-12-07T23:12:00.003+01:00</published><updated>2009-12-07T23:23:03.832+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='patterns'/><category scheme='http://www.blogger.com/atom/ns#' term='book'/><title type='text'>"Analysis Patterns 2 - Work in Progress"</title><content type='html'>I was about to buy &lt;a href="http://www.amazon.co.uk/Analysis-Patterns-Reusable-Object-Models/dp/0201895420/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1260224028&amp;sr=8-1"&gt;Analysis Patterns&lt;/a&gt; book by Martin Fowler but recently I found on his &lt;a href="http://martinfowler.com/ap2/index.html"&gt;web page&lt;/a&gt; that he's working on version two of this book, thus I've decided to wait a bit. He's also kindly publishing his work so waiting won't hurt so much.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6252922358877820177-819624387958747198?l=www.krzysztofbialek.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.krzysztofbialek.com/feeds/819624387958747198/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6252922358877820177&amp;postID=819624387958747198' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/819624387958747198'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/819624387958747198'/><link rel='alternate' type='text/html' href='http://www.krzysztofbialek.com/2009/12/analysis-patterns-2-work-in-progress.html' title='&quot;Analysis Patterns 2 - Work in Progress&quot;'/><author><name>Krzysztof Białek</name><uri>http://www.blogger.com/profile/00013320014264918228</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://2.bp.blogspot.com/-m7eO378Pvbk/TV7eBsTjBuI/AAAAAAAABY8/gS1BgZctvJM/s220/kb.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6252922358877820177.post-8666037618508364601</id><published>2009-12-07T21:36:00.006+01:00</published><updated>2009-12-08T11:01:03.000+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='qi4j'/><title type='text'>Why I won't use Qi4j (yet)</title><content type='html'>Several months ago I've published short series about Qi4j basics. I was impressed not only by the long list of interesting features, but also by the overall concept of the application architecture that Qi4j offers. Unfortunately I've recognized it's very difficult for me to write an application using Qi4j quickly and here is why.

1. Mixins.
When Mixins are very useful feature the way they are implemented in Qi4j doesn't work for me. I had to write a lot of nested classes and was loosing my focus because of this. I'm wondering what was theirs motivation to create this framework in Java not in Scala which has traits already built-in.

2. Lack of good relational DB support.
Another obstacle is that it's strongly focused on the object oriented data stores. Well, that's not bad actually, probably in the future we'll use them mostly. But today when at work I've to deal with relational databases this is serious disadvantage. Hiding these relational databases behind anti-corruption layers would work but it also makes usage of Qi4j's UnitOfWork impossible.

3. Testability.
Mocking interfaces with properties modeled with Property&amp;lt;T&amp;gt; needs endless effort. (Maybe I didn't find the right tool to do this)

4. Poor documentation.
Why the Spring framework is so popular? Because it's very well documented and its codebase is fairly readable - that's for sure one of the reasons. Nothing more to add here.

Anyway it's not as bad as it might look like. I appreciate Qi4j's developers for their innovating approach to software development and I'm tracking theirs mailing list carefully as there is a lot of interesting discussions I can learn from. Qi4j is definitely worth to give it a try also to feel the taste of &lt;a href="http://en.wikipedia.org/wiki/Behavior_Driven_Development"&gt;BDD&lt;/a&gt; for a while. It's amazing in how many different ways it's possible to write an application in Java.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6252922358877820177-8666037618508364601?l=www.krzysztofbialek.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.krzysztofbialek.com/feeds/8666037618508364601/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6252922358877820177&amp;postID=8666037618508364601' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/8666037618508364601'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/8666037618508364601'/><link rel='alternate' type='text/html' href='http://www.krzysztofbialek.com/2009/12/why-i-wont-use-qi4j-yet.html' title='Why I won&apos;t use Qi4j (yet)'/><author><name>Krzysztof Białek</name><uri>http://www.blogger.com/profile/00013320014264918228</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://2.bp.blogspot.com/-m7eO378Pvbk/TV7eBsTjBuI/AAAAAAAABY8/gS1BgZctvJM/s220/kb.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6252922358877820177.post-7921264336084820571</id><published>2009-10-23T09:55:00.003+02:00</published><updated>2009-10-23T10:27:48.920+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='collections'/><category scheme='http://www.blogger.com/atom/ns#' term='google'/><title type='text'>Google collections</title><content type='html'>&lt;a href="http://code.google.com/p/google-collections/"&gt;Google collections&lt;/a&gt; is very interesting, modern library which supplements standard library java.util.collections. It focuses on code simplicity and safety (immutability). And it brings some flavours of functional programming into Java land what I hope will help the developers evolve into more functional languages like Scala smoothly.
Here is a nice 10 minutes long &lt;a href="http://blog.jayway.com/2009/10/22/google-collections/"&gt;introduction&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6252922358877820177-7921264336084820571?l=www.krzysztofbialek.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.krzysztofbialek.com/feeds/7921264336084820571/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6252922358877820177&amp;postID=7921264336084820571' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/7921264336084820571'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/7921264336084820571'/><link rel='alternate' type='text/html' href='http://www.krzysztofbialek.com/2009/10/google-collections.html' title='Google collections'/><author><name>Krzysztof Białek</name><uri>http://www.blogger.com/profile/00013320014264918228</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://2.bp.blogspot.com/-m7eO378Pvbk/TV7eBsTjBuI/AAAAAAAABY8/gS1BgZctvJM/s220/kb.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6252922358877820177.post-3120463373708395741</id><published>2009-09-30T15:06:00.011+02:00</published><updated>2009-10-02T00:09:14.081+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='qi4j'/><title type='text'>Qi4j - First mixin</title><content type='html'>&lt;img style="float:left; margin:0 10px 10px 0;width: 102px; height: 130px; border: 0px;" src="http://www.qi4j.org/images/18.372fc8a6114d29eab35800023/logo-box.jpg" alt="Qi4j Logo" /&gt;This is my third post about Qi4j basics. In this post I'll show you how to implement behavior and wire it together with the code I already wrote for the previous post.
As you probably remember Qi4j manages composites. Composite is defined as a composition of fragments. Fragment is a piece of code responsible for concrete functionality. Each fragment can be categorized either as a mixin or modifier. Mixin's role is to provide an implementation for composite's methods. At the moment Rational composite has only properties. So let's add some behavior methods. (Mixins are not limited only to provide behavior implementation, in fact properties are managed by predefined mixin too)
&lt;span style="font-weight:bold;"&gt;Step 1. Extend Rational interface.&lt;/span&gt;
&lt;pre class="brush: java; gutter: true; highlight:[10,12]"&gt;
import org.qi4j.api.property.Property;
import org.qi4j.api.value.ValueComposite;

public interface Rational extends ValueComposite {

    Property&amp;lt;Integer&amp;gt; nominator();

    Property&amp;lt;Integer&amp;gt; denominator();

    Rational add(Rational toAdd);

    Rational mul(Rational toMultiply);
}
&lt;/pre&gt;The highlighted lines show new behavior methods. If you try to run the application you will see the exception with the following message:&lt;div class="cli" style="overflow: auto;"&gt;No implementation found for method public abstract com.blogspot.javasnippet.qi4j.Rational com.blogspot.javasnippet.qi4j.Rational.add(com.blogspot.javasnippet.qi4j.Rational) in com.blogspot.javasnippet.qi4j.Rational&lt;/div&gt;
That's because Qi4j doesn't see any implementation for these new methods.
&lt;span style="font-weight:bold;"&gt;Step 2. Implement mixin.&lt;/span&gt;
&lt;pre class="brush: java; gutter: true;"&gt;
public interface Rational extends ValueComposite {

    Property&amp;lt;Integer&amp;gt; nominator();

    Property&amp;lt;Integer&amp;gt; denominator();

    Rational add(Rational toAdd);

    Rational mul(Rational toMultiply);

    abstract class RationalMixin implements Rational {

        @Override
        public Rational add(Rational toAdd) {
            return null;
        }

        @Override
        public Rational mul(Rational toMultiply) {
            return null;
        }
    }
}
&lt;/pre&gt;
Things worth to be noted:
&lt;ul&gt;&lt;li&gt;RationalMixin class is defined inside Rational interface - although it's not mandatory and is done here for simplicity there are cases (mainly for modifiers) where it makes sense to put implementation directly into declaring interface.&lt;/li&gt;&lt;li&gt;RationalMixin class is abstract - as I want to implement only behavior methods I have to make this class abstract. This way it's possible to even split single interface implementation into set of mixins. Qi4j proxies and reflection does the rest.&lt;/li&gt;&lt;/ul&gt;Qi4j still is not aware of this stub implementation and crashes by throwing this same exception as before.
&lt;span style="font-weight:bold;"&gt;Step 3. Register mixin.&lt;/span&gt;
To register mixin I have to annotate interface Rational with @Mixins annotation like below.&lt;pre class="brush: java; gutter: true; highlight: 1;"&gt;
@Mixins(Rational.RationalMixin.class)
public interface Rational extends ValueComposite {
...
}
&lt;/pre&gt;Now the application is running fine - Qi4j sees implementation for all declared methods.
&lt;span style="font-weight:bold;"&gt;Step 4. Accessing composite state.&lt;/span&gt;
&lt;span style="font-style:italic;"&gt;UPDATE: It's no longer necessary to inject state to the mixin. See p.4.1&lt;/span&gt; 
Rational extends ValueComposite which in turn extends Composite interface. If you look at source code of Composite interface then you will see it declares usage of PropertyMixin - that's the one which is responsible for properties state management. So our Rational is composed out of two fragments: PropertyMixin and RationalMixin. To implement methods inside the RationalMixin we need to access the state of this Rational composite instance. Qi4j solves this by dependency injection. See highlighted lines below.
&lt;pre class="brush: java; gutter: true; highlight: [3,4];"&gt;
    abstract class RationalMixin implements Rational {

        @This
        private Rational state;

        @Override
        public Rational add(Rational toAdd) {
            return null;
        }

        @Override
        public Rational mul(Rational toMultiply) {
            return null;
        }
    }
&lt;/pre&gt;According to the JavaDoc, annotation @This &lt;span style="font-style:italic;"&gt;"denotes the injection of a reference to the same Composite as the fragment is a part of".&lt;/span&gt; Then the method add() can be implemented as:&lt;pre class="brush: java; gutter: true;"&gt;
        public Rational add(Rational toAdd) {
            int n1 = state.nominator().get();
            int d1 = state.denominator().get();
            int n2 = toAdd.nominator().get();
            int d2 = toAdd.denominator().get();

            int d = d1 * d2;
            int n = n1 * d2 + n2 * d1;
            return null;
        }
&lt;/pre&gt;
&lt;span style="font-weight:bold;"&gt;Step 4.1. Accessing composite state without using @This.&lt;/span&gt;
I wasn't aware of this feature when I was writing this post. Here is an update.
Qi4j routes calls to the abstract methods (in other words - the methods which are not implemented by the mixin) to the composite. So the implementation of RationalMixin can be rewritten as follows:&lt;pre class="brush: java; gutter: true;"&gt;
    abstract class RationalMixin implements Rational {

        @Override
        public Rational add(Rational toAdd) {
            int n1 = this.nominator().get();
            int d1 = this.denominator().get();
            int n2 = toAdd.nominator().get();
            int d2 = toAdd.denominator().get();

            int d = d1 * d2;
            int n = n1 * d2 + n2 * d1;
            return null;
        }

        @Override
        public Rational mul(Rational toMultiply) {
            return null;
        }
    }
&lt;/pre&gt;
&lt;span style="font-weight:bold;"&gt;Step 5. Accessing module resources.&lt;/span&gt;
Now it's time to create new Rational instance inside RationalMixin. To do this we need reference to the ValueBuilderFactory. This is solved by the dependency injection as well. Qi4j defines annotation @Structure which &lt;span style="font-style:italic;"&gt;"denotes the injection of a resource specific for the module which the injected object/fragment is instantiated in".&lt;/span&gt;
&lt;pre class="brush: java; gutter: true; highlight: [3,4,16,17,18,19];"&gt;
    abstract class RationalMixin implements Rational {

        @Structure
        private ValueBuilderFactory builderFactory;

        @Override
        public Rational add(Rational toAdd) {
            int n1 = this.nominator().get();
            int d1 = this.denominator().get();
            int n2 = toAdd.nominator().get();
            int d2 = toAdd.denominator().get();

            int d = d1 * d2;
            int n = n1 * d2 + n2 * d1;

            ValueBuilder&amp;lt;Rational&amp;gt; builder = builderFactory.newValueBuilder(Rational.class);
            builder.prototype().nominator().set(n);
            builder.prototype().denominator().set(d);
            return builder.newInstance();
        }

        @Override
        public Rational mul(Rational toMultiply) {
            return null;
        }
    }
&lt;/pre&gt;
The implementation of method add() is now finished. We can check if it works. Here is new body of method main() which computes 2/3 + 3/4 and prints the result to the console.
&lt;pre class="brush: java; gutter: true;"&gt;
    public static void main(final String[] args) throws Exception {
        SingletonAssembler assembler = new SingletonAssembler() {

            @Override
            public void assemble(ModuleAssembly module) throws AssemblyException {
                module.addValues(Rational.class);
            }
        };

        ApplicationSPI application = assembler.application();

        Module module = assembler.module();
        ValueBuilder&amp;lt;Rational&amp;gt; rb;
        rb = module.valueBuilderFactory().newValueBuilder(Rational.class);
        rb.prototype().nominator().set(2);
        rb.prototype().denominator().set(3);
        Rational r1 = rb.newInstance();

        rb = module.valueBuilderFactory().newValueBuilder(Rational.class);
        rb.prototype().nominator().set(3);
        rb.prototype().denominator().set(4);
        Rational r2 = rb.newInstance();

        Rational r3 = r1.add(r2);

        System.out.println(r3.nominator().get() + "/" + r3.denominator().get());
    }
&lt;/pre&gt;
You should see:
&lt;div class="cli"&gt;17/12&lt;/div&gt;
&lt;span style="font-weight:bold;"&gt;Summary&lt;/span&gt;
This is last post of my short series about Qi4j basics. Here is short list of things worth to be remembered:
&lt;ul&gt;&lt;li&gt;Qi4j is composite oriented and manages composites rather than objects.&lt;/li&gt;&lt;li&gt;Qi4j aims to help us in easier DDD adoption.&lt;/li&gt;&lt;li&gt;Qi4j uses dependency injection.&lt;/li&gt;&lt;/ul&gt;Qi4j offers much more features than I know about (I'm still learning). If you want to learn more go to its &lt;a href="http://www.qi4j.org/"&gt;website&lt;/a&gt;. I encourage you to give it a try.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6252922358877820177-3120463373708395741?l=www.krzysztofbialek.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.krzysztofbialek.com/feeds/3120463373708395741/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6252922358877820177&amp;postID=3120463373708395741' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/3120463373708395741'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/3120463373708395741'/><link rel='alternate' type='text/html' href='http://www.krzysztofbialek.com/2009/09/qi4j-first-mixin.html' title='Qi4j - First mixin'/><author><name>Krzysztof Białek</name><uri>http://www.blogger.com/profile/00013320014264918228</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://2.bp.blogspot.com/-m7eO378Pvbk/TV7eBsTjBuI/AAAAAAAABY8/gS1BgZctvJM/s220/kb.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6252922358877820177.post-1834037872624089272</id><published>2009-09-23T22:24:00.008+02:00</published><updated>2009-10-01T08:07:40.519+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='qi4j'/><title type='text'>Qi4j - First application</title><content type='html'>&lt;img style="float:left; margin:0 10px 10px 0;width: 102px; height: 130px; border: 0px;" src="http://www.qi4j.org/images/18.372fc8a6114d29eab35800023/logo-box.jpg" alt="Qi4j Logo" /&gt;In the previous post I've shown how to set up Qi4j project with maven. In this post I'll show how to create application which will slowly evolve into basic rational number calculator in the future. I realize that using Qi4j in such simple application is an overkill but I think it's suitable for showing the basics. 
Qi4j derives many concepts from the famous Domain Driven Design. According to DDD principles an application should be comprised of layers. Each layer can have one or more modules. 
Qi4j uses assemblers to define application structure, layers, modules and dependencies between them. Assemblers are classes derived from interface org.qi4j.bootstrap.Assembler.
In this simple application we'll use SingletonAssembler which instantiates one layer with one module inside.
&lt;span style="font-weight:bold;"&gt;Step 1. Create application class.&lt;/span&gt;
Create new class SimpleQi4jApplication in the package com.blogspot.javasnippet.qi4j which is located under directory src/main/java.
&lt;pre class="brush: java"&gt;
package com.blogspot.javasnippet.qi4j;

public class SimpleQi4jApplication {

    public static void main(final String[] args) {


    }

}
&lt;/pre&gt;
&lt;span style="font-weight:bold;"&gt;Step 2. Add SingletonAssembler.&lt;/span&gt;
&lt;pre class="brush: java"&gt;
public class SimpleQi4jApplication {

    public static void main(final String[] args) {
        SingletonAssembler assembler = new SingletonAssembler() {

            @Override
            public void assemble(ModuleAssembly module) throws AssemblyException {

            }
        };

    }
}
&lt;/pre&gt;SingletonAssembler is an abstract class with abstract method assemble left to be implemented. I don't have anything to be assembled at the moment so I'm leaving it empty.

&lt;span style="font-weight:bold;"&gt;Step 3. Activate application.&lt;/span&gt;
Qi4j application has simple life-cycle. It can be activated and deactivated.
&lt;pre class="brush: java"&gt;
public class SimpleQi4jApplication {

    public static void main(final String[] args) throws Exception {
        SingletonAssembler assembler = new SingletonAssembler() {

            @Override
            public void assemble(ModuleAssembly module) throws AssemblyException {

            }
        };

        ApplicationSPI application = assembler.application();
    }
}
&lt;/pre&gt;
Now we can start this doing-nothing application. (I've added throws Exception to main() method for the sake of simplicity). We don't have to activate application explicitly because SingletonAssembler does this automatically.

&lt;span style="font-weight:bold;"&gt;Step 3. Create composite.&lt;/span&gt;
Let's model the rational number. Each rational number can be expressed as a ratio &lt;span style="font-style:italic;"&gt;nominator/denominator&lt;/span&gt;, where both nominator and denominator are integers.
Here is Rational interface created with Qi4j goodies.
&lt;pre class="brush: java"&gt;
import org.qi4j.api.property.Property;
import org.qi4j.api.value.ValueComposite;

public interface Rational extends ValueComposite {

    Property&amp;lt;Integer&amp;gt; nominator();

    Property&amp;lt;Integer&amp;gt; denominator();

}
&lt;/pre&gt;
Several new features come with this code snippet:&lt;ul&gt;&lt;li&gt;The properties are declared as methods with result type Property&amp;lt;Integer&amp;gt;. Qi4j deprecates classic POJO model and replaces it with this simple and powerful concept.&lt;/li&gt;
&lt;li&gt;Interface Rational extends ValueComposite - this declaration makes Rational a Qi4j composite. In general Qi4j favours composites over objects (in Java meaning) and everything it manages is rather a composite than an object. There are several types of composites supported like value, entity, service and transient. In this example I choose to use value composite - an equivalent of DDD value object.&lt;/li&gt;
&lt;li&gt;Interface Rational defines only properties - there is no behavior. One of the greatest features of Qi4j is its ability to manage composite state automatically (through generic mixins). That means we don't need to provide any implementation at the moment.&lt;/li&gt;&lt;/ul&gt;Now we can add Rational composite to the Qi4j runtime.
&lt;pre class="brush: java; gutter: true;"&gt;
    public static void main(final String[] args) throws Exception {
        SingletonAssembler assembler = new SingletonAssembler() {

            @Override
            public void assemble(ModuleAssembly module) throws AssemblyException {
                module.addValues(Rational.class);
            }
        };

        ApplicationSPI application = assembler.application();
        application.activate();

        Module module = assembler.module();
        ValueBuilder&amp;lt;Rational&amp;gt; rb;
        rb = module.valueBuilderFactory().newValueBuilder(Rational.class);
        rb.prototype().nominator().set(2);
        rb.prototype().denominator().set(3);
        Rational r1 = rb.newInstance();

        System.out.println(r1.nominator().get() + "/" + r1.denominator().get());
    }
&lt;/pre&gt;
This program prints:
&lt;div class="cli"&gt;2/3&lt;/div&gt;
A lot of code for such easy task, hm? But remember that's just an example :-).
There are several things worth to be noted:&lt;ul&gt;&lt;li&gt;No constructors - As we don't provide implementation for Rational we can't use constructor. With Qi4j we have to use builders to create new instances.&lt;/li&gt;&lt;li&gt;When rb.prototype is mutable the r1 is immutable - that's built-in property of value composites.&lt;/li&gt;&lt;/ul&gt;
In the next post I'll finally implement behavior - operations add and multiply.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6252922358877820177-1834037872624089272?l=www.krzysztofbialek.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.krzysztofbialek.com/feeds/1834037872624089272/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6252922358877820177&amp;postID=1834037872624089272' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/1834037872624089272'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/1834037872624089272'/><link rel='alternate' type='text/html' href='http://www.krzysztofbialek.com/2009/09/qi4j-first-application.html' title='Qi4j - First application'/><author><name>Krzysztof Białek</name><uri>http://www.blogger.com/profile/00013320014264918228</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://2.bp.blogspot.com/-m7eO378Pvbk/TV7eBsTjBuI/AAAAAAAABY8/gS1BgZctvJM/s220/kb.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6252922358877820177.post-7934927542429569745</id><published>2009-09-18T16:49:00.027+02:00</published><updated>2009-09-23T22:31:05.805+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='qi4j'/><category scheme='http://www.blogger.com/atom/ns#' term='maven'/><title type='text'>Qi4j - Project setup with Maven and Eclipse</title><content type='html'>&lt;img style="float:left; margin:0 10px 10px 0;width: 102px; height: 130px; border: 0px;" src="http://www.qi4j.org/images/18.372fc8a6114d29eab35800023/logo-box.jpg" alt="Qi4j Logo" /&gt;&lt;a href="http://www.qi4j.org/"&gt;Qi4j&lt;/a&gt; is an implementation of composite oriented programming paradigm written in pure Java. As I'm just taking my first steps with Qi4j I've decided to write down some notes about that. In this post I'll show you how to setup your first Qi4j project with maven and eclipse. So, let's go.
&lt;span style="font-weight:bold;"&gt;Step 1. Create project skeleton.&lt;/span&gt;
With maven's archetype plugin we can get the skeleton really quickly.
&lt;div class="cli"&gt;mvn archetype:generate&lt;/div&gt;
Use the default project archetype (number 15)
Maven asks for the groupId and artifactId. 
&lt;div class="cli"&gt;Define value for groupId: : com.blogspot.javasnippet.qi4j
Define value for artifactId: : qi4j-example&lt;/div&gt;
The skeleton has been created with the following pom.xml file
&lt;pre class="brush: xml"&gt;
&amp;lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&amp;gt;
    &amp;lt;modelVersion&amp;gt;4.0.0&amp;lt;/modelVersion&amp;gt;
    &amp;lt;groupId&amp;gt;com.blogspot.javasnippet.qi4j&amp;lt;/groupId&amp;gt;
    &amp;lt;artifactId&amp;gt;qi4j-example&amp;lt;/artifactId&amp;gt;
    &amp;lt;packaging&amp;gt;jar&amp;lt;/packaging&amp;gt;
    &amp;lt;version&amp;gt;1.0-SNAPSHOT&amp;lt;/version&amp;gt;
    &amp;lt;name&amp;gt;qi4j-example&amp;lt;/name&amp;gt;
    &amp;lt;url&amp;gt;http://maven.apache.org&amp;lt;/url&amp;gt;
    &amp;lt;dependencies&amp;gt;
        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;junit&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;junit&amp;lt;/artifactId&amp;gt;
            &amp;lt;version&amp;gt;3.8.1&amp;lt;/version&amp;gt;
            &amp;lt;scope&amp;gt;test&amp;lt;/scope&amp;gt;
        &amp;lt;/dependency&amp;gt;
    &amp;lt;/dependencies&amp;gt;
&amp;lt;/project&amp;gt;
&lt;/pre&gt;&lt;span style="font-weight:bold;"&gt;Step 2. Add Qi4j maven repository.&lt;/span&gt;
To download Qi4j artifacts we must configure appropriate repository in the pom.xml.
To do that add the following section to your pom.xml file
&lt;pre class="brush: xml"&gt;
&amp;lt;repositories&amp;gt;
    &amp;lt;repository&amp;gt;
       &amp;lt;id&amp;gt;ops4j-repository&amp;lt;/id&amp;gt;
       &amp;lt;url&amp;gt;http://repository.ops4j.org/maven2&amp;lt;/url&amp;gt;
    &amp;lt;/repository&amp;gt;
&amp;lt;/repositories&amp;gt;
&lt;/pre&gt;&lt;span style="font-weight:bold;"&gt;Step 3. Add Qi4j dependecies.&lt;/span&gt;
Qi4j is not a single heavyweight jar but rather a set of small libraries. Then we can take only these we really need.
&lt;pre class="brush: xml"&gt;
&amp;lt;dependencies&amp;gt;
    &amp;lt;dependency&amp;gt;
        &amp;lt;groupId&amp;gt;org.qi4j.core&amp;lt;/groupId&amp;gt;
        &amp;lt;artifactId&amp;gt;qi4j-core-api&amp;lt;/artifactId&amp;gt;
        &amp;lt;version&amp;gt;${version.qi4j-core}&amp;lt;/version&amp;gt;
    &amp;lt;/dependency&amp;gt;
    &amp;lt;dependency&amp;gt;
        &amp;lt;groupId&amp;gt;org.qi4j.core&amp;lt;/groupId&amp;gt;
        &amp;lt;artifactId&amp;gt;qi4j-core-spi&amp;lt;/artifactId&amp;gt;
        &amp;lt;version&amp;gt;${version.qi4j-core}&amp;lt;/version&amp;gt;
    &amp;lt;/dependency&amp;gt;
    &amp;lt;dependency&amp;gt;
        &amp;lt;groupId&amp;gt;org.qi4j.core&amp;lt;/groupId&amp;gt;
        &amp;lt;artifactId&amp;gt;qi4j-core-bootstrap&amp;lt;/artifactId&amp;gt;
        &amp;lt;version&amp;gt;${version.qi4j-core}&amp;lt;/version&amp;gt;
    &amp;lt;/dependency&amp;gt;
    &amp;lt;dependency&amp;gt;
        &amp;lt;groupId&amp;gt;org.qi4j.core&amp;lt;/groupId&amp;gt;
        &amp;lt;artifactId&amp;gt;qi4j-core-runtime&amp;lt;/artifactId&amp;gt;
        &amp;lt;version&amp;gt;${version.qi4j-core}&amp;lt;/version&amp;gt;
        &amp;lt;scope&amp;gt;runtime&amp;lt;/scope&amp;gt;
    &amp;lt;/dependency&amp;gt;
    ...
&lt;/pre&gt;As you can see the Qi4j version number is set as a property so we must define it (when I'm writing this the latest version is 1.0-RC1).
&lt;pre class="brush: xml"&gt;
&amp;lt;properties&amp;gt;
    &amp;lt;version.qi4j-core&amp;gt;1.0-RC1&amp;lt;/version.qi4j-core&amp;gt;
&amp;lt;/properties&amp;gt;
&lt;/pre&gt;Busy people can find final pom.xml below.
&lt;pre class="brush: xml; collapse: true"&gt;
&amp;lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&amp;gt;
    &amp;lt;modelVersion&amp;gt;4.0.0&amp;lt;/modelVersion&amp;gt;
    &amp;lt;groupId&amp;gt;com.blogspot.javasnippet.qi4j&amp;lt;/groupId&amp;gt;
    &amp;lt;artifactId&amp;gt;qi4j-example&amp;lt;/artifactId&amp;gt;
    &amp;lt;packaging&amp;gt;jar&amp;lt;/packaging&amp;gt;
    &amp;lt;version&amp;gt;1.0-SNAPSHOT&amp;lt;/version&amp;gt;
    &amp;lt;name&amp;gt;qi4j-example&amp;lt;/name&amp;gt;
    &amp;lt;url&amp;gt;http://maven.apache.org&amp;lt;/url&amp;gt;

    &amp;lt;properties&amp;gt;
        &amp;lt;version.qi4j-core&amp;gt;1.0-RC1&amp;lt;/version.qi4j-core&amp;gt;
    &amp;lt;/properties&amp;gt;

    &amp;lt;repositories&amp;gt;
        &amp;lt;repository&amp;gt;
            &amp;lt;id&amp;gt;ops4j-repository&amp;lt;/id&amp;gt;
            &amp;lt;url&amp;gt;http://repository.ops4j.org/maven2&amp;lt;/url&amp;gt;
        &amp;lt;/repository&amp;gt;
    &amp;lt;/repositories&amp;gt;

    &amp;lt;dependencies&amp;gt;
        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;org.qi4j.core&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;qi4j-core-api&amp;lt;/artifactId&amp;gt;
            &amp;lt;version&amp;gt;${version.qi4j-core}&amp;lt;/version&amp;gt;
        &amp;lt;/dependency&amp;gt;
        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;org.qi4j.core&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;qi4j-core-spi&amp;lt;/artifactId&amp;gt;
            &amp;lt;version&amp;gt;${version.qi4j-core}&amp;lt;/version&amp;gt;
        &amp;lt;/dependency&amp;gt;
        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;org.qi4j.core&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;qi4j-core-bootstrap&amp;lt;/artifactId&amp;gt;
            &amp;lt;version&amp;gt;${version.qi4j-core}&amp;lt;/version&amp;gt;
        &amp;lt;/dependency&amp;gt;
        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;org.qi4j.core&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;qi4j-core-runtime&amp;lt;/artifactId&amp;gt;
            &amp;lt;version&amp;gt;${version.qi4j-core}&amp;lt;/version&amp;gt;
            &amp;lt;scope&amp;gt;runtime&amp;lt;/scope&amp;gt;
        &amp;lt;/dependency&amp;gt;

        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;junit&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;junit&amp;lt;/artifactId&amp;gt;
            &amp;lt;version&amp;gt;3.8.1&amp;lt;/version&amp;gt;
            &amp;lt;scope&amp;gt;test&amp;lt;/scope&amp;gt;
        &amp;lt;/dependency&amp;gt;
    &amp;lt;/dependencies&amp;gt;
&amp;lt;/project&amp;gt;
&lt;/pre&gt;&lt;span style="font-weight:bold;"&gt;Step 4. Build the application.&lt;/span&gt;
Now it's time to download Qi4j dependencies and build the application for the first time.
&lt;div class="cli"&gt;mvn install&lt;/div&gt;
&lt;span style="font-weight:bold;"&gt;Step 5. Import project to the eclipse workspace.&lt;/span&gt;
As Eclipse is my favorite IDE I call maven to generate eclipse project files.
&lt;div class="cli"&gt;mvn eclipse:eclipse -DdownloadSources -DdownloadJavadocs&lt;/div&gt;
Start Eclipse and &lt;a href="http://docs.codehaus.org/display/GFS/howto+import+project+into+eclipse#howtoimportprojectintoeclipse-ImportintoEclipse"&gt;import&lt;/a&gt; your fresh project into your workspace.
Now you can remove generated files App.java and AppTest.java
That's it. In the next post I'll show you how to start up your first Qi4j application.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6252922358877820177-7934927542429569745?l=www.krzysztofbialek.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.krzysztofbialek.com/feeds/7934927542429569745/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6252922358877820177&amp;postID=7934927542429569745' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/7934927542429569745'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/7934927542429569745'/><link rel='alternate' type='text/html' href='http://www.krzysztofbialek.com/2009/09/how-to-setup-qi4j-project.html' title='Qi4j - Project setup with Maven and Eclipse'/><author><name>Krzysztof Białek</name><uri>http://www.blogger.com/profile/00013320014264918228</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://2.bp.blogspot.com/-m7eO378Pvbk/TV7eBsTjBuI/AAAAAAAABY8/gS1BgZctvJM/s220/kb.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6252922358877820177.post-1155981597402542443</id><published>2009-09-08T22:44:00.004+02:00</published><updated>2009-09-09T10:20:53.554+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><title type='text'>Collection Literals in Java 7</title><content type='html'>Today I read the final list of the &lt;a href="http://blogs.sun.com/darcy/entry/project_coin_final_five"&gt;Project Coins&lt;/a&gt; - the accepted list of Java language enhancement proposals for Java 7. The goal of one of them is to introduce &lt;a href="http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/001193.html"&gt;collection literals&lt;/a&gt; for List, Set and Map.
Probably it will be more convenient to build simple collections with them, but why to bind Java language (through JLS) with java.util.List and its friends? One may say that java.util.* is part of the JDK anyway.
For me it's not enough justification for that step which looks more like an attempt to patch Java deficiencies with more or less weak solution. Unfortunately, the backward compatibility requirement doesn't allow to take a better one. I would be happy if I never saw this in Java language.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6252922358877820177-1155981597402542443?l=www.krzysztofbialek.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.krzysztofbialek.com/feeds/1155981597402542443/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6252922358877820177&amp;postID=1155981597402542443' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/1155981597402542443'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/1155981597402542443'/><link rel='alternate' type='text/html' href='http://www.krzysztofbialek.com/2009/09/collection-literals-in-java-7.html' title='Collection Literals in Java 7'/><author><name>Krzysztof Białek</name><uri>http://www.blogger.com/profile/00013320014264918228</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://2.bp.blogspot.com/-m7eO378Pvbk/TV7eBsTjBuI/AAAAAAAABY8/gS1BgZctvJM/s220/kb.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6252922358877820177.post-4305381195899831080</id><published>2009-08-19T10:03:00.002+02:00</published><updated>2009-08-19T10:09:19.251+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='plugins'/><category scheme='http://www.blogger.com/atom/ns#' term='eclipse'/><title type='text'>Some useful eclipse plugins</title><content type='html'>I found the following eclipse plugins being very useful.

1. &lt;a href="http://andrei.gmxhome.de/anyedit/index.html"&gt;AnyEdit&lt;/a&gt; - "AnyEdit plugin adds several new tools to the context menu of text- based Eclipse editors, to output consoles, to Eclipse main menu and editor toolbar. AnyEdit contributes also Import/Export working sets wizards."

2. &lt;a href="http://andrei.gmxhome.de/skins/index.html"&gt;Extended VS Presentation&lt;/a&gt; - "Extended VS Presentation plugin contributes powerful editing sessions management and highly customisable and very useful Eclipse skin ("look and feel")."&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6252922358877820177-4305381195899831080?l=www.krzysztofbialek.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.krzysztofbialek.com/feeds/4305381195899831080/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6252922358877820177&amp;postID=4305381195899831080' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/4305381195899831080'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/4305381195899831080'/><link rel='alternate' type='text/html' href='http://www.krzysztofbialek.com/2009/08/some-useful-eclipse-plugins.html' title='Some useful eclipse plugins'/><author><name>Krzysztof Białek</name><uri>http://www.blogger.com/profile/00013320014264918228</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://2.bp.blogspot.com/-m7eO378Pvbk/TV7eBsTjBuI/AAAAAAAABY8/gS1BgZctvJM/s220/kb.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6252922358877820177.post-6890514210791168567</id><published>2009-06-30T19:46:00.011+02:00</published><updated>2009-07-01T08:12:18.804+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='dependency injection'/><category scheme='http://www.blogger.com/atom/ns#' term='spring'/><title type='text'>Dependency Injection bad practices</title><content type='html'>"If all you have is a hammer, everything looks like a nail"
 - Abraham Maslow

To make it clear - I don't claim that DI is evil. I think it's really useful tool. Just a tool like a hammer. Unfortunately very often I can see DI being misused. People tend to put DI in each piece of the application what is often either an overkill or at least unnecessary complication. 
One of the bad practices I've recognized is &lt;b&gt;programming in XML&lt;/b&gt;. That happens for example when (hierarchies of) objects (including abstract objects) are defined in Spring configuration files and then used as injected prototypes in Java code which is coupled to their properties. Simple and clear solution is to introduce factory class to create objects directly in Java code.

That's rather rare case but don't worry there are also other more spectacular like for example &lt;a href="http://martinfowler.com/bliki/AnemicDomainModel.html"&gt;Anemic Domain Model&lt;/a&gt;. Which is very common and appreciated by some people. It decouples basically everything including the data and the logic. In my opinion DI encourages to use this approach because it enforces to drop the encapsulation and lets to do anyone anything - you have to set the properties somehow, right?
Of course there are solutions like constructor injection and factory beans but they are used rarely.

As far as I remember Spring 1.0 has been announced as a holy grail of JEE development. People dropped EJB2 and started learning Spring. I must admit EJB2 didn't have much in common with object oriented programming as well. The goal of Spring was to simplify JEE project and it has been achieved. Unfortunately Spring has inherited EJB2 functional approach - or Spring users did that.

Did you ever seen a Java project written by someone who is dazzled by DI? It's easy to recognize such a project. If during the review you have to analyze call hierarchies defined mostly in DI configuration and distributed among several XML files - that's it.

DI is not a golden hammer of software development and we also must remember about the design patterns. The art is to join them together and find the balance to not see everything as a nail anymore.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6252922358877820177-6890514210791168567?l=www.krzysztofbialek.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.krzysztofbialek.com/feeds/6890514210791168567/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6252922358877820177&amp;postID=6890514210791168567' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/6890514210791168567'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/6890514210791168567'/><link rel='alternate' type='text/html' href='http://www.krzysztofbialek.com/2009/06/dependency-injection-bad-practices.html' title='Dependency Injection bad practices'/><author><name>Krzysztof Białek</name><uri>http://www.blogger.com/profile/00013320014264918228</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://2.bp.blogspot.com/-m7eO378Pvbk/TV7eBsTjBuI/AAAAAAAABY8/gS1BgZctvJM/s220/kb.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6252922358877820177.post-5094014079630252836</id><published>2009-06-24T13:03:00.005+02:00</published><updated>2009-06-24T13:09:28.495+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='parallel'/><category scheme='http://www.blogger.com/atom/ns#' term='concurrency'/><title type='text'>"From Concurrent to Parallel" by Brian Goetz</title><content type='html'>Brian Goetz is talking about the fork-join framework and parallel array types coming in Java SE 7. For me it looks absolutely great. See &lt;a href="http://www.infoq.com/presentations/brian-goetz-concurrent-parallel"&gt;the presentation&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6252922358877820177-5094014079630252836?l=www.krzysztofbialek.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.krzysztofbialek.com/feeds/5094014079630252836/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6252922358877820177&amp;postID=5094014079630252836' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/5094014079630252836'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/5094014079630252836'/><link rel='alternate' type='text/html' href='http://www.krzysztofbialek.com/2009/06/from-concurrent-to-parallel-by-brian.html' title='&quot;From Concurrent to Parallel&quot; by Brian Goetz'/><author><name>Krzysztof Białek</name><uri>http://www.blogger.com/profile/00013320014264918228</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://2.bp.blogspot.com/-m7eO378Pvbk/TV7eBsTjBuI/AAAAAAAABY8/gS1BgZctvJM/s220/kb.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6252922358877820177.post-696254214551435417</id><published>2009-06-19T09:11:00.007+02:00</published><updated>2009-06-19T10:04:49.617+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='sql'/><category scheme='http://www.blogger.com/atom/ns#' term='spring'/><category scheme='http://www.blogger.com/atom/ns#' term='jdbc'/><title type='text'>Spring JDBC - don't forget to call compile()</title><content type='html'>If you're using Spring JDBC &lt;a href="http://static.springframework.org/spring/docs/2.5.6/api/org/springframework/jdbc/object/StoredProcedure.html"&gt;StoredProcedure&lt;/a&gt; and if your application produces errors like that below from time to time. 
&lt;code&gt;
ERROR [...] CallableStatementCallback; uncategorized SQLException for SQL [&lt;b&gt;{? = call foo.bar(??)})}?)}&lt;/b&gt;]; SQL state [99999]; error code [17041]; Missing IN or OUT parameter at at index:: 3; nested exception is java.sql.SQLException: Missing IN or OUT parameter at index:: 3
&lt;/code&gt;
Then check if you call method &lt;a href="http://static.springframework.org/spring/docs/2.5.6/api/org/springframework/jdbc/object/RdbmsOperation.html#compile()"&gt;compile()&lt;/a&gt; after defining your IN and OUT parameters otherwise you might expect such an error being thrown during deployment to live system.

If compile() is not called explicitly then Spring builds SQL string lazily at first call. If this stored procedure is called in more than one thread in parallel (which is very common scenario) then a race starts. SQL string variable is shared between the threads and as there is no synchronization mechanism involved all threads are appending to it.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6252922358877820177-696254214551435417?l=www.krzysztofbialek.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.krzysztofbialek.com/feeds/696254214551435417/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6252922358877820177&amp;postID=696254214551435417' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/696254214551435417'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/696254214551435417'/><link rel='alternate' type='text/html' href='http://www.krzysztofbialek.com/2009/06/spring-jdbc-dont-forget-to-call-compile.html' title='Spring JDBC - don&apos;t forget to call compile()'/><author><name>Krzysztof Białek</name><uri>http://www.blogger.com/profile/00013320014264918228</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://2.bp.blogspot.com/-m7eO378Pvbk/TV7eBsTjBuI/AAAAAAAABY8/gS1BgZctvJM/s220/kb.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6252922358877820177.post-1973260921899885583</id><published>2009-06-16T08:51:00.003+02:00</published><updated>2009-06-16T08:54:30.903+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='subversion'/><category scheme='http://www.blogger.com/atom/ns#' term='ubuntu'/><title type='text'>Subversion 1.6 for Ubuntu Jaunty</title><content type='html'>Subversion 1.6 is now available in the &lt;a href="https://launchpad.net/~anders-kaseorg/+archive/subversion-1.6"&gt;launchpad repository&lt;/a&gt;.
&lt;code&gt;
deb http://ppa.launchpad.net/anders-kaseorg/subversion-1.6/ubuntu jaunty main
deb-src http://ppa.launchpad.net/anders-kaseorg/subversion-1.6/ubuntu jaunty main
&lt;/code&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6252922358877820177-1973260921899885583?l=www.krzysztofbialek.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.krzysztofbialek.com/feeds/1973260921899885583/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6252922358877820177&amp;postID=1973260921899885583' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/1973260921899885583'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/1973260921899885583'/><link rel='alternate' type='text/html' href='http://www.krzysztofbialek.com/2009/06/subversion-16-for-ubuntu-jaunty.html' title='Subversion 1.6 for Ubuntu Jaunty'/><author><name>Krzysztof Białek</name><uri>http://www.blogger.com/profile/00013320014264918228</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://2.bp.blogspot.com/-m7eO378Pvbk/TV7eBsTjBuI/AAAAAAAABY8/gS1BgZctvJM/s220/kb.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6252922358877820177.post-8963875881763011006</id><published>2009-06-15T08:34:00.005+02:00</published><updated>2009-06-15T08:49:07.692+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='analyzer'/><category scheme='http://www.blogger.com/atom/ns#' term='mat'/><category scheme='http://www.blogger.com/atom/ns#' term='eclipse'/><category scheme='http://www.blogger.com/atom/ns#' term='memory'/><category scheme='http://www.blogger.com/atom/ns#' term='heap'/><title type='text'>MAT - Eclipse Memory Analyzer</title><content type='html'>Eclipse Galileo is just around the corner and brings MAT - heap memory analyzing tool on board.
Let's give it a try. &lt;a href="http://dev.eclipse.org/blogs/memoryanalyzer/2008/05/27/automated-heap-dump-analysis-finding-memory-leaks-with-one-click/"&gt;Here&lt;/a&gt; is very nice introduction.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6252922358877820177-8963875881763011006?l=www.krzysztofbialek.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.krzysztofbialek.com/feeds/8963875881763011006/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6252922358877820177&amp;postID=8963875881763011006' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/8963875881763011006'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/8963875881763011006'/><link rel='alternate' type='text/html' href='http://www.krzysztofbialek.com/2009/06/eclipse-memory-analyzer-mat.html' title='MAT - Eclipse Memory Analyzer'/><author><name>Krzysztof Białek</name><uri>http://www.blogger.com/profile/00013320014264918228</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://2.bp.blogspot.com/-m7eO378Pvbk/TV7eBsTjBuI/AAAAAAAABY8/gS1BgZctvJM/s220/kb.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6252922358877820177.post-5065747060354426257</id><published>2009-02-26T19:52:00.012+01:00</published><updated>2009-03-02T13:36:48.774+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='development'/><category scheme='http://www.blogger.com/atom/ns#' term='architecture'/><title type='text'>The "species" of Software Architects</title><content type='html'>During a discussion with one of my colleagues I was asked to write down the definition of Software Architect I use. I recognized this as a good opportunity to refine the meaning of this term for me as well.
First I read what wikipedia says about the &lt;a href="http://en.wikipedia.org/wiki/Software_architect"&gt;Software Architect&lt;/a&gt;. Although the article is very detailed I was happy to find my state of knowledge being very similar to it.
I'd like to emphasize two following aspects:
&lt;ol&gt;&lt;li&gt;One of the agile practices says, the architects must write code. I think, especially &lt;span style="font-weight:bold;"&gt;Application architects&lt;/span&gt; ought to follow this principle. In my opinion such a person should be a skilled and experienced developer with stellar knowledge about the patterns, frameworks, methodologies and tools. And must be an active member of the development team.&lt;/li&gt;
&lt;li&gt;&lt;span style="font-weight:bold;"&gt;Enterprise architects&lt;/span&gt; design and coordinate the way the applications communicate with each other. He is interested in the contracts, SLAs. He cooperates with the Application architects and gathers feedback from them.&lt;/li&gt;&lt;/ol&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6252922358877820177-5065747060354426257?l=www.krzysztofbialek.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.krzysztofbialek.com/feeds/5065747060354426257/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6252922358877820177&amp;postID=5065747060354426257' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/5065747060354426257'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/5065747060354426257'/><link rel='alternate' type='text/html' href='http://www.krzysztofbialek.com/2009/02/species-of-software-architects.html' title='The &quot;species&quot; of Software Architects'/><author><name>Krzysztof Białek</name><uri>http://www.blogger.com/profile/00013320014264918228</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://2.bp.blogspot.com/-m7eO378Pvbk/TV7eBsTjBuI/AAAAAAAABY8/gS1BgZctvJM/s220/kb.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6252922358877820177.post-8242678727835001404</id><published>2009-02-21T19:33:00.014+01:00</published><updated>2009-03-02T13:39:53.538+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='development'/><category scheme='http://www.blogger.com/atom/ns#' term='milestone'/><title type='text'>Improve development with milestones</title><content type='html'>The team which I'm member of works in a month long iterations. We often have to develop quite large user stories. To improve our development process we introduced the idea of milestones.&lt;br/&gt;
How to define the milestones:
&lt;ol&gt;&lt;li&gt;Define about 3-4 milestones (for a month long iteration).&lt;/li&gt;&lt;li&gt;Each milestone must deliver a testable set of functionality.&lt;/li&gt;&lt;li&gt;One milestone can comprise tasks from more than one story.&lt;/li&gt;&lt;/ol&gt;&lt;br/&gt;It gives the following benefits:
&lt;ol&gt;&lt;li&gt;Early testing - as the milestone is delivered the QA team can start doing its work.&lt;/li&gt;&lt;li&gt;Better feeling about the progress of the project (besides the burn-down chart).&lt;/li&gt;&lt;li&gt;Smaller goals are easier to achieve.&lt;/li&gt;&lt;/ol&gt;
Of course the idea of milestones is not new. See more at &lt;a href="http://en.wikipedia.org/wiki/Milestone_(project_management)"&gt;wikipedia&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6252922358877820177-8242678727835001404?l=www.krzysztofbialek.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.krzysztofbialek.com/feeds/8242678727835001404/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6252922358877820177&amp;postID=8242678727835001404' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/8242678727835001404'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/8242678727835001404'/><link rel='alternate' type='text/html' href='http://www.krzysztofbialek.com/2009/02/improve-development-with-milestones.html' title='Improve development with milestones'/><author><name>Krzysztof Białek</name><uri>http://www.blogger.com/profile/00013320014264918228</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://2.bp.blogspot.com/-m7eO378Pvbk/TV7eBsTjBuI/AAAAAAAABY8/gS1BgZctvJM/s220/kb.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6252922358877820177.post-874542615325787631</id><published>2009-02-02T20:17:00.026+01:00</published><updated>2009-09-17T10:40:32.842+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='strategy'/><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='nullobject'/><category scheme='http://www.blogger.com/atom/ns#' term='exceptions'/><title type='text'>Null Object - the third player</title><content type='html'>In my previous post I wrote about my considerations about the exceptions and null values. I suggested one more possibility - an "error-indicating object", which basically means to return a special, recognizable instance of the returned interface. This approach has been already described as the &lt;a href="http://en.wikipedia.org/wiki/Null_Object_pattern"&gt;Null Object Pattern&lt;/a&gt;. Returned object is supposed to do nothing. This approach is mostly suitable for mocking up business entities.
Here I'd like to extend this pattern to the service level. Let's assume we're working on a word processor which is able to read/write documents in many formats. Of course our word processor is very flexible and uses format plug-ins and we are able to add/remove the formats very easily.
What happens when I try to open a document file? For sure the word processor has to choose appropriate format handler. It's very common case when the logic is context dependent and is chosen at runtime. Yes, you're right, that's a well known Strategy Pattern.
All right, so let's assume there are the following interfaces defined:
&lt;pre name="code" class="java"&gt;
public interface DocumentHandlerSelector {
 DocumentHandler selectDocumentHandlerFor(URI documentURI);
}

public interface DocumentHandler {
  Document read(URI documentURI);
  void write(Document documentToWrite);
}

public interface Document {
  boolean isLoaded();
}
&lt;/pre&gt;
Everything is fine as long as the concrete handler can be explicitly chosen by the DocumentHandlerSelector. But what to do if for some reason the application can't pick the correct one? For sure it should show a gentle information about the problems with opening the document in the message box. It can be achieved in two equivalent ways.

The first way - throw the exceptions:
&lt;pre name="code" class="java"&gt;
public Document open(final URI documentURI) {
 Document document = null;
 try {
     DocumentHandler handler = documentHandlerSelector
         .selectDocumentHandlerFor(documentURI);
     document = handler.read(documentURI);
 }
 catch (UnknownDocumentFormatException e) {
     showMessageBox("The document cannot be opened");
 }
 return document;
}
&lt;/pre&gt;
The second way - use the null objects (for both DocumentHandler and Document):
&lt;pre name="code" class="java"&gt;
public Document open(final URI documentURI) {
 DocumentHandler handler = documentHandlerSelector
     .selectDocumentHandlerFor(documentURI);
 Document document = handler.read(documentURI);
 if (!document.isLoaded()) {
      showMessageBox("The document cannot be opened");
 }
 return document;
}
&lt;/pre&gt;And here are the implementations to make the second example easier to understand:
&lt;pre name="code" class="java"&gt;
public class UnsupportedDocumentHandler 
    implements DocumentHandler {

 private static final Document UNSUPPORTED_DOCUMENT 
     = new UnsupportedDocument();

 Document read(final URI documentURI) {
     return UnsupportedDocumentHandler.UNSUPPORTED_DOCUMENT;
 }
}

class UnsupportedDocument implements Document {
 boolean isLoaded() {
     return false;
 }
}
&lt;/pre&gt;
I like the NullObject approach for not returning plain null from the open() method. And what do you prefer?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6252922358877820177-874542615325787631?l=www.krzysztofbialek.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.krzysztofbialek.com/feeds/874542615325787631/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6252922358877820177&amp;postID=874542615325787631' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/874542615325787631'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/874542615325787631'/><link rel='alternate' type='text/html' href='http://www.krzysztofbialek.com/2009/02/null-object-third-player.html' title='Null Object - the third player'/><author><name>Krzysztof Białek</name><uri>http://www.blogger.com/profile/00013320014264918228</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://2.bp.blogspot.com/-m7eO378Pvbk/TV7eBsTjBuI/AAAAAAAABY8/gS1BgZctvJM/s220/kb.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6252922358877820177.post-4576407918842621161</id><published>2009-01-27T18:02:00.001+01:00</published><updated>2009-02-02T22:23:19.799+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='exceptions'/><title type='text'>Throwing an exception vs returning null</title><content type='html'>Recently I came across the following problem: How should the callee report a rejection/failure to the caller?
I'm writing about the Java components such as business logic services, repositories, etc. Our applications are full of them, thus it should be clear to everybody how to deal with this problem.

I can suggest the following possibilities:
&lt;ol&gt;&lt;li&gt;Throw checked exception,&lt;/li&gt;&lt;li&gt;Throw unchecked exception,&lt;/li&gt;&lt;li&gt;Return null,&lt;/li&gt;&lt;li&gt;Return some extra error-indicating object.&lt;/li&gt;&lt;/ol&gt;So, which one should I use? In my opinion only the answers 2 and 4 are correct.
However, surprisingly in the last few days I heard from two other Java guys, that I should return a null.

In this concrete case the service is a stateless converter which transforms back and forth between the XML message and domain objects. Although the message is well formed and valid the converter may not be able to transform the message, because the transformation is content dependant. Should it then return null or throw an exception?

They say it should return null because throwing an exception forces the caller to control the flow with the exceptions. I disagree with that, because from the converter point of view the data it gets is unknown and has to be rejected. The rejection should be reported as an unchecked exception with the exact reason inside. Then the caller can catch the exception and take further steps, or more probably let it bubble up to the MDB and be resolved there.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6252922358877820177-4576407918842621161?l=www.krzysztofbialek.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://www.krzysztofbialek.com/feeds/4576407918842621161/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6252922358877820177&amp;postID=4576407918842621161' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/4576407918842621161'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6252922358877820177/posts/default/4576407918842621161'/><link rel='alternate' type='text/html' href='http://www.krzysztofbialek.com/2009/01/throwing-exception-vs-returning-null.html' title='Throwing an exception vs returning null'/><author><name>Krzysztof Białek</name><uri>http://www.blogger.com/profile/00013320014264918228</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='26' height='32' src='http://2.bp.blogspot.com/-m7eO378Pvbk/TV7eBsTjBuI/AAAAAAAABY8/gS1BgZctvJM/s220/kb.jpg'/></author><thr:total>0</thr:total></entry></feed>
