InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

JSR-275: Units and Measures Introduced

Posted by Geoffrey Wiseman on Oct 17, 2007

Sections
Development,
Architecture & Design
Topics
Java ,
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.

All generic type parameters aren't escaped by Daniel Yokomizo Posted
Re: All generic type parameters aren't escaped by Ryan Slobojan Posted
Re: All generic type parameters aren't escaped by Werner Keil Posted
  1. Back to top

    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.

  2. Back to top

    Re: All generic type parameters aren't escaped

    by Ryan Slobojan

    Hi Daniel,

    Thanks for the note - the article has been updated.

    --
    Ryan Slobojan

  3. Back to top

    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>

Educational Content

New-age Transactional Systems - Not Your Grandpa's OLTP

John Hugg discusses high volume transaction processing applications with high and low frequency profiles, and how VoltDB can be used for that purpose.

Cool Code

Kevlin Henney examines code samples to see what can be learned from them starting from the premise that one won’t write great code unless he knows how to read it.

Collaboration: At the Extremities of Extreme

Jason Ayers share the observations he made watching a team of developers collaborating in real time on the same code base, pushing XP, pair programming and continuous integration to their extremes.

Yesod Web Framework

Michael Snoyman presents Yesod, a web framework written in Haskell and containing a web server, templating, ORM, libraries (templating, gravatar, etc.).

Transactions without Transactions

Richard Kreuter and Kyle Banker on how to avoid classical RDBMS transactional systems by using compensation mechanisms, transactional messaging or transactional procedures.

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.

Interview: Software Systems Architecture: Working With Stakeholders Using Viewpoints and Perspectives

InfoQ spoke to the authors of Software Systems Architecture on a couple of new topics, the System Context viewpoint and Agile, which have been added to the second edition.