Package javax.measure

Provides strongly typed measurements to enforce compile-time check of parameters consistency and avoid interface errors.

See:
          Description

Interface Summary
Measurable<Q extends Quantity> This interface represents the measurable, countable, or comparable property or aspect of a thing.
 

Class Summary
DecimalMeasure<Q extends Quantity> This class represents a measure whose value is an arbitrary-precision decimal number.
Measure<V,Q extends Quantity> This class represents the result of a measurement stated in a known unit.
MeasureFormat This class provides the interface for formatting and parsing measures.
VectorMeasure<Q extends Quantity> This class represents a measurement vector of two or more dimensions.
 

Package javax.measure Description

Provides strongly typed measurements to enforce compile-time check of parameters consistency and avoid interface errors.

Let's take the following example:[code] class Person { void setWeight(double weight); }[/code] Should the weight be in pound, kilogram ??
Using measures there is no room for error:[code] class Person { void setWeight(Measurable weight); }[/code] Not only the interface is cleaner (the weight has to be of mass type); but also there is no confusion on the measurement unit:[code] double weightInKg = weight.doubleValue(KILOGRAM); double weightInLb = weight.doubleValue(POUND);[/code] Measurable work hand-in-hand with units (also parameterized). For example, the following would result in compile-time error:[code] double weightInLiter = weight.doubleValue(LITER); // Compile error, Unit required. [/code]

Users may create their own Measurable implementation:[code] public class Period implements Measurable { long nanoseconds; ... } public class Distance implements Measurable { double meters; ... } public class Velocity3D implements Measurable { double x, y, z; // In meters. public double doubleValue(Unit unit) { ... } // Returns vector norm. ... } [/code]

Users may also combine a definite amount (a number) with a unit and make it a Measure (and a Measurable instance).[code] person.setWeight(Measure.valueOf(180.0, POUND)); // Measure timer.setPeriod(Measure.valueOf(20, MILLI(SECOND)); // Measure circuit.setCurrent(Measure.valueOf(Complex.valueOf(2, -3), AMPERE); // Measure bottle.setPression(Measure.valueOf("23.4 kg/(m.s2)").to(PASCAL)); // Measure // or bottle.setPression(Measure.valueOf("23.4 kg/(m.s2)", Pressure.class); [/code]

Measures may be formatted (or parsed) using either the standard unit format (UCUM based) or a locale sensitive format.[code] // Standard format (UCUM). It is used by Measure.valueOf(...) and Measure.toString() // This format is not locale-sensitive and can be used for unambiguous // electronic communication of quantities together with their units without loss of information. Measure pressure = Measure.valueOf("123.45 N/m2", Pressure.class); // Parse (executes MeasureFormat.getStandard().parse("123.45 N/m2")). System.out.println(pressure.to(NonSI.BAR)); // Format (executes MeasureFormat.getStandard().format(pressure.to(NonSI.BAR)). > 0.0012345 bar // Locale format (here French locale). This format is often nicer looking but // may result in loss of information/precision. Measure acceleration = Measure.valueOf(54321, MICRO(METRE_PER_SQUARE_SECOND)); System.out.println(MeasureFormat.getInstance().format(acceleration)); System.out.println(MeasureFormat.getInstance().format(acceleration.toSI())); > 54 321 m/s²/1000000 > 0,054 m/s² [/code]



Copyright © 2010. All Rights Reserved.