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 {@link javax.measure.Measurable
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.
...
}
[/code]
Users may also combine a definite amount (scalar, vector, collection, etc.)
to a unit and make it a {@link javax.measure.Measure Measure} (and
a {@link javax.measure.Measurable Measurable} instance). For example:
[code]
// Scalar measurement (numerical).
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); // (2 - 3i) A
bottle.setPression(Measure.valueOf(Rational.valueOf(20, 100), ATMOSPHERE)); // (20/100) Atm
// Vector measurement.
abstract class MeasureVector extends Measure {
... // doubleValue(Unit) returns vector norm.
}
MeasureVector v = MeasureVector.valueOf(METRE_PER_SECOND, 1.0, 2.0, 3.0);
plane.setVelocity(v);
// Statistical measurement.
class Average extends Measure{
... // 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 Amount extends Measurable {
public Amount(double value, double error, Unit unit) { ... }
public Amount plus(Amount that) {...}
public Amount> times(Amount> that) {...}
... // doubleValue(Unit) returns estimated value.
}
[/code]
|
Measurable.java | Interface | This interface represents the measurable, countable, or comparable
property or aspect of a thing.
Implementing instances are typically the result of a measurement:[code]
Measurable weight = Measure.valueOf(180.0, POUND);
[/code]
They can also be created from custom classes:[code]
class Delay implements Measurable {
private long nanoSeconds; // Implicit internal unit.
public double doubleValue(Unit unit) { ... |