Dec
19
2007
13

Java Tips: Initializing Collection

Especially in unit test, 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: Rob Juurlink commented that this solution will create a fixed read only collection, so you may want to wrap it with ArrayList (or other Collection class) to make it writable

Written by Nanda Firdausi in: java, programming | Tags: ,
Dec
19
2007
0

AppUpdater: Updating applications in Windows

Ubuntu’s users who use apt must like this when they work with a Windows machine. This application is very similar to apt but works under Windows machine.

Sure, the supported applications list is not as big as what apt has, but for starting, this can be a good start.

Via: LifeHacker

Written by Nanda Firdausi in: windows | Tags: ,
Dec
19
2007
3

Google Talk Translator

This is so cool! You can add a translator to your Google Talk friend. It will automatically translate all your sentence to the language that you want. This is so useful to chat with other friend with different language or even just to translate some sentences that you don’t understand.

Google Talk translator

To do this you just have to add this email to your friends list: “[from language]2[to language]@bot.talk.google.com”, and the supported language pairs are: ar2en, bg2en, de2en, de2fr, el2en, en2ar, en2de, en2el, en2es, en2fr, en2it, en2ja, en2ko, en2nl, en2ru, en2zh, es2en, fi2en, fr2de, fr2en, hi2en, hr2en, it2en, ja2en, ko2en, nl2en, ru2en, uk2en, ur2en, zh2en. So for translating English to Arabic, just add en2ar@bot.talk.google.com and start chatting.

Great work Google Talk team!

EDIT: fixed the screen capture

Written by Nanda Firdausi in: google, technology | Tags: , ,
Dec
18
2007
1

Java Tips: LineNumberReader

Ever have a requirement to read a file with the information about the line number? Sure, a normal FileReader/BufferedReader and a counter will solve the problem, but JDK provides more elegant solution by using LineNumberReader. This is not a huge problem, but will save several seconds of your debugging time.

Example:

LineNumberReader ln = new LineNumberReader(new FileReader(filename));
String line = ln.readLine();
int j = ln.getLineNumber();
Written by Nanda Firdausi in: java, programming | Tags: ,
Dec
18
2007
0

Sharing screen capture

As I post before, share is really becoming a really important nowadays. And this wave is also coming to the screen capture. One obvious application of it is of course to explain something nicely to other people.

Before, we are pretty pleased with the functionality provided by SnagIt Pro or Snapz Pro X. But now, they are considered as too hard for supporting sharing. Let’s review the step you need to take a screen capture and show it to friend using instant messaging application.

  1. Capture the screen using screen capture application and save it
  2. Annotate the image using image editor
  3. Upload to online storage using file manager
  4. Put the URL in the instant messaging

Wouldn’t it be nice if we can do all of those things in one workflow from one application? That’s the idea of Jing and Skitch. Both applications offer the same functionalities. You can do the capture, annotate the result, and upload it to an online storage in one flow. They both even provide the default online storage to be used for the purpose. Beside that, it also possible to upload the picture to a FTP, Flickr, or in case of Skitch, to .Mac.

So how they compare to each other? Here is the result of my observation:

Jing:

  • Available now, you can try it or whatever purpose you like
  • Available in both Windows and Mac (Unfortunately no Linux version)
  • Still have some bugs in the application (unexpected quit etc)

Skitch:

  • The annotation seems to be easier
  • Available only in Mac
  • Currently only available in closed beta

Overall, I think both applications are pretty wonderful. They are easy to use, the interface is elegant and doesn’t take a lot of time to master all the functionalities. Both applications also support video capture.

If there is no further innovations from both of them, the next question is how much money they ask from the user. Certainly, I really hope we can see a FREE tag… :)

Written by Nanda Firdausi in: share, technology | Tags: ,
Dec
17
2007
1

Kampuang nan jauh di mato

Great play, sir!

Here is the lyric:

Kampuang den jauh di mato
Gunung sansay baku liliang
Takana jo kawan - kawan den lamo
Sangke basuliang - suliang

Pan duduk nyo nan elok
Nan suko bagotong royong
Sakit sanang samo-samo di raso
Den takana jo kampuang.

Takana jo kampuang
Ibu, ayah, adiak sadonyo
Rasu ma imbau-imbau den pulang
Den takana jo kampuang.

Written by Nanda Firdausi in: funny | Tags: , ,
Dec
17
2007
0

Java Tips: MessageFormat

Writing a working code is not enough. There is so wide spectrum from where we can distinguish good programmer and a bad one. This is one example of it.

Suppose we need to write a String that concatenate several predefined substrings with several variables. What do we do? Obviously, the most natural thing is to write:

String c = "The book with title " + title + " is sold with price "
              + price + " to " + buyername;

Nothing wrong with the code but obviously if the sentence build many times, you already know that it’s not that efficient. The use of StringBuffer or StringBuilder in Java 5 can make it much better.

String c = new StringBuilder("The book with title ").append(title)
                    .append(" is sold with price ").append(price)
                    .append(" to ").append(buyername);

Great! But not for future development. The code looks cryptic and hard to be changed. The use of MessageFormat is the best solution for such problem.

String result = MessageFormat.format(
     "The book with title {0} is sold with price {1} to {2}", 
      new Object[] {title, price, buyername});

This is even better since we can customize the format of the price, for example:

String result = MessageFormat.format(
     "The book with title {0} is sold with price {1,number,currency} to {2}", 
      new Object[] {title, price, buyername});
Written by Nanda Firdausi in: java, programming | Tags: ,
Dec
17
2007
0

JavaScript - Applet communication

I don’t like applet… and I don’t like javascript. But some time you just need to use them. It is bad if it happens but even worse if you have to make them communicate to each other.

Java Script call applet function
JavaScript can call applet function. Suppose we have an applet with name “appletsample” and main class have a public method name register()

public Main extend Applet {
 
   public void start() {
      -- applet code --
   }
 
   public void register() {
      -- do something here --
   }
 
}

We can call register() from javascript like this:

   document.appletsample.register();

Just remember that the code that is called from applet is working on different context, which means:

  1. We have to work with security issue
  2. If the code generate UI, it will use standard Look&Feel of the OS

To work around the security issue, there is two suggestions can be chosen:

  • javascript only change a boolean variable, and create one thread that check this variable all the time, and if there is some changes, trigger the real action which need security permission.
    public Main extend Applet implements Runnable {
     
       boolean generate = false;
     
       public void start() {
          Thread thread = new Thread(this);
          thread.start();
          -- applet code --
       }
     
       public void register() {
          generate = true;
       }
     
       public void run() {
          while (true) {
              if (generate) {
                 -- do something here --
              }
          }
       }
     
    }
  • wrap the code that needs security permission by java.security.PrivilegedAction
    Example:

    public Main extend Applet {
     
       public void start() {
          -- applet code --
       }
     
       public void register() {
          AccessController.doPrivileged(new PrivilegedAction() {
             public Object run() {
                -- do something here --
             }
          });
       }
     
    }

The first solution, however, takes a lot of CPU processes, where in a while, will also solve problem with Look&Feel. The second solution is not solving the Look&Feel problem, but uses CPU efficiently.

Call javascript from applet
We need classes from netscape. This is located in file plugin.jar, you can find it in JRE-DIR/lib/. After you find the file, include it with your application classpath. Then write code in your applet like this:

JSObject win = (JSObject) JSObject.getWindow(this);
(String) win.eval("alert('test javascript');");

Change alert(’test javascript’); to whatever javascript code you want to call from applet.

Written by Nanda Firdausi in: java, programming | Tags: ,
Dec
17
2007
4

InterCityExpress

ice.jpg

For a long time, Germany inter city train connection was mainly operated by InterCity train. When Japan introduced Shinkansen and France produced TGV, some industrial companies were also planning to build new faster train for Germany. The proposal was approved with one condition… it should able to use old railways so that Germany didn’t have to put a lot of money to build new railway (which is the case for Shinkansen and TGV).

This approach is proofed to be very effective. Germany had already famous for its dense railways and using the approach, in a short time, the train became the most popular rapid mass transport between cities in Germany. In the same time, more and more better railways are build throughout Germany to achieve the velocity’s limit of train. Currently, its top speed can only be achieved on the line between Frankfurt-Cologne and Nuremberg-Munich, where it can run until 300 km/h.

ICE Network

Over the time, several types of the train has been developed. InterCityExpress is picked as the name and the model is named as ICE 1, ICE 2, and ICE 3. Several variations of the models are also developed afterward.

Most of the train has signal amplifier for mobile phone so people can still use their phone despite of the speed of the train. In the modern model, every two seats in ICE has access to the electrical power so its very convenient for businessmen or anybody that want to use laptop or charge battery. And recently, several ICEs are also equipped with WLAN courtesy of T-Mobile.

This train is also considered as very attractive for many train fans. This can be concluded from many fan pages about the train over the internet. Every information about the train can also be read in the fan pages. Two of the most interesting sites are http://www.ice-fanpage.de/ and http://www.railfaneurope.net/pix/de/electric/emu/ICE/pix.html.

Written by Nanda Firdausi in: technology | Tags: ,
Dec
17
2007
2

Having memory problem with Firefox? Use Flock!

Several months ago when I first tried Flock, I was pretty disappointed. The browser is buggy and even has bigger memory problem as Firefox. Not long after the installation, I uninstall it. And I never intended to try it one more time. Not even when it escaped the beta. A social browser just doesn’t get my attention.

This is somehow changed when I see the post in Techcrunch. According to the post:

Then came Flock 1.0. I’d never been a Flock fan before, always believing it to be nothing more than Firefox with plugins (Flock is based on the Firefox engine). Having watched the demo at TechCrunch 40 I downloaded the beta of Flock 1.0 and surfed away without incident. Some how the folks at Flock had tweaked the underlying Firefox engine to stop the memory issues.

So I give Flock another try. And yes, it seems to me that it is working better than Firefox. The previous post shows an image how much memory taken by Flock compared to Firefox.

Memory comparison of Flock and Firefox

Great work Flock team. Sadly I won’t still use your social features. I certainly will use Firefox more often if the solution can be implemented in Firefox, but I guess it will just be a dream since Firefox’s memory leak is a feature.

Written by Nanda Firdausi in: technology, tips | Tags: , , ,

Powered by WordPress. Theme: TheBuckmaker. PHP Scripts, OpenID