Java Tips: Initializing Collection

Especially in unit test, it is common case that we have to initialize an array or a collection.

Well, for array, it’s OK… A simple code that we know can solve the problem:

String[] s = new String [] {"1", "2"};

But how about Collection? Normal way to initialize collection is something like this (which pretty ugly):

List<String> s = new ArrayList<String>();
s.add("1");
s.add("2");

I hardly find an elegant solution until I see this post. There are at least three better solution for the case.

First solution:

List<String> s = new ArrayList<String>() {{ add("1"); add("2"); }};

Which unfortunately, doesn’t pass Java Code Convention (that is, if you format the code, it will become uglier than the original).

List<String> s = new ArrayList<String>() {
   {
      add("1");
      add("2");
   }
};

Second solution:

List<String> s = Arrays.asList(new String[]{"1", "2"});

This solution is the best if you use Java 1.4 or before. But if you use Java 5, the third is more elegant:

List<String> s = Arrays.asList("1", "2");

Great!

EDIT: this solution will create a fixed size BUT modifiable collection, so you may want to wrap it with ArrayList (or other Collection class) to make it writable.

Hello World… Using Spring Roo 1.1.0M1 under STS 2.3.3M1

The first day’s Google I/O keynote shows how much work has been done to integrate Spring Roo with GWT. Despite the fact that the demo is not smoothly done, it is kind of interesting combination. Before we have to do all the work to integrate Spring + Hibernate + GWT manually, now the work has already been done. Even nicer, it has tight integration with Eclipse, my favorite Java IDE :D.

Unfortunately the information on how to start creating and playing is not integrated and I spent quite some times to finally run a very simple application using all the technologies. Let me share my experience here…

  1. What you need first is an STS 2.3.3M1. It’s not that easy to find, so here is the link: http://www.springsource.com/products/springsource-google-download. Unfortunately, you have to fill the forms as I can’t find another way to download it without filling the form. Warning, you need STS 2.3.3M1 not release 2.3.2.
  2. Next you have to run the installer. I had no problem for this and at the end, the STS is nicely installed on my computer.
  3. Run the STS and put the workspace location as you like.
  4. If you start a new workspace you’ll get something like this:
  5. Just close the Welcome page and you will get something like this:
  6. You’ll need to install DataNucleus Eclipse Plugin and Google Plugin for Eclipse. To do so, go the third tab under the dashboard and select both extensions. Install it and restart the STS.
  7. We can start creating a new project. Create a Roo project with name ‘hello’ and top level package ‘com.hello’. Wait a bit for Maven to download all the dependencies.
  8. You can use Roo Shell to start adding entities to the project. Do following command:
    persistence setup --provider DATANUCLEUS --database HYPERSONIC_IN_MEMORY
    entity --class ~.server.domain.Employee --testAutomatically
    field string --fieldName userName --sizeMin 3 --sizeMax 30
    gwt setup
    

  9. After that, you should right click on the project and select Google -> Web Toolkit Settings…. And then just click OK. I don’t know what happens but without it the application complain that it can’t find GWT SDK.
  10. Again, right click on the project. Select Maven -> Enable Dependency Management.
  11. Now you can run the application by right click on it and select Run -> Web Application. There you go you got the GWT version of the application.
  12. Alternatively is to run mvn gwt:run from console or from Eclipse.

That’s that!

Technically-Thorough Review about iPhone 4

Particularly, read the second page about iPhone 4 antenna. In short it can be concluded, that Apple somehow calibrate the bars too optimistically, probably because iPhone 4 is able to work at places where signal is very low. This problem is probably solvable by software update.

Second conclusion is that holding the iPhone 4 in certain way is in fact causes more problem compared to other smartphones. This is unfortunately won’t be solved just by upgrading the software.

Unique and Creative Domain URL

Internationalized domain name makes it possible for person or organization to register a domain name that include non alphanumeric characters to its domain name. This is useful mostly for countries that use German, Japanese, or Arabic.

An example for this is a website of Jebu Island (제부도.com). If you click the link, it will take you to http://xn--hq1bp8p1yi.com/.

While using international characters may work for some, that doesn’t really mean your website will be uniquely identified. It may only help people using those characters to find your website.

A creative way to exploit this internationalized domain name is by using symbols on your domain name. An example for this is what Daring Fireball does for its shortened URLs. The website used http://✪df.ws/ for that purpose and on twitter it’s pretty catchy.

One thing to remember though, don’t use symbols on domain name to make your users find you easier. In fact, it is very hard to type symbols using normal keyboard. Like Daring Fireball, use it somewhere to make the domain names catchy and outstanding.

So the question is now, how to create such domain name? First of course, you need to decide what the name of the website should be. For this you will need a list of supported symbols. You can find that in this page under Script: COMMON.

After that, find out what is the real domain name by entering the name in Punycode Converter. For example I enter ⋄1k.ws and as the result get xn--1k-5qv.ws.

If you’ve got the encoded domain name, you can try to register the domain name to a registrar. I found that domain .ws is the most unrestrictive top level domain that will allow you to use all the symbols on the previous page. The other top level domain like .com, .org, and .net are pretty strict and don’t allow symbols (and only internationalized characters) on the domain name.

So, good luck for your next unique domain name!

Maven Plugin: Java Code Formatter

Enforcing code format in an organization needs discipline. And the best way to do that is by doing some automation. A plugin for Maven will be a easy decision if your organization has used Maven to manage the build.

This plugin is one plugin that exactly does that. The backbone on the plugin comes from Eclipse Java Formatter, which I still endorse as the best in the industry.

Compared to Jalopy, which is now not free, I find Eclipse formatter has the flexibility we often needed. Although it is not bug-free, for most cases it is more than enough.

However, I concerned by the fact that official maven doesn’t have the latest jar of Eclipse JDT. It still only has version 3.3 of it. Hopefully they will have it soon, especially after Helios is released, which bring a lot of improvement to the Java formatter.

MS Word: 'Paste text without formatting' keyboard binding

It’s very annoying if you have to copy text from website or other places to email or you document and get the text formatted like it is on the website. Yes there is option to paste text without formatting in the menu but to get there you have to open dialog and so on and so on.

This document shows you how to create a keyboard binding for ‘Paste text without formatting’ in Microsoft Word 2000 and above. I will just copy the information here.

First we need to create the macro:

To create the macro to cut and paste without formatting

  1. Start Word.
  2. On the Tools menu, point to Macro, and then click Macros to display the Macros dialog box.
  3. In the Macro name box, type PasteUnformattedText.
  4. Make sure that All active templates and documents is displayed in the Macros in list, and then click Create. The Microsoft Visual Basic® Editor is displayed.
    Note If you need to learn about the Visual Basic Editor, see the Power User Corner column Managing Macros with the Visual Basic Editor.
  5. Directly above the End Sub statement in the PasteUnformattedText subroutine, type the following line of code:
    Selection.PasteSpecial DataType:=wdPasteText
    
  6. On the File menu, click Close and Return to Microsoft Word.

And now how to create the keyboard binding. Instead of Ctrl+V that is shown in the article, I will make my keyboard binding for the macro Ctrl+Shift+V.

To create the keyboard binding

  1. On the Tools menu, click Customize.
  2. Click the Keyboard button.
  3. Make sure the Save changes in box displays Normal.dot.
  4. In the Categories list, click Macros.
  5. In the Macros list, click PasteUnformattedText.
  6. Click in the Press new shortcut key box, press and hold the CTRL and Shift key, and then press and hold the V key at the same time. The Press new shortcut key box displays Ctrl+Shift+V.
  7. Click Assign. Click Close and then click Close again.

That’s it… now you can press Ctrl+Shift+V to paste text without formatting easily.

Eclipse: Copy Qualified Java Name

Eclipse has a nice feature to copy qualified name from a resource and this is very useful to create documentation or working with XML. For example:

Say that I need the full qualified name from class CaseFormat. What I have to do is:

  1. Click on + beside CaseFormat.java.
  2. Right click on CaseFormat (not CaseFormat.java).
  3. Select Copy Qualified Name.
  4. There you go, you can paste the full qualified name of class CaseFormat anywhere you want.

But that’s kind of a lot of steps, isn’t it?

OK… you can create a keyboard shortcut for Copy Qualified Name but still you have to expand CaseFormat.java before you can do that. If you don’t expand the *.java node what you’ll get is: ‘/guava/src/com/google/common/base/CaseFormat.java’ instead of ‘com.google.common.base.CaseFormat’.

This problem is kind of annoying and if you have to do this a lot of time, it will certainly damage your productivity.

So here is a tiny plugin to solve the problem.

com.satukubik.copyqualifiedname_1.0.0

You can download it, save it to your dropins folder, and restart Eclipse. After that a shortcut Ctrl+Shift+C is available for you to copy qualified java name without having to expand the *.java node.

For those who are curious, I’ve put the source code of the plugin here: http://code.google.com/a/eclipselabs.org/p/copyqualifiedjavaname/. Feel free to clone, suggest improvement, or report a bug.