100 Days of 100 Words: Day 16, Questions about Java’s Demented Dimension

I came across something that struck me as odd, today. While using a java swing object I was trying to change the size of the window that popped up for the user. With JFileChooser you can call setSize with a Dimension object to change the size (supposedly, I had issues getting it to work and it ended up not being that important). The purpose of the Dimension object is to store a width and height as integers. That’s it. A comment at the top of the class clearly states:

* The <code>Dimension</code> class encapsulates the width and
* height of a component (in integer precision) in a single object.

Okay, I get that. Sounds good. But when I looked behind the curtain to see what functions were available within the class, I found this:

* Sets the size of this <code>Dimension</code> object to
* the specified width and height in double precision.
* Note that if <code>width</code> or <code>height</code>
* are larger than <code>Integer.MAX_VALUE</code>, they will
* be reset to <code>Integer.MAX_VALUE</code>.
@param width the new width for the <code>Dimension</code> object
@param height the new height for the <code>Dimension</code> object
@since 1.2
public void setSize(double width, double height) {
this.width = (int) Math.ceil(width);
this.height = (int) Math.ceil(height);

Maybe it doesn’t strike you as odd, but that top line should:

 * Sets the size of this <code>Dimension</code> object to
* the specified width and height in double precision.

The comment at the top of the class stated that the width and height are stored “in integer precision” but here we are with a function in the same class the purports to use double precision. Which is it? The code within the function is simple and we can easily see that the dimensions are not stored in double precision. Math.ceil is called to round up to the next integer and then the result is cast to an integer and stored in the width/height. The point of using doubles is so that numbers like 10.1, 10.543, and 10.92 are all different, but to this function they would all be the same.

My question is this: Why does this function even exist? Is it meant to make things easier for someone dealing with doubles? I don’t think it does. Most of the time a programmer isn’t going to read every line of documentation in an object (Say whaaaaaa??!! I know. It’s true, thought) . In fact, they may not even read the comments on that function, and even if they decided to check it out they would find a comment that lies to them. I will grant you that this seems like a small thing. Likely if your dimensions are set to 801x600 instead of 800.1x599.4, you and your users won’t notice. And if it’s for a display, how would you even display 0.4 pixels? Fair.

Okay, time to move on, right? NOPE.

Okay, time to move on, right? NOPE. Looking at the other functions, I continue to scratch my head. There is a setSize that takes two integers which makes sense because width and height are integers. Make consumers of this functions use that version only. Then they can decide how they want to turn their doubles into integers or if that’s even the thing they want. Worse still is the fact that there are only one version each of getWidth and getHeight both of which return doubles!!! WHY?!

So many questions. Where will the answers come from?

Turns out, as is usually the case, there’s a Stack Overflow response to give me the answer. Looks like Dimension was “retrofitted” and the desired case is that it can be used “wherever a Dimension2D is required” and therefore extends the Dimension2D abstract class and implements its abstract methods. Does that make sense? Why would you implement an interface that took and returned doubles when all you wanted was to store your dimensions in integer form? It’s hard to say. I’m not sure what the original intention was when this was implemented and I may never know. Perhaps I’ll e-mail the authors. But I don’t have the time right now. I’m going to stop myself here and be satisfied with this answer…I hope. If the past is any indication, though, I may end up pulling on this thread more later until I’ve unraveled the whole mystery…if that’s even possible.