Sunday, 21 August 2005

Spot the 'deliberate' mistake...

I was coding in commons lang earlier tonight when I found one of my test cases wasn't passing. It took a few minutes to realise my stupid error. So here's the test! Can you spot it too???

public int readToken(char[] chars, int start) {
  int pos = start;
  while (pos < chars.length) {
    if (chars[pos++] == '"') {
      return pos;
    }
  }
  return start++;
}

(By the way, there are lots of possible errors here like NPE and this is a very simplified version of the actual method, however somewhere here is a particular 'weird' error.












Did you find it?
Its the return statement. You might think on glancing at the code that the method would return the value of start plus one. But it doesn't. It returns the value of start, as the ++ occurs after the return statement. Obvious really - yet in a non-obvious way. The fix of course is to do return ++start; ;-).

(Sorry if you got it straight away, but it amused me for a minute or two...)

Thursday, 11 August 2005

Joda-Time 1.1 is released

It took a while, but version 1.1 of Joda-Time, the replacement for JDK Date and Calendar, has been released!

This release does fix a few bugs, however these were mostly relatively minor. It does however include a number of useful new methods and usability enhancements. For example:

  • a new Partial class, allowing any group of datetime fields to be defined. A typical use might be to create a YearMonth object for a credit card
  • new convenience methods for adding periods, such as plusDays(n) or minusHours(n)
  • YearMonthDay and TimeOfDay are now comparable
  • new methods to create a Period from two YearMonthDay or two TimeOfDay objects
  • new methods on Interval - gap, abuts and overlap
  • better handling of two digit years in formatting/parsing
  • added ISO formats for ordinal (dayOfYear) dates

And stay tuned for announcements about Hibernate and JSP integration!

Full details can be found on the website.

Oh, and if anyone wants to help develop another calendar systems, such as Hebrew, Islamic, Chinese, etc. then please contact me!!!