Skip to content

Unit API - Java

Quantities

The Raynigon Unit API provides an Implementation of JSR-385. This allows the usage of Quantity objects. A Quantity object consists of a value and a unit in which the value is represented. The Raynigon Unit API provides static methods to create Quantity objects.

Quantity<Length> distance = Metre(1)
Quantity<Temperature> temperature = Celsius(1)

Arithmetic Operations

The arithmetic functionality defined by JSR-385 is also implemented in the Raynigon Unit API.

Quantity<Temperature> temperature0 = Celsius(1)
Quantity<Temperature> temperature1 = Celsius(2)



Quantity<Temperature> result = temperature0.add(temperature1)



result == Celsius(276.15)

SystemOfUnits

Every Unit used in the Raynigon Unit API has to be associated with exactly one SystemOfUnits.

SISystemOfUnits

The Raynigon Unit API provides the SISystem as implementation for the SI-Units System as a SystemOfUnits. All SI-Units have to be exist in the SISystem itself. If a SI-Unit is missing in the SISystem, a PR should be created to add it.

Custom SystemOfUnits

A custom SystemOfUnits can be implemented by implementing the SystemOfUnits interface. The SystemOfUnits hs then to be registered on the UnitsApiService.

class DummySystemOfUnits implements SystemOfUnits {

    @Override
    String getName() {
        return "DummySystemOfUnits"
    }

    public <Q extends Quantity<Q>> Unit<Q> getUnit(Class<Q> quantityType) {
        return quantityType == Length.class ? new DummyUnit() : null
    }

    @Override
    public Unit<?> getUnit(String string) {
        return "WTF".equalsIgnoreCase(string) ? new DummyUnit() : null
    }

    @Override
    public Set<? extends Unit<?>> getUnits() {
        return Set.of(new DummyUnit())
    }

    @Override
    public Set<? extends Unit<?>> getUnits(Dimension dimension) {
        return dimension == UnitDimension.LENGTH ? Set.of(new DummyUnit()) : Set.of()
    }
}
public class DummyUnit extends AlternateUnit<Length> {

    public DummyUnit() {
        super("DummySystemOfUnits", "WTF", "DummyUnit", new Metre(), Length.class);
    }
}

Register custom SystemOfUnits in code

The registration can be done by calling the "addSystemOfUnits" method on the UnitsApiService or by registering the SystemOfUnits as a service.

UnitsApiService.getInstance().addSystemOfUnits(new DummySystemOfUnits())

Register custom SystemOfUnits with SPI

For general information about SPI see here. To register the custom SystemOfUnits with SPI, create a file javax.measure.spi.SystemOfUnits in the META-INF/services directory. Add the fully qualified name to the file and ensure the file is present at the specified location in the classpath.

com.raynigon.unit.api.core.service.testdata.DummySystemOfUnits