You are brilliant, We are Hiring. Find out more...
Palantir Tech Blog

Archive for September, 2007

Palantir screenshots in the wild: Swing Sightings

Tuesday, September 11th, 2007

Palantir ScreenshotPalantir ScreenshotPalantir Screenshot Palantir Screenshot: dashboardPalantir Screenshot: right_clickPalantir Screenshot: timeline_viewerPalantir Screenshot: flowsPalantir Screenshot: graph_explorerPalantir Screenshot: histogram

We recently had a visit from some distinguished guests. Chris Campbell, a member of the Java 2D Team at Sun, came to see demonstrations of the Palantir products. We were very pleased and flattered by his positive reactions to the work that we’ve done.On the basis of that visit, we were added to the Java Desktop community site as a Swing Sighting Preview and merited a mention on Romain Guy’s blog: Another Pretty Java Application.We’re excited to present this series of screenshots as the first public unveiling of the Palantir applications. On a technical note: everything you see in these screenshots is from live, running applications. The applications are entirely written in Java and the GUIs are composed of custom Swing components.After the jump: bigger thumbnails with a description of each screenshot. You can click above or below to see the full resolution screenshots. (more…)

Getting equals() and hashCode() right

Wednesday, September 5th, 2007

Whenever you override the equals() and hashCode() functions of a class, it is very important that you do so correctly. Bugs in these functions are often quite insidious, because they will not fail catastrophically. Your application will compile and probably do something reasonable, passing any smoke test you throw at it. However, the output will be subtly incorrect. Moreover, by the time you realize that some has gone wrong, there is little information about what caused the error. This is because the error occurred 10 steps ago, nested five levels deep, when a member of a HashSet went MIA. While a NullPointerException is normally trivial to diagnose and fix, you can easily lose several hours tracking down an equals()/hashCode() bug.

There are many excellent tutorials out there on equals() and hashCode(). The best source is probably Items 7 and 8 of Effective Java by Josh Bloch, and we have a couple copies of this book floating around the office. A good online tutorial is Java theory and practice: Hashing it out, by another Java guru, Brian Goetz.

To test your understanding, make sure that you can answer the following questions. I often ask variants of the first two during interviews. The third comes from an error we’ve seen more than once in our code base.

  1. Suppose we have a Media class, composed of title and contents fields. What can go wrong if equals() is based on both fields, while hashCode() is based solely on title?
  2. Conversely, what can go wrong if hashCode() is based on both fields, but equals() is based solely on title?
  3. Consider a class A which overrides equals() and hashCode(). If you create a class B which extends class A, and use Eclipse to generate your equals() and hashCode() functions for class B, then the output of class B’s hashCode() function will depend on the value of super.hashCode(). What happens if you refactor class B so that it no longer extends class A?

Best Practices: compareTo consistent with equals

Sunday, September 2nd, 2007

What is wrong with this class and why? I’ll tell you beforehand there are two things I am looking for and they are both in the compareTo function. Yes, this came from the Palantir code base and caused me some issues. It has been modified slightly for illustrative purposes.

(more…)