I've been tracking down a problem with the CompareToBuilder that's part of commons lang. If I had a boolean in my class, and if used the reflectionCompare method it was throwing a ClassCastException. It didn't make any sense to me.
Then I took at look at the source (soooo useful!) and I finally figured it out I think. (btw, a great site for seeing source of OS projects is this one).
At one point in the commons lang project they have this line:
comparison = ((Comparable) lhs).compareTo(rhs);
which was throwing a ClassCastException. Both lhs and rhs are "Objects". Can you see the problem?
When doing something reflexivly Java casts primitive types to their wrapper types (boolean become Boolean and int becomes Integer). I'm using Java 1.4.2 and Boolean doesn't implement Comparable. Doh!
Sometimes I really wish that they just made compareTo part of the Object class...
Thursday, 25 May 2006
Friday, 19 May 2006
Thursday, 18 May 2006
There's always an excuse...
I can always find an excuse for not running, biking, going for a walk, ... just about anything. My excuse this week? I don't want to bike in thunder showers. And every day this week they have said that it will!! Grrr.... arrggg... I should just suck it up. *sigh*
Tuesday, 16 May 2006
Monday, 15 May 2006
Sorting it all out
One of the things that I learned in the last month or so is that sorting in different languages, with accented characters, isn't the same. :-( I'm going to outline a case-insensitive example in Java and Oracle and my experience with that.
With Oracle as a back-end, if I wanted to order by a String column I would call the upper (or lower) method on the field to return a sorted list. I knew that this was a bit of a hack because some (European?) languages have strange conversions from upper to lower case. I remember reading that's why they made XML case sensitive...
So, with the work that I was doing I run some sql (or hql) with something like "... order by (UPPER(col_name))" and in my test suite I would check that it was sorting correctly by using a case insensitive comparator. Now, to find out that it will not work you just have to read the documentation. But course I never thought that it applied to what I was doing.
Here's an example: if you have the names "BÈretta" and "Butler", the default in Java and using upper in the db are both to return "BÈretta" after "Butler". Not correct in a French locale.
To sort it correctly in Oracle, you should do something like:
"order by nlssort(col_name, 'NLS_SORT = FRENCH')"
To sort it correctly in Java, you would have to use a collator like the RuleBasedCollator. Example:
Collator collator = Collator.getInstance(Locale.CANADA_FRENCH);
// make the ordering case-insensitive
collator.setStrength(Collator.SECONDARY);
The Collator is actually very interesting because you can put different emphasis on different parts of the sorter. Really kind of cool.
With Oracle as a back-end, if I wanted to order by a String column I would call the upper (or lower) method on the field to return a sorted list. I knew that this was a bit of a hack because some (European?) languages have strange conversions from upper to lower case. I remember reading that's why they made XML case sensitive...
So, with the work that I was doing I run some sql (or hql) with something like "... order by (UPPER(col_name))" and in my test suite I would check that it was sorting correctly by using a case insensitive comparator. Now, to find out that it will not work you just have to read the documentation. But course I never thought that it applied to what I was doing.
Here's an example: if you have the names "BÈretta" and "Butler", the default in Java and using upper in the db are both to return "BÈretta" after "Butler". Not correct in a French locale.
To sort it correctly in Oracle, you should do something like:
"order by nlssort(col_name, 'NLS_SORT = FRENCH')"
To sort it correctly in Java, you would have to use a collator like the RuleBasedCollator. Example:
Collator collator = Collator.getInstance(Locale.CANADA_FRENCH);
// make the ordering case-insensitive
collator.setStrength(Collator.SECONDARY);
The Collator is actually very interesting because you can put different emphasis on different parts of the sorter. Really kind of cool.
Labels:
work
Sunday, 14 May 2006
Friday, 12 May 2006
Security over Freedom
Pretty much this whole /. thread is a good read, with the following being the best part:
"The masses almost always value security over freedom until they have so little of either a revolution is born."
"The masses almost always value security over freedom until they have so little of either a revolution is born."
Labels:
interesting read
Subscribe to:
Posts (Atom)