Package tech.units.indriya.unit
This package provides supports for physics units, in conformity with the
Units of Measurement API.
Usage:
import javax.measure.quantity.*; // Holds quantity types.
import tech.units.indriya.AbstractUnit;
import tech.units.indriya.function.AbstractConverter;
import static tech.units.indriya.unit.Units.*; // Standard units.
import static javax.measure.MetricPrefix.*;
import ...US.*; // US units (external module)
public class Main {
public void main(String[] args) {
// Conversion between units (explicit way).
AbstractUnit sourceUnit = KILO(METRE);
AbstractUnit targetUnit = MILE;
PhysicsConverter 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).toSystemUnit());
// 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.times(60); // Ok.
tUnit<Time> MINUTE = METRE.times(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 = ComparableQuantity.of(2, CELSIUS); // Compile error.
long milliseconds = duration.longValue(MILLI(SECOND)); // Ok.
long milliseconds = duration.longValue(POUND); // Compile error.
</code>
Runtime checks of dimension consistency can be done for more complex cases.
<code>
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.of("K").asType(Temperature.class); // Ok.
Unit<Temperature> KELVIN = AbstractUnit.of("kg").asType(Temperature.class); // Runtime error.
- Version:
- 2.1
- Author:
- Jean-Marie Dautelle, Werner Keil
-
Class Summary Class Description AlternateUnit<Q extends Quantity<Q>> This class represents units used in expressions to distinguish between quantities of a different nature but of the same dimensions.AnnotatedUnit<Q extends Quantity<Q>> This class represents an annotated unit.BaseUnit<Q extends Quantity<Q>> This class represents the building blocks on top of which all others physical units are created.DefaultSystemOfUnitsService ProductUnit<Q extends Quantity<Q>> This class represents units formed by the product of rational powers of existing physical units.TransformedUnit<Q extends Quantity<Q>> This class represents the units derived from other units using converters.UnitDimension This class represents a dimension of a unit of measurement.Units This class defines common units.
It is a representative subset of the SI system.