Search in sources :

Example 36 with UnitConverter

use of javax.measure.UnitConverter in project com.revolsys.open by revolsys.

the class UnitConverstionOperation method perform.

@Override
public void perform(final int sourceAxisCount, final double[] sourceCoordinates, final int targetAxisCount, final double[] targetCoordinates) {
    final int numPoints = sourceCoordinates.length / sourceAxisCount;
    final int axisCount = this.axisCount;
    final UnitConverter converter = this.converter;
    for (int vertexIndex = 0; vertexIndex < numPoints; vertexIndex++) {
        for (int axisIndex = 0; axisIndex < targetAxisCount; axisIndex++) {
            double value;
            if (axisIndex < sourceAxisCount) {
                value = sourceCoordinates[vertexIndex * sourceAxisCount + axisIndex];
                if (axisIndex < axisCount) {
                    value = converter.convert(value);
                }
            } else {
                value = Double.NaN;
            }
            targetCoordinates[vertexIndex * targetAxisCount + axisIndex] = value;
        }
    }
}
Also used : UnitConverter(javax.measure.UnitConverter)

Example 37 with UnitConverter

use of javax.measure.UnitConverter in project smarthome by eclipse.

the class QuantityType method toUnit.

/**
 * Convert this QuantityType to a new {@link QuantityType} using the given target unit.
 *
 * @param targetUnit the unit to which this {@link QuantityType} will be converted to.
 * @return the new {@link QuantityType} in the given {@link Unit} or {@code null} in case of a
 */
@SuppressWarnings("unchecked")
@Nullable
public QuantityType<T> toUnit(Unit<?> targetUnit) {
    if (!targetUnit.equals(getUnit())) {
        try {
            UnitConverter uc = getUnit().getConverterToAny(targetUnit);
            Quantity<?> result = Quantities.getQuantity(uc.convert(quantity.getValue()), targetUnit);
            return new QuantityType<T>(result.getValue(), (Unit<T>) targetUnit);
        } catch (UnconvertibleException | IncommensurableException e) {
            logger.debug("Unable to convert unit from {} to {}", getUnit(), targetUnit);
            return null;
        }
    }
    return this;
}
Also used : IncommensurableException(javax.measure.IncommensurableException) UnitConverter(javax.measure.UnitConverter) UnconvertibleException(javax.measure.UnconvertibleException) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 38 with UnitConverter

use of javax.measure.UnitConverter in project indriya 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);
    assertEquals(600, result2);
}
Also used : UnitConverter(javax.measure.UnitConverter) Test(org.junit.jupiter.api.Test)

Example 39 with UnitConverter

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

the class TemporalQuantity method divide.

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

Example 40 with UnitConverter

use of javax.measure.UnitConverter in project indriya 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(tech.units.indriya.AbstractUnit) UnitConverter(javax.measure.UnitConverter)

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