Skip to main content

Measures of confusion with JSR-275

I love the idea behind JSR-275, a units of measurement API for Java (see here for a background article by Jean-Marie Dautelle, the lead author) but I wish there were more examples out there about how to use it properly.

I'm presently writing some code for a spatial simulation. I want the user to be able to specify constraints on spatial entities such as their maximum area, width, movement over time and so forth. Bringing JSR-275 to bear on this means that I can use a consistent set of units within the code while leaving the user free to express their input data in whatever units are convenient for them.

The theory is beautiful but my, alas, my code is ugly.

Take this snippet for example...
// get a constraint from the user
Constraint constraint = ...

// get the unit used internally for this constraint (e.g. "m")
Unit<?> stdUnit = ...

// the value for the constraint (in the user's units)
double value = constraint.getValue();

// get the label for the user's units (e.g. "ft")
String unitName = constraint.getUnitLabel();

// match the label to a jsr-275 class
Unit<?> unit = Unit.valueOf(unitName);

if ( !unit.isCompatible(stdUnit) ) {
// e.g. mixing up length and area units
// throw an exception
}

if (!unit.equals(stdUnit)) {
/*
* The user is exercising their freedom to use a different unit
* to us (curse them) so we convert their value to one
* expressed in our units.
*
* (This is where it gets ugly)
*/
UnitConverter converter = null;

// is it an area constraint ?
if (unit.isCompatible(Area.UNIT)) {
converter = unit.asType(Area.class).getConverterTo(
stdUnit.asType(Area.class) );

// is it a length constraint ?
} else if (unit.isCompatible(Length.UNIT)) {
converter = unit.asType(Length.class).getConverterTo(
stdUnit.asType(Length.class) );

// bummer - it was something else I forgot to allow for
} else {
throw new RuntimeException("Constraint system doesn't allow for "
+ unit.toString());
}

value = converter.convert(value);


Now I'd like to avoid the if-else block that says Is it an area ? Is it a length ? and just say...
   converter = unit.getConverterTo(stdUnit);

...but the compiler isn't happy with this. It wants the Unit references to be fully parameterized.

If I was only working with length, for instance, things would be simple...
   Unit<Length> stdUnit = ... // our standard unit (e.g. m)
Unit<Length> unit = ... // the user's unit (e.g. cubits)
UnitConverter converter = unit.getConverterTo( stdUnit );


That's concise and elegant. But since I want to allow constraints to be applied to different quantities (length and area at the moment; later possibly others) I seem stuck with the if-else alternative parameterizations above.

I'm sure I must be missing something here. So, dear reader, if you are a JSR 275 guru please enlighten me !

Comments

Popular posts from this blog

Fitting an ellipse to point data

Some time ago I wrote an R function to fit an ellipse to point data, using an algorithm developed by Radim Halíř and Jan Flusser1 in Matlab, and posted it to the r-help list. The implementation was a bit hacky, returning odd results for some data. A couple of days ago, an email arrived from John Minter asking for a pointer to the original code. I replied with a link and mentioned that I'd be interested to know if John made any improvements to the code. About ten minutes later, John emailed again with a much improved version ! Not only is it more reliable, but also more efficient. So with many thanks to John, here is the improved code: fit.ellipse <- function (x, y = NULL) { # from: # http://r.789695.n4.nabble.com/Fitting-a-half-ellipse-curve-tp2719037p2720560.html # # Least squares fitting of an ellipse to point data # using the algorithm described in: # Radim Halir & Jan Flusser. 1998. # Numerically stable direct least squares fitting of ellipses. …

packcircles version 0.2.0 released

Version 0.2.0 of the packcircles package has just been published on CRAN. This package provides functions to find non-overlapping arrangements of circles in bounded and unbounded areas. The package how has a new circleProgressiveLayout function. It uses an efficient deterministic algorithm to arrange circles by consecutively placing each one externally tangent to two previously placed circles while avoiding overlaps. It was adapted from a version written in C by Peter Menzel who lent his support to creating this R/Rcpp version. The underlying algorithm is described in the paper: Visualization of large hierarchical data by circle packing by Weixin Wang, Hui Wang, Guozhong Dai, and Hongan Wang. Published in Proceedings of the SIGCHI Conference on Human Factors in Computing Systems, 2006, pp. 517-520 (Available from ACM). Here is a small example of the new function, taken from the package vignette: library(packcircles) library(ggplot2) t <- theme_bw() + theme(panel.grid = el…

Build an application plus a separate library uber-jar using Maven

I've been working on a small Java application with a colleague to simulate animal movements and look at the efficiency of different survey methods. It uses the GeoTools library to support map projections and shapefile output. GeoTools is great but comes at a cost in terms of size: the jar for our little application alone is less than 50kb but bundling it with GeoTools and its dependencies blows that out to 20Mb.

The application code has been changing on a daily basis as we explore ideas, add features and fix bugs. Working with my colleague at a distance, over a fairly feeble internet connection, I wanted to package the static libraries and the volatile application into separate jars so that he only needed to download the former once (another option would have been for my colleague to set up a local Maven repository but for various reasons this was impractical).

A slight complication with bundling GeoTools modules into a single jar (aka uber-jar) is that individual modules make ext…