The Artima Developer Community
Sponsored Link

Java Community News
Jean-Marie Dautelle on Java Measures and Units

1 reply on 1 page. Most recent reply: Oct 5, 2007 3:07 AM by Mark Thornton

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Frank Sommers

Posts: 2642
Nickname: fsommers
Registered: Jan, 2002

Jean-Marie Dautelle on Java Measures and Units Posted: Oct 3, 2007 1:32 PM
Reply to this message Reply
Summary
The Java Measures and Units specification, JSR 275, is maturing, with a final version expected in early 2008. A recent JavaWorld article by Jean-Marie Dautelle introduces the API and its benefits.
Advertisement

While Java provides many compile-time checks to ensure the safety of code, variable values developers choose to describe physical units, such as those measuring as time and space, have no equivalent safety checks. A developer may choose to mean meters, for example, whereas another developer working on the same project may think that that same variable refers to inches.

The Java Measures and Units API, JSR 275, aims to reduce the chances of such misunderstandings. A recent JavaWorld article by Jean-Marie Dautelle, Introduction to JSR-275: Measures and Units, one of the JSR 275 expert group members, introduces this API:

Developers who work with physical quantities (in scientific, engineering, medical and manufacturing domains, for example) need to be able to handle measurements of those quantities in their programs. 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...

Developers must either use inadequate models of measurements or create their own solutions. A standard solution can be safer, save development time for domain-specific work, and -- most important -- make code more readable and less error-prone. JSR 275 establishes such a framework for Java developers...

Whether or not JSR 275 will be included in the Java 7 platform has not yet been decided. We can certainly hope so, because it provides a common framework allowing unit-safe integration of higher-level libraries (such as JSR 310: Date and Time API).

Dautelle notes that JSR 275 defines a rather simple API, with just three interfaces:

The Java namespace for JSR 275 is javax.measure, with one abstract class, Measure, and one interface, Measurable. A Measure is basically the product of a numerical value and a unit, such as "7.2 liters" or "24 hours," whereas Measurable identifies quantities capable of being measured, such as "altitude" or "delay."

In addition, the JSR 275 API provides convenience methods to convert between various units of measure:

Units can be retrieved from constants held by various SystemOfUnits subclasses, such as SI (holding the "Système International d'Unités" standard units) or NonSI (holding nonstandard but common units). Custom systems of units can be created, and units can belong to several system of units. For example, the imperial system would contain many of the NonSI units. (You can use the SystemOfUnits.getUnits() member method to list all the units held by a system.)

For convenience, the SI class provides static multiplier methods, such as KILO(Unit) or CENTI(Unit), equivalent to the multiplication or division of the unit by a power of 10. For example, KILO(METER) is equivalent to METER.times(1000), whereas CENTI(METER) is equivalent to METER.divide(100).

What do you think of the JSR 275 API? If your programs deal with entities of the physical world, such as time, space, or money, how do you make your choice of measurement units clear to other developers working on the code base?


Mark Thornton

Posts: 275
Nickname: mthornton
Registered: Oct, 2005

Re: Jean-Marie Dautelle on Java Measures and Units Posted: Oct 5, 2007 3:07 AM
Reply to this message Reply
I hope JSR 275 finally succeeds --- I was disppointed at the failure of the previous effort.

My current practice is to use SI base units for relevant quantities. I.e. speeds are always metres per second internally even though the user will want to see miles or km per hour. This has unfortunate conflicts with Java's practice of measuring time in milliseconds.

Flat View: This topic has 1 reply on 1 page
Topic: John O'Conner Explains JavaFX Functions and Operations Previous Topic   Next Topic Topic: Kirill Grouchnikov on Desktop Parallelism

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use