Reficio™ - Reestablish your software!

JavaCamp #1 in Krakow

Last weekend I returned to Krakow to take part in the Java Camp meeting. I decided to travel by car, which was actually an unfortunate decision. 300km, 2 traffic jams, 6 hours of driving – all these facts perfectly summarize my journey :(   The meeting was organized by PJUG (Polish Java User Group). It took place at AGH University of Science and Technology which I have graduated from, thus it was really nice to return there. The agenda was interesting, especially when you consider the fact that I was one of the speakers :)

The meeting began with the session called “Developing Rich Web Applications in JBoss RichFaces” led by Marcin Nowrot. Marcin presented the overview of the newest RichFaces (3.3) components by showing a sample demo application. I have to admit that this session wasn’t really beneficial for me since it was more like a RichFaces advert. I expected an objective review which would summarize all the pros and cons of the library, so all in all, I was disappointed. I also didn’t like the fact that hardly no code was shown. What is more, Marcin’s application was similar to the Exadel Demo so everybody could do the same test at home. The last thing I would like to mention is the speech code used by Marcin. Addressing people by sir or madame is not a common behavior on JUG meetings – it built a wall between Marcin and the audience. But I would like to emphasize the fact that it is only my humble opinion – I am sure that there were many people that enjoyed the session.

Afterwards, I presented jBPM in action! The main goal of my session was to present the most important advantages of leveraging jBPM capabilities in an ordinary JEE application. I also wanted to popularize it a bit since in Poland it is rather unknown. It was confirmed by the fact that only two people raised their hands after I asked how many of them had used any of BPM frameworks before (there was about 60-80 people present). After outlining the basic concepts of BPM I went into the details of jBPM. I presented the wide variety of jBPM tools: ant installer, web console, eclipse plugin and signavio editor. Later, I showed two examples which explained how to integrate jBPM with EJB and Spring. In my opinion people were a little bit surprised that it can be done so easily. Yeah, jBPM is lightweight! Finally, I presented a sample business application implemented in… Grails. It was supposed to automate the process regarding JUG meeting’s organization. Some methods were implemented in two ways: with and without jBPM. First of all I showed the version which didn’t use jBPM – business rules and the logic of the process were hard-coded. Later I showed the version which was implemented with the usage of jBPM. Then I explained all the advantages and disadvantages of such approach. According to the feedback that I received my presentation wasn’t so bad but I don’t want to rate myself… :)

Later, we had the possibility to get acquainted with JavaFX. Andrzej Grzesik introduced the concept of RIA. It is the acronym of Rich Internet Application, but you can also find different kind of RIAs on the Internet :) The presentation was cool. Andrzej shared his experience concerning JavaFX giving an objective view of it’s current status and stability. JavaFX and RIA do not belong to my current area of interest, but it was cool to deepen my knowledge in that field.

Next, there was a long awaited pizza break! It was a great opportunity to chat with other developers and to exchange opinions concerning the sessions which were already held. Pizza was a little bit to spicy :D so we have drunk all the coke left to stay alive, but who cares – it was great!

The last session that I attended was the “Matlab Java Injection” presented by Miroslav Kopecky from Prague. It was a really good presentation! Mirek showed how to leverage Matlab’s capabilities directly in Java applications. He explained step-by-step how to set up the environment, write Matlab functions encompassed by “m” files, import them to Java application and finally use as Java classes. Mirek also presented a wide variety of examples which enabled us to deeply understand all the covered aspects. My favorite example was the JSP servlet showing a javascript plot generated by Matlab. I was really amazed by the fact that Matlab provides such sophisticated Java integration mechanisms. I have even promised myself to write a sample Java-Matlab application, if I will find enough time…

Summing up, it was really great to visit Krakow for the second time this autumn. I met new people, learned new things and gained a priceless practical experience as a speaker. What more could I ask for?

Splitting and Tokenizing Strings in Java

There are at least two ways of tokenizing Strings in Java. Simple examples work like a charm, but it is very easy to encounter some weird or unintuitive behavior while experimenting with complex regexes or some corner cases. Thus, in order to gain deeper knowledge, I am gonna dive into the implementations details and present some general rules of string tokenizing in Java.

How to tokenize a String:

To pass for example SCJP a programmer has to predict the exact result of the split method invocation (including the empty matches – what causes most of the problems). I will now present general rules which could probably help in systematizing the knowledge regarding the String tokenization.

The rules are organized in such a way, that the preceding rules have a bigger priority that the following rules, so you should follow the list until one of the rules matches the situation you are actually examining:

1.) If the regex expression does not match any part of the input:

  • Matcher: returns exactly one element – namely the given String
  • Scanner: returns exactly one element – namely the given String

Example:

  • Matcher: “James Bond”.split(“MI6″, 0) == ["James Bond"]
  • Scanner: new Scanner(“James Bond”).useDelimiter(“MI6″).next() == “James Bond”;

2.) When the given String is empty:

  • Matcher: returns exactly one empty match [""]
  • Scanner: the result is empty []

Example:

  • Matcher: “”.split(“MI6″, 0) = [""]
  • Scanner: new Scanner(“”).useDelimiter(“MI6″).hasNext() == false;

3.) When the delimiter regex is empty:

  • Matcher (index == 0): Tokenized characters of the given String preceded by one empty String
  • Matcher (index < 0): Tokenized characters of the given String preceded and followed by one empty String
  • Scanner: Tokenized characters of the given String

Example:

  • Matcher (index == 0): “007″.split(“”, 0) = ["", "0", "0", "7"]
  • Matcher (index < 0): "007".split("", -1) = ["", "0", "0", "7", ""]
  • Scanner: Scanner s = new Scanner(“”).useDelimiter(“MI6″); s.next() = “0″, s.next() = “0″, s.next() = “7″, s.hasNext() = false;

The last thing I want to mention are the “empty-matches” or “zero-length” matches:

  • Matcher (index == 0): Returns: leading and inner “empty-matches” – BUT IF ALL MATCHES ARE EMPTY – the result set is empty
  • Matcher (index < 0): Returns: leading, inner and trailing "empty-matches"
  • Scanner: Returns: only inner “empty-matches”

Example:

  • Matcher (index == 0): “::”.split(“:”, 0) = []
  • Matcher (index < 0): "::".split(":", -1) = ["", "", ""]
  • Matcher (index == 0): “:1::2::”.split(“:”, 0) = ["" , "1", "", "2"]
  • Matcher (index < 0): ":1::2::".split(":", -1) = ["" , "1", "", "2", "", ""]
  • Scanner: Scanner s = new Scanner(“::”).useDelimiter(“:”); s.next() = “”, s.hasNext() = false;

Hope this helps!

String literal pool and JLS rules for String objects

I have been playing with the String objects in order to review all the rules defined by the JLS regarding the String creation and handling. I have recently created a JUnit test case which examines all the aforementioned rules. Enjoy!

JLS rules for Strings:

  • Literal strings within the same class (§8) in the same package (§7) represent references to the same String object (§4.3.1).
  • Literal strings within different classes in the same package represent references to the same String object.
  • Literal strings within different classes in different packages likewise represent references to the same String object.
  • Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals.
  • Strings computed by concatenation at run time are newly created and therefore distinct.

Other rules:

  • Strings from the string pool are not eligible for garbage-collection since JVM maintains a single reference to each unique String object from the String pool (Subject of discussion: LINK)
  • new operator does not put newly created string into the string pool
  • From the javadoc: “String.intern() returns a canonical representation for the string object. When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

All test methods are presented below. The code compiles and runs without errors.

MSc Thesis Award

I am very excited to announce that Marek’s and my Master Thesis was awarded in the contest organized by the Polish Information Processing Society. Official results can be found here: RESULTS. Now we are awaiting results of ABB IT Challenge which will be published in May 2009. Wish us luck :)