May 2007


Programming and Code snippets and Java31 May 2007 06:29 am

File names default sort order

If you are developing an application which works with files, one day you’ll try to get list of the names of all files in a particular directory. It could be like this simplified snippet:

    public List<String> getAllImageNames() {
        List<String> names = new ArrayList<String>();
        File imagesDir = new File(IMAGE_BASE_DIRECTORY));
        if (!imagesDir.exists()) {
            return names;
        }
        for (File image : imagesDir.listFiles(COMMON_IMG_FILTER)) {
            names.add(image.getName());
        }
        return names;
    }

    private static final FileFilter COMMON_IMG_FILTER = new FileFilter() {
        public boolean accept(File pathname) {
            return pathname.isFile() && (!pathname.getName().startsWith("."));
        }
    };
 

Let’s assume that you have some image files with the same name suffix and an index number as a trailing part:

System order Natural order
IMG_1.jpg IMG_1.jpg
IMG_2.jpg IMG_2.jpg
IMG_21.jpg IMG_3.jpg
IMG_22.jpg IMG_21.jpg
IMG_3.jpg IMG_22.jpg

Everything seems to be all right, but returned list is sorted in a way Java usually sorts strings. While this results could completely satisfy your program API, it is not very useful to work with such lists for a human. Moreover, such lists are very common in our life, for example, many digital photo cameras store pictures with such names.
Let’s make the problem more general. We have a list of strings, which could contain digits, and we want to sort this array to get a natural ordered list. You’d say that it is piece of cake, because Java has good facility to perform array/list sorting : Collections.sort() and Comparator. Therefore, we need an appropriate Comparator implementation.

Known solutions for natural order sorting

Unfortunately, there are no any well-known library like Jakarta Commons to perform this sort. And if you try to google it, you will find only some posts like this one with home-grown solutions and grumbling about lack of existing proven solution. The most important thing is to know the right keywords to google against it. Most of articles on this topic are about Natural order.

I found some valuable resources:

  • Natural String Order at Stephen Friedrich’s Blog - an article of Java professional with working Java source and even JUnit tests. The implemented algorithm is very comprehensive and can work with different set of sorting rules (Ascii, case insensitive and locale specific).
  • HumaneStringComparator: Sorting Strings for People - another one Comparator implementation by Tim Fennell.
  • Implementation by Pierre-Luc Paour.
  • Natural Order String Comparison - C version of implementation and links to another languages.
  • Natural Order Numerical Sorting - overview of the problem, links to another articles and solutions, but almost all of them are broken or not Java. It seemed that the author was keen about that idea, registered a special domain and gathered essential information, and then left this site floating without any maintain.

Stress test

One of the main software development rule states: don’t invent your own wheel unless the problem is not your core business. As string sorting was a utility purpose matter, before implementing my own solution I decided to test already found. The test was easy and straightforward - every Comparator had to sort the same number of randomly generated and shuffled strings. The strings were generated by fixed pattern: [a-z][0..1000].jpg. Input data was the large array (100000) of such strings:
..............
jqazrrveqy113.jpg
wzedsgzmvo912.jpg
aayexqpfdu311.jpg
zvpzxjwkml354.jpg
nelacribtl964.jpg
rehsgmzugb244.jpg
eoptzxybtz459.jpg
ukbeogpmhe157.jpg
zgvnrzohwc176.jpg
.............. .

I was interested in only one algorithm efficiency parameter - time elapsing during full sort. As usual, the elapsing time itself doesn’t tell much about the efficiency. To have a minimum achievable value to compare with I chose a result given by the comparator based on a standard Java String.compareTo() method which should be the fastest because it doesn’t deal with string parsing.

        Comparator<String> baseComparator = new Comparator<String>() {
            public int compare(String strA, String strB) {
                // Assuming that strA always != null
                return strA.compareTo(strB);
            }
        };
 

The test results are shown in a table:

Author Version Sort Time Pros Cons
Pierre-Luc Paour NaturalOrderComparator 453ms Quite fast hard to read C-style algorithm
Stephen Friedrich NaturalComparator 4828ms Locale specific Too slow
Stephen Friedrich NaturalComparatorAscii 360ms The fastest one Only ASCII
Stephen Friedrich NaturalComparatorIgnoreCaseAscii 500ms Fast enough, case insensitive Only ASCII
Tim Fennell HumaneStringComparator 4797ms Very good example of OOP style, brief understandable algorithm, use Java facilities extensively Too slow and inefficient
Java native String.compareTo() 235ms standard standard

Conclusion

As you can see from the result sheet, the most powerful and fast is Stephen Friedrich’s package. It is written with good Java style, well commented and supplied with JUnit tests. It provides different kind of sorting depends on what are going to sort - simple ASCII strings like file names or country specific strings containing special characters.
Of course, one day I’d like to see any well-proven solution in the Jakarta Commons project and simply add it as a jar.

Social30 May 2007 01:32 pm

I’ve just returned from the movie theater where I was watching the Pirates 3.
Official ad The Pirates is one of very few film series I’ve seen every part of them. Making a good trilogy is a tough job, especially if you care not only about revenues but about happy spectators leaving a cinema after the first night. Firstly, I liked the very first episode. The I was happy to enjoy the second one and started to wait for the continue.
Some impressions about tonight’s performance:

  • New characters from Thailand on the stage.
  • Still great Captain Jack Sparrow with his typical pirate humor.
  • Amazing battles.
  • Film lasts for too long, even with 15 minutes interval to buy additional pop-corn. There are too much action.
  • Unpredictable ending which promises one more new series.
  • It’s not boring, but there are definitely less fun than in previous series.

So, I’d rather recommend watching it for real Pirates’ funs.

Pets and Social29 May 2007 04:05 am

In spite of the every evening thunderstorms and rains during last three days, the weather is still hot, and my working dress code is like on the picture.
I’m happy that I don’t have to wear a suit, and can afford wearing flip flops and shorts instead.

dress_code.jpg

To make the our cat’s life more pleasant, we give him a bath. After some fear of the water, he got used to it. After the shower, he was cleaning and drying himself for a couple of hours, and eventually his fur became extremely white, like a snow. Next time, we’re planning to wash him with a special cat shampoo.

wet_cat.jpg
Programming and Blogging and Social28 May 2007 11:41 am

As I stated a few days ago, I mistakenly broke my previously used FastTrack theme. I liked it very much because of the main photo at the top. There depicted a young men with a rucksack walking along underground station. As his face was hidden and his picture was blurred, his character resembled me. I like wearing rucksacks and usually use subway if I need to get somewhere. This fact made the theme quite personal, and I didn’t need to customize it more for a long time. After I broke the theme and I had some hours to get down to WordPress Themes internal kitchen, I created my own theme.

To not take long, I’ll write my work log:

WP Theme editing on the local machine

Editing a WP theme remotely is a real pain. In this case, you have to upload/download your theme sources every time you change something. More easy way is a installing your WP copy on the local machine. There are lots of howto-s over the Internet. I’m little bit familiar with Apache+PHP+MySQL bundle, and installed it within 30 minutes. After this, I downloaded my current WP files from my hosting directory and upgraded it the latest WP version (2.2).

Tools

I took the FastTrack theme as a base for my new theme. The markup of all themes are very similar, because all of them uses the same layout: header, content, sidebar and footer. All parts are stored in few .php files with correspondent and clear names. The appearance of a theme is mostly determined by СSS file with styles of all parts. Hence almost all time you have to work with a CSS file editor. For this purpose I chose the great FireBug Add-On which show all comprehensive information about a web page including a CSS styles and HTML components on the page. PHP files I edited with Komodo and CSS styles with TopStyle. For choosing right color schema I found out cool on-line tool for color schema generation - kuler.

Creation

I took one of my photos which looked fine as a header image and cropped it to the proper size. Then I tidied up all unnecessary elements like top navigation, odd styles, etc. The width of the post area became wider on 100px. Then I chose a color schema using kuler. It took some time to play with links appearance and font sizes. And in a few hours the new theme I called harpsichord was done.

If you want to customize your theme, I can suggest you starting from Guides to creating your own themes.

P.S.
I eventually tasted the new-brand Eclipse chewing gum :) For those who are unfamiliar with my special interest to it, I’d say that Eclipse is the most popular open-sourced IDE for Java language (not only however).

Eclipse chewing gum
Blogging and Social24 May 2007 01:40 pm

I’ve broken the blog theme fasttrack I’d been using for a year. It happened while I was playing with now-reading plugin which allows to arrange all books you are reading at the moment , adding reviews and rating it. I’ve been delaying the moment of getting familiar with WordPress themes templating, and I’m afraid the necessity of doing it has just come.

General18 May 2007 02:37 pm

Today was the birthday of my dear wife. Firstly, she had a little party at her new work during the lunch time. Then we celebrated it with our closest relatives(all parents) at home. Since we hadn’t had enough time to cook enough, we decided to make an exotic dinner. We ordered delivering of some Japanese national food - sushi from a restaurant. Everybody were delighted with that delicious sea food.
sushi

General14 May 2007 12:18 pm

I finally uploaded all interesting photos I made during our May vacations. If you have some free time and you are interested in Krakow, I strongly recommend you to skim through the Krakow, May 2007 photos. Actually, I’ve been adding photo by photo during last week and it turned out to be the largest topic in my gallery. The total number of photos is 245. Enjoy!

Music and Social13 May 2007 12:11 pm

Verka Serduchka
Yesterday night we were watching Eurovision 2007 song contest final. Ukraine was represented by well-known in CIS countries show man - Verka Serduchka. Verka is a stage-name of an actor who plays a bad-educated, utter ignorant lady who originally worked as a train conductor. Verka speaks an illiterate, sometimes, rude language - “surzhik” stands for mixed Ukrainian and Russian languages. The jokes are extremely primitive and not for intellectuals. Most of intelligent people with good taste consider Verka at least as an offensive show to Ukrainian spectators. In spite of this facts, Verka is extremely popular among vast amount of people. I think that it is bright sign of culture crisis on post-soviet Ukraine.
When the final results were summed up, and Verka won 2nd place, I to fell into a snare. Of course, I was happy that Ukrainian singer took 2nd place, but I couldn’t believe that it did Serduchka.
I’m not good at speaking on culture specific problems, so it is enough for this post.
read more: Verka Goes to Helsinki!

Social09 May 2007 02:24 pm

I’ve just returned from Krakow. Basically, it was amazing vacations which worth another large post. I’m about to start it in a few days, and being under a great impression of the trip it is not easy to arrange all thoughts, photos, feelings and complete the post. However, I’ve already uploaded some of photos to a new gallery topic - Krakow vacations, May 2007. It is not a quick process because I have to sort a 2 gigabytes media I’ve shot on my new photo camera.

While I’m not get used to a working life pace after being on vacations, I’m little bit lazy, and allow to myself watching a video. Yesterday, we had a couple of hours before going to bed, and I chose a film which title I had heard lots of times but I’d never seen it. The film was Requiem for a dream. It was a one more indelible impression of the month. I don’t know what exactly authors wanted to say, but I understood it as a film about addiction, loneliness and pain. And of course about willfulness.
[This paragraph is for that people who have seen it]
There are few things I liked about this film the most. First thing is the possible difference in understanding of the story idea. There are many vectors where the action develops. For somebody the main idea could lay just in serious harm of drugs and dangerous diet pills. Another start blaming the irresponsible doctors who caused a lot of pain. For somebody the main character is mother (there are three mothers - lonely, imagined, and “ideal” parent), and everything are related with them. Also, there are love and friendship.
The second thing is the tension which is permanently intensifying without any relief till the end. There are no usual Hollywood happy end, but such story couldn’t have it. This film provokes spending many days in thinking.
Finally I like soundtracks to the film, it is very simple but exactly what that film needed to be a drama.

If you want to be free, try not to be addicted person as much as you can, and don’t forget your old parents!