Monday, 2 July 2007

My first real Java application

I just found very funny Java application in my archive. It was year 1999 and I was studying college in Ostrava. We had strange theme called Java Technologies. Following applet was my semestral project for the theme. It is very simple, multi threaded (!!!), simulation of cross way. A colored squares are cars and yellow squares in their corners are flashers. The cars follows traffic lights and right-hand rule. Look and enjoy :-)






Do you want sources? Download crossway.zip

What I am missing on del.icio.us

As member of a team I was looking for some solution which will enable us to share bookmarks across the team. Because I have some good experiences with del.icio.us bookmark manager and it also fulfil basic conditions, my focus was clear from the beginning.

The basic conditions were:

  1. bookmarks should be shared among all team members
  2. because the members are traveling and then they are out of corporate network we need access to bookmarks from anywhere
  3. posting and using bookmark should be as simple as possible
Then I identified additional conditions:
  1. some members uses IE, some members uses Mozilla
  2. at least some members of team (eg. me :-) has it's own personal account on del.icio.us and they want to use bookmarks from both of them at a time.
My idea was that every member will have it's own account and will be logged in using browser extension. There will be also team specific account (TA) and every member will add it into it's network. At the beginning I thought that it will enable me to see my and TA's bookmarks on one heap and I will also be able to add for:ta tag from a suggestions when posting new team bookmark. But:
  • network member's bookmarks are not merged between user's bookmarks (I mean merged just for view)
  • for:ta tag suggestion is present, but it adds bookmarks just into "links for you" section of TA (BTW at the moment it is impossible to remove proposed bookmark - see del.icio.us FAQ)
Those two problems cause that:
  • we can't use personal and TA account together with browser extension
  • somebody should be responsible for accepting proposed bookmarks of TA from "links for you"
But I found workarounds (just for Mozilla which implements Live Bookmarks). Both bookmark sets (TA's bookmarks and TA's "links for you" bookmarks) provides RSS feeds. So every member will have two Live Bookmarks through which he can access both groups of shared bookmarks. But the solution still have several imperfections:
  • you cant use browser extension to access team bookmarks
  • you can't search bookmarks by tags (but you can create more Live Bookmarks for each or some tags)
  • when somebody save bookmarks from "links for you" to main group the bookmark is present in both groups
Maybe we are trying to get pears from apple tree. I also know that del.icio.us want to stay as simple as possible and they do it very well, but still I have some proposals to del.icio.us team:
  • improve "links for you" functionality
    • user can define list of trusted users and their proposed bookmarks will be added directly (with some flag) into "my bookmarks"
    • provide possibility to delete bookmarks from "links from you" group (in progress - see del.icio.us FAQ)
  • improve "network" functionality
    • provide possiblity to configure you network member to have his bookmarks merged (just for view) between your bookmarks
  • provide RSS feeds containing users tags
Anyway, at the moment we use it with workarounds and we are happy at least with this solutions, but it can be better.

Thursday, 14 June 2007

Intercepting Filter Design Pattern

Intercepting Filter is classified as an enterprise or J2EE design pattern. Intercepting Filter design pattern is typically used in cases where some messages should be processed but the target processing should be wrapped into chain of arbitrary pre/post processings as are authorization checks, message resources transformations (eg. translation, escaping), ...

Intercepting Filter design pattern is built from five basic components:

  1. FilterManager
  2. FilterChain
  3. IFilter interface
  4. Context
  5. ITarget interface

FilterManager - FilterManager class is a boundary class of Intercepting Filter design pattern. It means that a client uses it as an interface to process some target operation (represented by ITarget interface) in a specific context (represented by abstract interface Context). FilterManager is then typically responsible for FilterChain creation, it's setup (concrete filter set configuration, target propagation, ...) and finally it's execution.

FilterChain - FilterChain class is basicaly data object carrying list of filters and the target, with just very simple logic of filter queue.

IFilter - IFilter interface has just one method. The method is typically implemented as captured on following diagram. It makes some pre processing, and if everything is OK than it calls FilterChain to process to following filter.

If something is wrong (some data are wrong or missing in Context) then it can return or throw exception.

When the target is reached through chain of filters, it is processed, and the thread is returning back same way, so it can make some post processing in every filter, during which it can also make some changes to Context (eg. response translation, escaping, ...).

ITarget - ITarget interface should be implemented as message receiver or it's wrapper.

Context - Context class in this presentation of Intercepting Filter design pattern should be considered as very abstract. It means that it can be totally different type of class or it can be set of classes or it can be some Map, etc. For example it can be ServletRequest together with ServletResponse when we are talking about Intercepting Filter design pattern implementation defined by Sun - Core J2EE Patterns - Intercepting Filter.

Implementation of Intercepting Filter design pattern is pretty simple. The most complex component can be FilterManager which can implement some sophisticated way of configuration which defines mapping filters to context and their configuration. But whenever it is implemented it is very simple to extend message processing by new features or change its behavior.

Related patterns:
  • Decorator [GoF]
    • Does not have FilterChain - the order of filters is defined wrapping one filter into another.
  • Template Method [GoF]
    • Very simple version of Intercepting Filter design pattern implementing Filter Chain (template method) and IFilter implementations (methods called from template method) in one class.
  • Chain of Responsibility [GoF]
    • Does not have FilterChain - the order of "interceptors" is defined as chain of successors.
  • Interceptor [POSA2]
    • Similar also to Chain of Responsibility but the chain of successors is not "hard coded" but propagated to "interceptor" as parameter of the method call.
  • Pipes and Filters [POSA1]
    • The Pipes and Filters design pattern is oriented basically for stream related use cases. It also doesn't provide implicit possibility of post processing.
References:

Wednesday, 30 May 2007

Singleton pattern implementation in 4 steps

The article describes very simple, fast and efficient way how to create class implementing Singleton design pattern [GoF]. Everything is done in 4 steps using standard features of Eclipse IDE (refactoring and source code manipulation). But it is of course applicable for every IDE providing similar refactoring functionality.

Steps:

  • Create new empty 'SingletonClass' class - use New > Class action from popup menu over package or Ctrl+N shortcut. In the dialog just ensure that the source folder and project are specified and define the name of new class. Press Enter or click OK.
New Class Created
  • From the body of the class invoke Source > Generate Constructors from Superclass... action, of course using Alt+Shift+S, C shortcut. In the following diaglog just press Enter or click OK. (This step can be done together with prvious step checking 'Constructors from superclass' option in the 'New Java Class' dialog).
  • From the constructor signature definition (constructor name) invoke Refactor > Introduce Factory... action (using Alt+Shift+T, O shortcut). In an 'Introduce Factory' dialog change 'Factory method name' value to the name of singleton access method (eg. getInstance). Press Enter or click OK.
  • Select 'new SingletonClass()' from 'getInstance()' method and invoke Refactor > Extract Constant... action (Alt+ Shift + T, A).
  • In a dialog you can change 'Constant name' and visibility but typically the default value is OK. So just press Enter or click OK and voila ....
The result is full-value Singleton design pattern implementation created in 4 very simple steps.

Wednesday, 16 May 2007

Code Content Assistant Efficiency

Purpose of this article is to evaluate efficiency of content assistant functionality provided by Eclipse with focus on type name completion. The results will be of course applicable not just for type names but also for field and method names.

Content assistant functionality
Eclipse content assistant is simply suggesting code completions while writing code considering to concrete context - type names, field names, method names, ... It also provides excellent feature called "camel case matches" (NPE matches to NullPointerException) which is also object of my evaluation.

Content assistant efficiency definition
Content assistant efficiency is number, which says how many times more you will get considering to what you spend. When we are talking about type names then this number is defined as typeNameLength/price. Where typeNameLength is simply the number of letters in the type name. The price is lowest number of letters and key presses (arrow down in suggestion list + Enter) necessary to be pressed to generate type name using content assistant.

For example class NullPointerException has 20 letters but using content assistant we can generate the name by 5 key presses (typing "NPE" + one down arrow press, because NoPermissionException is also in the list + Enter). It means that in case of NullPointerException class the efficiency of content assistant is 20/5=4. Basically it means that the result is four time bigger than the effort to generate it.

The example is talking just about one class. But it can be also applicable for set of classes (eg. jar) simply counting their lengths together and dividing by counted prices.

How I evaluated
Of course the biggest challenge was to find lowest number of letters and key presses necessary to be pressed to generate the type name. For this purpose I wrote simple application which for 3905 patterns (maximum 5 words with maximum 5 letters) distributes classes by name into groups where classes in one group matches the pattern with same result. After alphabetical sort of the classes in the group it counts price for every class as length of group id + position in the sorted list (first = 0) + 1 (Enter). The lowest price for a class over all groups is the number what we are looking for.

Example:
Pattern: ^([A-Z])[^A-Z]([A-Z])[^A-Z]([A-Z])[^A-Z].*$
Group Id: NPE
Sorted classes: NoPermissionException, NullPointerException
Prices: 3+0+1=4, 3+1+1=5

Pattern: ^([A-Z][^A-Z]{1})[^A-Z]([A-Z])[^A-Z]([A-Z])[^A-Z].*$
Group Id: NoPE
Sorted classes: NoPermissionException
Prices: 4+0+1=5

Lower price for NoPermissionException class is in the first group so it is the lowest price.

For evaluation I used classes from seven jars (rt.jar, catalina.jar, xercesImpl.jar, derby.jar, xalan.jar, axis.jar, junit.jar) with total number of 12707 classes.

Results
As we can see in the table below the content assistant efficiency counted for all seven jars together is 3.20.

It means that using content assistant is about three times more effectively than typing everything. The second benefit of content assistant usage is higher quality of code, because it is harder to make mistakes.

All this can be true just in case that the user uses content assistant effectively. About how to really use it and what are the best practises see TBD - article about effective useage is in progress.

Attachment
On chart in the image bellow we can see a histograms of class name length and class price. It shows number of classes with concrete length (blue) and number of classes for price (red). We can see the efficiency of the content assistant as a peek of red bars in comparison with blue "hill".