Woshiadai Dev Notebook

May 5, 2008

instanceof OR getClass()

Filed under: Java, Linux

First, wanna share a nice vim/gvim theme called Wombat. Just download it and put it into $HOME/.vim/colors and setcolors wombat in your $HOME/.vimrc and .gvimrc.

I have seen several interview questions asking you to override equals() method for your class. There are two ways to check the classname. Take a Foo class for example:

public class Foo{
private int id;
public boolean equals(Object o){
if (this == o)
return true;
if (!(o instanceof Foo))
return false;
if (o == null)
return false;
Foo other = (Foo)o;
return (this.id == other.id) ? true : false;
}

Another way:

public class Foo{
private int id;
public boolean equals(Object o){
if (this == o)
return true;
if (getClass() != o.getClass())
return false;
if (o == null)
return false;
Foo other = (Foo)o;
return (this.id == other.id) ? true : false;
}

Basically, it is a question of using instanceof or getClass(). Josh Bloch in his Effective Java and his interview with Altima argues that instanceof should be used because of Liskov substitution principle, meaning that one subclass object should be able to replace occurrences of its superclass object without making changes for correctness.

Cay Horstmann does not agree with the use of instanceof and argues that getClass() method should be used instead to enforce the contract of equals method. He presents a superclass-subclass example where reflexive contract for equals method is broken: parent.equals(child) is true, but child.equals(parent) is false if we stick to the instanceof approach.

I think with some special conditional checks on the inheritance structure, you can still use instanceof and satisfy reflexive contract. But getClass() is much simpler.

So, basically, the question is like this: if Eric is a manager, he is represented as an object e of Employee class and an object m of Manager class which extends Employee class. Do you think e equals m (because they are actually representing a same person, using instanceof approach) or do you think they are not equals (because they are of different classes, using getClass() approach)?

April 25, 2008

OpenSUSE extreme makeover

Filed under: Linux

First, SCIM (fitx) did not work out well for me. It works but even after I have completed Chinese input, the floating window still dangles around and giving me suggestions for the same word over and over again. Plus, I heard people say that some applications might crash SCIM input tool.

So, I simply uninstalled SCIM and got Fcitx, a lightweight input tool. This time, everything worked out very beautifully. The easiest way to install is from OpenSUSE online search and use “one-click install”. The homepage for Fcitx is here and people are really saying good words about it.

============= Make your OpenSUSE look like Mac ================

There are many tutorials from Google and this is one of the best I found that treats every aspects including bootup screen, GDM login window, GDM splash picture, window theme, Compiz/Fusion theme, Firefox theme, pidgin theme, music player theme, wallpapers, etc. This is an open source project called Mac4Lin, really solid job!

I also tried the docking application Avant Window Navigator, which emulates Mac dock for application launchers. Maybe I did not configure it right, it only shows icons representing active application windows, not application launchers that you can use to open applications. So, I did not install this docking application.

Other parts are perfectly fine and I am quite satisfied with the look and feel of my OpenSUSE now. Maybe someday when I save enough money, I will go get a Macbook Pro…

April 21, 2008

OpenSUSE 10.3 - Part 2

Filed under: Linux

There is one last software that I need to use daily, an email client. I have been using Thunderbird for quite some time on Windows and I am very happy about its interface and performance.

Our school email server gets quite slow sometimes and I use Alpine text-based email client. This is the open source version of the popular Pine email client on UNIX and people ported it to various other platforms including Windows. It is lightweight and has a great performance.

So, which one to use on OpenSUSE? I am kinda lazy and copied over my .pinerc from Windows home directory to OpenSUSE home directory. The source code from Alpine website does not compile on my machine, complaining some strange problems and I directly downloaded a copy from here. It installed very nicely and with my existing .pinerc, all my settings from Windows are back into action.

However, I had a problem with “backspace” key. Instead of deleting the character before it, it deletes the character after it, which is the same behavior as delete key. This seems to be a known issue for Linux keyboard mapping and the correct “backspace” behavior is ctrl+H. I tried to set default editor to nano and avoid using pico, the default editor that comes with Alpine, but it did not work.

So, I had to choose another email client. Since Evolution is the default email client, I decided to give it a try. But very disappointedly, the performance really sucks and it took very long to render the interface as well as email content. Although it has nice features like notes, schedule, contacts, etc., I gave it up right away.

Finally, I installed and configured Thunderbird. World is in peace now. It runs smoothly and grabs content very fast. To emulate the Mac style, I also installed CrossOver X theme for Thunderbird. There are some other nice Mac-related themes such as CrossOver and Cutebird. Now, it feels that I am using a Mac now, haha!

=====================

Since I use English and Chinese at the same time, I need a Chinese input tool. On WindowsXP I use Google Pinyin and on Win2003 Server I use sogo.

SCIM is one of the language input platform available and it supports a wide variety of input languages. Someone ported FIT Chinese input tool onto linux and it is called Fitx. It has a sister WeFit, which is the most popular Chinese input software on iPhone.

To install Fitx with least hassle, go to openSUSE software search engine and search for fitx, then click “1-click install” button. When the installation is finished, log out and log in to see it in action. You can configure hot keys to activate and deactivate Fitx.

OpenSUSE 10.3

Filed under: Linux

It’s been quite some time since I last used my OpenSUSE (I have a dual-boot machine and do most of my research projects using Windows tools). Now I am looking for a job and UNIX familiarity seems a precious resource. So… I switched back to OpenSUSE. I have been using SUSE since version 8. I tried Ubuntu again and again, the device driver problems bited me again and again while OpenSuSe always came to my rescure.

Here are something I did to set up the OpenSUSE 10.3 box as my daily development machine.

1) Install Sun J2SE. The default gcj is cool but I’d prefer Sun’s JDK more. Here is a reasonably good tutorial to set up J2SE: http://fedorasolved.org/browser-solutions/sun-jdk. Besides using update-alternatives to add java, I also added javac in a similar manner.

There is one thing that I found incorrect in this tutorial: if you add his java.sh into /etc/profile.d/, this messed up the PATH variable somehow and gdm even cannot start correctly. So, instead I added these to my ~/.profile:
export JAVA_HOME=/opt/jdk1.6.0_06
export PATH=$PATH:$JAVA_HOME/bin
export MANPATH=$MANPATH:$JAVA_HOME/man

2. Install Netbeans (as of writing, 6.1 is still RC2). Netbeans 6 caught up very quickly to Eclipse and it did a great job to integrate lots of useful things in nice packages. Since it also has good support for C/C++ development and Ruby, I decided to use it on OpenSuSe.

However, no matter what I did, there are always problems during installation. First, even though there is only one installation wizard running, it always says “there is another installation instance running, are you sure you want to start a new instance?”, yes, of course. Then it dies and Bug-Buddy, a GUI-based bug reporting application shows up and installation wizard simply crashes. I found someone having the same problem and his solution is to uninstall Bug-Buddy and try installation again, it worked!!!

BTW, you can choose to download and install JDK together with Netbeans. In this case, you still need to add Java plugin to Firefox and update alternatives for java and javac.

3. Install Aptana Studio. This is my current Web application IDE. It support ajax, PHP, RoR, AIR, and iPhone development, covering all major ajax libraries you will find today. And its community version is FREE! You can forget about Dreamweaver, Frontpage, M$ Expression Web, etc. Well, if you are into ASP.NET development, probably you still need VS.NET though.

4. Add several software update sources (I installed OpenSUSE using a live Gnome CD): OpenSUSE non-open source and open source, Mozilla, Gnome stable, etc. With these update sources, you can install software unavailable to the live Gnome installation CD.

5. Update Firefox to the latest stable version. It is still 2.0.0.x as of now. I have been using FF3pre, code named Minefield for sometime on Windows now and it simply blew me away with performance and new features like smart URL address field.

Install these plugins for FF: Greasemonkey, BetterGmail2, Firebug, Session Manager, del.icio.us.

6. Update pidgin, the IM application for linux, to latest stable version. Activate two plugins “historys” and “conversion colors”. The default font size is 12, too big for me and I changed it to 10. The default theme is quite ugly, especially the system tray icon. So, you can find a Pidgin theme on gnome-look made by Embrace.

Go download this Human Pidgin Theme (I guess it is made for Ubuntu based on colors) from here.

Then, login as root. Unpack the package and you will see two directories: pidgin and Style. Use pidgin to replace /usr/share/pixmaps/pidgin directory, which contains original theme. Make a backup if you want to roll back later. The Style directory is a bit confusing and it actually adds a background image. To use it, simply copy pidgin_bg.jpg and .gtkrc-2.0 to your home directory.

Now, start pidgin and you will see a more polished interface.

7. Install new login window themes, gnome application window themes and mouse icon themes. I like Mac themes but don’t have the money right now to buy a Macbook Pro, so I can tune my OpenSUSE at least look similar.

Application Themes: Go to http://art.gnome.org/themes/gtk2/ first using Firefox. Then, open “Appearance Preference” window (computer->control center->Appearance (under look and feel). Then, just drag a theme to your “Appearance Preference” window and it will get installed automatically. Then, choose that newly installed theme. I am using Glossy P theme and it looks like Mac style.

Login Themes: login as root first. Type “gdmsetup” on the terminal and this opens up gdm settings. In “Local” tab, you will see currently installed login themes. Choose one you like.

Or, go to http://art.gnome.org/themes/gdm_greeter/, download a new login theme and install it in gdmsetup window. It says you can drag and drop to install, but I did not succeeded somehow and choosed “Add…” to manually install the themes. I am using “Sunergos Blue GDM Theme” and I like it a lot, simple and beautiful.

Mouse themes: you can download a theme pack from gnome-looks.org and unpack it to ~/.icons. For example, mine icon pack name is Obsidian. Then, after you unpack it into .icons directory, it becomes ~/.icons/Obsidian, rename it to ~/.icons/default and logout and login to see the new effect. I am using a theme called “Shere_Khan_X” to match the Mac window theme.

You can also customize splash screen, but I will leave it for later.






















Get free blog up and running in minutes with Blogsome
Theme designed by Ben de Groot