InfoQ

News

JSR-275: Units and Measures Introduced

Posted by Geoffrey Wiseman on Oct 17, 2007 07:42 AM

Community
Java
Topics
Research
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.

2 comments

Reply

All generic type parameters aren't escaped by Daniel Yokomizo Posted Oct 17, 2007 2:20 PM
Re: All generic type parameters aren't escaped by Ryan Slobojan Posted Oct 17, 2007 9:23 PM
  1. Back to top

    All generic type parameters aren't escaped

    Oct 17, 2007 2:20 PM by Daniel Yokomizo

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

  2. Back to top

    Re: All generic type parameters aren't escaped

    Oct 17, 2007 9:23 PM by Ryan Slobojan

    Hi Daniel, Thanks for the note - the article has been updated. -- Ryan Slobojan

Exclusive Content

Measuring Agile in the Enterprise: 5 Success Factors for Large-Scale Agile Adoption

Michael Mah analyzes the development process in 5 companies: 2 Agile (one of them BMC) and 3 classic. He presents the factors which contributed to the success of BMC's Agile adoption.

Tom Preston-Werner on Powerset, GitHub, Ruby and Erlang

In this interview filmed at RubyFringe 2008, Tom Preston-Werner talks about how both Powerset and GitHub use Ruby and Erlang, as well as tools like Fuzed, god, and more.

David Laribee on Alt.NET and its Mission

David Laribee discusses the purpose of ALT.NET, its mission and future.

Discover RailsKits and Stop Writing Redundant Code

Ruby on Rails has become a popular Ruby framework for creating web applications in recent years. An aspect of creating a web application is the need to repeatedly create the same base functionality.

A Formal Performance Tuning Methodology: Wait-Based Tuning

Steven Haines talks about tackling web application performance tuning by proposing a method called wait-based tuning.

Shaw and Fowler About Forging a New Alliance

Shaw and Fowler talk about the need for a new relationship between the business department and the IT department. Studies have shown that projects mostly fail due to miscommunication between the two.

How to GET a Cup of Coffee

In this article, Jim Webber, Savas Parastatidis and Ian Robinson show how to drive an application's flow through the use of hypermedia in a RESTful application.

Archaeopteryx: A Ruby MIDI Generator

Eccentric artist turned overnight anti-celebrity, Giles Bowkett captures the heart and soul of RubyFringe as he demonstrates his revolutionary Archaeopteryx MIDI drum pattern generator.