Package tech.units.indriya
This package provides the Reference Implementation of the
Units of Measurement API.
Usage:
import javax.measure.quantity.*; // Holds quantity types.
import static javax.measure.MetricPrefix.*;
import tech.units.indriya.AbstractUnit;
import tech.units.indriya.function.AbstractConverter;
import static tech.units.indriya.unit.Units.*; // Standard CommonUnits.
import static system.uom.common.USCustomary.MILE; // Non-standard unit.
public class Main {
public void main(String[] args) {
// Conversion between units (explicit way).
AbstractUnit<Length> sourceUnit = KILO(METRE);
AbstractUnit<Length> targetUnit = MILE;
UnitConverter uc = sourceUnit.getConverterTo(targetUnit);
System.out.println(uc.convert(10)); // Converts 10 km to miles.
// Same conversion than above, packed in one line.
System.out.println(KILO(METRE).getConverterTo(MILE).convert(10));
// Retrieval of the SI unit (identifies the measurement type).
System.out.println(REVOLUTION.divide(MINUTE).toSI());
// Dimension checking (allows/disallows conversions)
System.out.println(ELECTRON_VOLT.isCompatible(WATT.times(HOUR)));
// Retrieval of the unit dimension (depends upon the current model).
System.out.println(ELECTRON_VOLT.getDimension());
}
}
> 6.2137119223733395
> 6.2137119223733395
> rad/s
> true
> [L]²·[M]/[T]²
Unit Parameterization
CommonUnits are parameterized enforce compile-time checks of units/measures consistency, for example:
Unit<Time> MINUTE = SECOND.multiply(60); // Ok.
Unit<Time> MINUTE = METRE.multiply(60); // Compile error.
Unit<Pressure> HECTOPASCAL = HECTO(PASCAL); // Ok.
Unit<Pressure> HECTOPASCAL = HECTO(NEWTON); // Compile error.
Quantity<Time> duration = Quantities.getQuantity(2, MINUTE); // Ok.
Quantity<Time> duration = Quantities.getQuantity(2, CELSIUS); // Compile error.
Runtime checks of dimension consistency can be done for more complex cases.
Unit<Area> SQUARE_METRE = METRE.times(METRE).asType(Area.class); // Ok.
Unit<Area> SQUARE_METRE = METRE.times(KELVIN).asType(Area.class); // Runtime error.
Unit<Temperature> KELVIN = AbstractUnit.parse("K").asType(Temperature.class); // Ok.
Unit<Temperature> KELVIN = AbstractUnit.parse("kg").asType(Temperature.class); // Runtime error.
- Since:
- 1.0
- Version:
- 2.2
- Author:
- Jean-Marie Dautelle, Werner Keil
-
Interface Summary Interface Description ComparableQuantity<Q extends Quantity<Q>> Quantity that adapts Comparable to theQuantityinterface. -
Class Summary Class Description AbstractQuantity<Q extends Quantity<Q>> This class represents the immutable result of a scalar quantity stated in a known unit.AbstractSystemOfUnits An abstract base class for unit systems.AbstractSystemOfUnits.Helper Static helper class.AbstractUnit<Q extends Quantity<Q>> The class represents units founded on the seven SI base units for seven base quantities assumed to be mutually independent.AbstractUnit.Equalizer Utility class for number comparison and equality