New Early adopter or innovator? InfoQ has been working on some new features for you. Learn more

JSR-275: Units and Measures Introduced

| by Geoffrey Wiseman on Oct 17, 2007. Estimated reading time: 2 minutes |
JSR-275: Units Specification aims to add support for units to Java software development, with the hope of reducing a certain class of errors. As described by Jean-Marie Dautelle, co-spec-lead of JSR-275:

Inadequate models of physical measurements can lead to significant programmatic errors. In particular, the practice of modeling a measure as a simple number with no regard to the units it represents creates fragile code. Another developer or another part of the code might misinterpret the number as representing a different unit of measurement. For example, it might be unclear whether a person's weight is expressed in pounds, kilograms, or stone. The issue is difficult to solve through testing and has already cost millions of dollars to society worldwide. (NASA lost a $125 million Mars orbiter because a Lockheed Martin engineering team used English units of measurement while the agency's team used the more conventional metric system for a key spacecraft operation.)

JSR-275 introduces a new package, javax.measure, with an interface, Measurable, and a class, Measure.

Measurable represents an attribute which could be measured which could be measured (e.g. Altitude, Height), and for which a value can be retrieved, given a compatible unit:

Measurable<length> height = person.getHeight();
long inches = height.longValue(NonSI.INCH);

By using generics, Measurable is typesafe; a compile-time error would be generated when passing Measurable<mass> to a function that requires Measurable<length>:

Measurable<mass> weight = person.getWeight();
person.setHeight( weight ); // error!

Measure represents a particular measurement, the combination of a numeric value and a particular unit. A particular measurable value (e.g. geoffreyWiseman.getHeight()) could be represented by different measures (e.g. Measure.valueOf(73,NonSI.INCH), Measure.valueOf(1.8542,SI.METRE)).

The API also contains quantities (e.g. Mass, Height, Power, Pressure), which are used to parameterize the generic measures and measurements, and Units (INCH, METRE), which are held within systems of units. SI represents the metric system, both with prefixes and units (METER, KILO(METER), etc.), and NonSI holds common units that aren't part of the metric system (DAY, FOOT, KNOT, etc.).

It's possible to convert between units of the same dimension using a UnitConverter. Parsing and formatting are accounted for, including compound units for combinations like feet and inches; hours, minutes and seconds. The API is designed for extension, supporting the creation of new units, systems of units, quantities.

There has been some discussion of using JSR-275 to support JSR-310, the date/time API, but there would need to be further discussion. Stephen Colebourne points out in JSR-275: Suitable for Java 7? that date/time units can't be treated like consistent scientific units for conversion:

Er, how exactly did we manage to convert between months and days? What does that really mean? Well, assuming I've understood everything, what it does is use a definition of a year as 365 days, 5 hours, 49 minutes, and 12 seconds, and a month being one twelth of that. It then goes ahead and does the conversion from months to days using that data.

To learn more, investigate the specification or the reference implementation.

Rate this Article

Adoption Stage

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

All generic type parameters aren't escaped by Daniel Yokomizo

The article didn't escape the generic type parameters with &lt; and &gt; so Measurable<Length> looks like Measurable.

Re: All generic type parameters aren't escaped by Ryan Slobojan

Hi Daniel,

Thanks for the note - the article has been updated.

Ryan Slobojan

Re: All generic type parameters aren't escaped by Werner Keil

Hi Daniel,

There are still some minor typos, e.g. <mass> and similar generic types should be <Mass> instead.

While discussions are currently hot with JCP members including Stephen and JSR-310 on how to integrate those types best, other innovative, competing platforms like .NET (F# mostly), Scala (with a very limited half-baked approach) or Curl (where a Quantity similar to the base type for Mass, Length, etc. in JSR-275 is an essential part of its "LANG.UNITS" package ;-) show how Java or JavaFX must be enhanced to still be attractive for a modern Cloud and RIA type environment.</mass></mass>

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

3 Discuss

Login to InfoQ to interact with what matters most to you.

Recover your password...


Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.


More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.


Stay up-to-date

Set up your notifications and dont miss out on content that matters to you