Package tech.units.indriya
This package provides a Java SE 8 implementation of 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 CommonUnits.
import static tech.units.indriya.unit.MetricPrefix.*;
import static tech.units.indriya.spi.UCUM.*; // Standard & Non-Standard CommonUnits.
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).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:
AbstractUnit
Runtime checks of dimension consistency can be done for more complex cases.
AbstractUnit SQUARE_FOOT = FOOT.times(FOOT).asType(Area.class); // Ok.
AbstractUnit SQUARE_FOOT = FOOT.times(KELVIN).asType(Area.class); // Runtime error.
AbstractUnit KELVIN = AbstractUnit.of("K").asType(Temperature.class); // Ok.
AbstractUnit KELVIN = AbstractUnit.of("kg").asType(Temperature.class); // Runtime error.
- Since:
- 1.0
- Version:
- 1.0
-
Interface Summary Interface Description ComparableQuantity<Q extends javax.measure.Quantity<Q>> Quantity specialized for the Java SE platform.ComparableUnit<Q extends javax.measure.Quantity<Q>> Unit specialized for the Java SE platform. -
Class Summary Class Description AbstractConverter The base class for ourUnitConverter
implementations.AbstractConverter.Identity This class represents the identity converter (singleton).AbstractConverter.Pair This class represents converters made up of two or more separate converters (in matrix notation[pair] = [left] x [right]
).AbstractQuantity<Q extends javax.measure.Quantity<Q>> This class represents the immutable result of a scalar measurement stated in a known unit.AbstractQuantity.Equalizer Utility class for number comparison and equalityAbstractSystemOfUnits An abstract base class for unit systems.AbstractSystemOfUnits.Helper AbstractUnit<Q extends javax.measure.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