
Doh!
Mad science in silico...
Good guidelines on packaging are remarkably hard to come by. But, at minimum, a well designed package structure will minimize dependencies between packages and avoid circular dependencies. I've come to follow other rules of thumb about inter-package dependencies. Everyone can depend on a util package. But a util package should have no dependencies inside the project. Domain model classes should be defined abstractly and be free of outside dependencies.If you're aware of two's compliment representation of integers, you can probably guess the output of the following line of Java:
System.out.println(Integer.toHexString(-1));
Sure enough, you get this:
ffffffff
Knowing that, what do you think the result of this line might be?
System.out.println(Integer.decode("0xffffffff"));
Would you believe NumberFormatException: For input string: "ffffffff"? The reason is that decode works on signed values. Ugly with a capital Ug. Decode works as expected on hexadecimal values in which the most significant bit is clear. But here, that high bit is set. The decode function expects a negative hex value where ever you'd use a negative decimal value.
Now, what would be a proper punishment for the kind of protohuman homunculus who would use a sign with a hexadecimal number? Fifty lashes with a wet noodle for the addled mind that thought this was a good idea:
Integer.toHexString(Integer.decode("-0x1"));
ffffffff
Most sane programmers find this highly annoying. There's a bug dated in 1999, (and several dupes) in the Java bug database for this, and its counterpart bug in Long. What an embarrassment.
One work-around (for integers) is to use a Long when you expect a hex number with the sign bit set.
int thisIsTotallyWeak = Long.decode("0xffffffff").intValue();
Digithead's lab notebook is a place for tutorials, cheat-sheets and stray thoughts on topics related to programming, bioinformatics, machine-learning and visualization.