Saturday, December 22, 2007

Getting started with KDE4 development

Getting started with KDE4 development is actually really easy (and seams to get easier every day).

I started with compiling kdepim on my own. There were only a few step necessary to do this.

1. I had to check out the source of KDE4 (I checked out all of it) using

svn co svn://anonsvn.kde.org/home/kde/trunk/KDE

2. I had to run cmake on the projects I needed (kdepimlibs and kdepim in my case) and have it generate a Kdevelop project and make files. I did this with

cmake -GKDevelop3 -i .

in the according directories. Things like creating a separate build directory like said in the official documentation seam not to be necessary.

3. I ran make in kdepimlibs and kdepim do create the binaries.

4. To started my own :-) kmail with

kdepim/kmail/kmail.shell

You can not start kdepim/kmail/kmail because you need to set some environment variables which is done by the script.

5. I opened the kdepim project in kdevelop (just worked)

6. To start kmail out of kdevelop (for debugging) I had to add the environment variables set by the shell script to the projects runtime options.

As a long term Eclipse and Netbeans user I miss a lot of their features in KDevelop. Refactoring to name just one. I gave the C++ features of Eclipse an Netbeans a try on the KDE4 source but it turns out that they seam not to be able to handle such a huge codebase.

I still think there is a way to get either Eclipse or Netbeans to work for KDE4 development but it surly needs more time that just a few hours.

So meanwhile I am happy with KDevelop and I will start with my first KDE4 development experiments in kmail.

Wednesday, December 19, 2007

Removing items from a List in Java

Yesterday I discussed with some colleagues how to remove items from a list in Java while iterating this list. A trivial task it seams and yes it is. But still there are several options of how to do this, some of which have interesting side effects.

Here a small summary of what I found out while investigating this topic together with some examples. All examples assume a 5 elements list reading as follows ["item 1 remove", "item 2", "item 3 remove", "item 4", "item 5 remove"]

So lets start with a non working example:
for (String val : list) {
if (val.contains("remove"))
list.remove(val);
}
Well this actually removes the first item, but when moving to the second item a ConcurrentModificationException is thrown. This is because the iterator of the list, at least if the list is an extension of AbstractList, checks in the next() method if there was a modification to the list. If so next() fails.

The next example also uses the for-each loop but breaks after removing the first item.
for (String val : list) {
if (val.contains("remove")) {
list.remove(val);
break;
}
}
This works of course because the next() method of the iterator never gets a chance to check if there was a modification.

From this examples one can clearly see how dangerous it is to call remove(...) in a for-each loop.

So the best way to do this is to use the iterators remove() method. Sadly this method is not available when using the for-each loop so we have to use the good old while(it.hasNext()).

Here is the example for that case:
while (it.hasNext()) {
String val = it.next();
if (val.contains("remove"))
it.remove();
}

Of course calling list.remove(...) would fail like in the for-each loop.

While this was the correct way there is still the question what happens if one uses a normal for loop like in the following example:
for (int i = 0; i < list.size(); i++) {
if (list.get(i).contains("remove"))
list.remove(i);
}

Well this works. But careful. The lists size is reduced with each remove. Storing list.size() in a variable and using this variable breaks the program.

So the only good way to remove items from a list while iterating the list is to use a while-loop, a good old fashioned iterator and the iterators remove method.