Search in sources :

Example 6 with UnitConverter

use of javax.measure.UnitConverter in project tutorials by eugenp.

the class WaterTankTests method givenMeters_whenConvertToKilometer_ThenConverted.

@Test
public void givenMeters_whenConvertToKilometer_ThenConverted() {
    double distanceInMeters = 50.0;
    UnitConverter metreToKilometre = METRE.getConverterTo(MetricPrefix.KILO(METRE));
    double distanceInKilometers = metreToKilometre.convert(distanceInMeters);
    assertEquals(0.05, distanceInKilometers, 0.00f);
}
Also used : UnitConverter(javax.measure.UnitConverter) Test(org.junit.Test)

Example 7 with UnitConverter

use of javax.measure.UnitConverter in project uom-se by unitsofmeasurement.

the class EBNFHelper method newUnitPrecedenceInternal.

private static int newUnitPrecedenceInternal(Unit<?> unit, Appendable buffer, SymbolMap symbolMap) throws IOException {
    UnitConverter converter = null;
    boolean printSeparator = false;
    StringBuilder temp = new StringBuilder();
    int unitPrecedence = NOOP_PRECEDENCE;
    Unit<?> parentUnit = unit.getSystemUnit();
    converter = ((AbstractUnit<?>) unit).getSystemConverter();
    if (KILOGRAM.equals(parentUnit)) {
        if (unit instanceof TransformedUnit<?>) {
            // incosistency
            if (unit.equals(GRAM)) {
                return noopPrecedenceInternal(buffer, symbolMap.getSymbol(GRAM));
            } else {
                // parentUnit = GRAM;
                // converter = unit.getConverterTo((Unit) KILOGRAM);
                converter = ((TransformedUnit) unit).getConverter();
            }
        // parentUnit = GRAM;
        } else {
            converter = unit.getConverterTo((Unit) GRAM);
        }
    } else if (CUBIC_METRE.equals(parentUnit)) {
        if (converter != null) {
            parentUnit = LITRE;
        }
    }
    // https://github.com/unitsofmeasurement/si-units/issues/4
    if (unit instanceof TransformedUnit) {
        TransformedUnit transUnit = (TransformedUnit) unit;
        if (parentUnit == null)
            parentUnit = transUnit.getParentUnit();
        // String x = parentUnit.toString();
        converter = transUnit.getConverter();
    }
    unitPrecedence = formatInternal(parentUnit, temp, symbolMap);
    printSeparator = !parentUnit.equals(AbstractUnit.ONE);
    int result = ConverterFormatter.formatConverter(converter, printSeparator, unitPrecedence, temp, symbolMap);
    buffer.append(temp);
    return result;
}
Also used : TransformedUnit(tec.uom.se.unit.TransformedUnit) UnitConverter(javax.measure.UnitConverter) Unit(javax.measure.Unit) TransformedUnit(tec.uom.se.unit.TransformedUnit) BaseUnit(tec.uom.se.unit.BaseUnit) AbstractUnit(tec.uom.se.AbstractUnit) AnnotatedUnit(tec.uom.se.unit.AnnotatedUnit)

Example 8 with UnitConverter

use of javax.measure.UnitConverter in project uom-se by unitsofmeasurement.

the class UnitConverterTest method testDouble.

@Test
public void testDouble() {
    UnitConverter converter = sourceUnit.getConverterTo(targetUnit);
    double length1 = 4.0;
    double length2 = 6.0;
    double result1 = converter.convert(length1);
    double result2 = converter.convert(length2);
    assertEquals(400, result1, 0);
    assertEquals(600, result2, 0);
}
Also used : UnitConverter(javax.measure.UnitConverter) Test(org.junit.Test)

Example 9 with UnitConverter

use of javax.measure.UnitConverter in project uom-se by unitsofmeasurement.

the class ProductUnit method getSystemConverter.

@Override
public UnitConverter getSystemConverter() {
    UnitConverter converter = AbstractConverter.IDENTITY;
    for (Element e : elements) {
        if (e.unit instanceof AbstractUnit) {
            UnitConverter cvtr = ((AbstractUnit) e.unit).getSystemConverter();
            if (!(cvtr.isLinear()))
                throw new UnsupportedOperationException(e.unit + " is non-linear, cannot convert");
            if (e.root != 1)
                throw new UnsupportedOperationException(e.unit + " holds a base unit with fractional exponent");
            int pow = e.pow;
            if (pow < 0) {
                // Negative power.
                pow = -pow;
                cvtr = cvtr.inverse();
            }
            for (int j = 0; j < pow; j++) {
                converter = converter.concatenate(cvtr);
            }
        }
    }
    return converter;
}
Also used : AbstractUnit(tec.uom.se.AbstractUnit) UnitConverter(javax.measure.UnitConverter)

Example 10 with UnitConverter

use of javax.measure.UnitConverter in project indriya by unitsofmeasurement.

the class TimeUnitQuantity method multiply.

/**
 * @since 1.0.1
 */
@Override
public ComparableQuantity<?> multiply(Quantity<?> multiplier) {
    if (getUnit().equals(multiplier.getUnit())) {
        return TimeQuantities.getQuantity(value * multiplier.getValue().intValue(), timeUnit);
    }
    Unit<?> mulUnit = getUnit().multiply(multiplier.getUnit());
    UnitConverter conv;
    try {
        conv = getUnit().getConverterToAny(mulUnit);
        return TimeQuantities.getQuantity(value * conv.convert(multiplier.getValue()).intValue(), timeUnit);
    } catch (UnconvertibleException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return TimeQuantities.getQuantity(value * multiplier.getValue().intValue(), timeUnit);
    } catch (IncommensurableException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return TimeQuantities.getQuantity(value * multiplier.getValue().intValue(), timeUnit);
    }
}
Also used : IncommensurableException(javax.measure.IncommensurableException) UnitConverter(javax.measure.UnitConverter) UnconvertibleException(javax.measure.UnconvertibleException)

Aggregations

UnitConverter (javax.measure.UnitConverter)54 IncommensurableException (javax.measure.IncommensurableException)18 UnconvertibleException (javax.measure.UnconvertibleException)13 Test (org.junit.jupiter.api.Test)11 Test (org.junit.Test)9 RationalConverter (tech.units.indriya.function.RationalConverter)7 Unit (javax.measure.Unit)5 List (java.util.List)4 Mass (javax.measure.quantity.Mass)4 Date (java.util.Date)3 DateFormat (java.text.DateFormat)2 DecimalFormat (java.text.DecimalFormat)2 Format (java.text.Format)2 NumberFormat (java.text.NumberFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2 HashMap (java.util.HashMap)2 Dimension (javax.measure.Dimension)2 CompoundFormat (org.apache.sis.io.CompoundFormat)2 Angle (org.apache.sis.measure.Angle)2 AngleFormat (org.apache.sis.measure.AngleFormat)2