|
JSR-275 - Measurements and Units Specification | ||||||||
PREV PACKAGE NEXT PACKAGE | FRAMES NO FRAMES |
See:
Description
Interface Summary | |
---|---|
Measurable<Q extends Quantity> | This interface represents the measurable, countable, or comparable property or aspect of a thing. |
Class Summary | |
---|---|
Measure<V,Q extends Quantity> | This class represents the result of a measurement stated in a known unit. |
Provides strongly typed measurements to enforce compile-time check of parameters consistency and avoid interface errors.
Let's take the following example:
Should the weight be in pound, kilogram ??
class Person {
void setWeight(double weight);
}
Using measures there is no room for error:
Not only the interface is cleaner (the weight has to be of mass type);
but also there is no confusion on the measurement unit:
class Person {
void setWeight(Measurable<Mass> weight);
}
Measurable work hand-in-hand with units (also parameterized).
For example, the following would result in compile-time error:
double weightInKg = weight.doubleValue(KILOGRAM);
double weightInLb = weight.doubleValue(POUND);
double weightInLiter = weight.doubleValue(LITER); // Compile error, Unit<Mass> required.
Users may create their own Measurable
implementation:
public class Period implements Measurable<Duration> {
long nanoseconds;
...
}
public class Distance implements Measurable<Length> {
double meters;
...
}
public class Velocity3D implements Measurable<Velocity> {
double x, y, z; // In meters.
...
}
Users may also combine a definite amount (scalar, vector, collection, etc.)
to a unit and make it a Measure
(and
a Measurable
instance). For example:
// Scalar measurement (numerical).
person.setWeight(Measure.valueOf(180.0, POUND)); // Measure<Double, Mass>
timer.setPeriod(Measure.valueOf(20, MILLI(SECOND)); // Measure<Integer, Duration>
circuit.setCurrent(Measure.valueOf(Complex.valueOf(2, -3), AMPERE); // (2 - 3i) A
bottle.setPression(Measure.valueOf(Rational.valueOf(20, 100), ATMOSPHERE)); // (20/100) Atm
// Vector measurement.
abstract class MeasureVector<E, Q extends Quantity> extends Measure<E[], Q> {
... // doubleValue(Unit) returns vector norm.
}
MeasureVector<Double, Velocity> v = MeasureVector.valueOf(METER_PER_SECOND, 1.0, 2.0, 3.0);
plane.setVelocity(v);
// Statistical measurement.
class Average<Q extends Quantity> extends Measure<double[], Q>{
... // doubleValue(Unit) returns average value.
}
sea.setTemperature(Average.valueOf(new double[] { 33.4, 44.55, 32.33} , CELCIUS));
// Measurement with uncertainty (and additional operations).
public class Estimate<Q extends Quantity> extends Measure<Double, Q> {
public Estimate(double value, double error, Unit<Q> unit) { ... }
public Estimate<Q> plus(Estimate<Q> that) {...}
public Estimate<?> times(Estimate<?> that) {...}
... // doubleValue(Unit) returns estimated value.
}
|
JSR-275 - Measurements and Units Specification | ||||||||
PREV PACKAGE NEXT PACKAGE | FRAMES NO FRAMES |