Search in sources :

Example 41 with UnitConverter

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

the class TimeUnitQuantity method divide.

/**
 * @since 1.0.1
 */
@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 42 with UnitConverter

use of javax.measure.UnitConverter in project uom-se 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)

Example 43 with UnitConverter

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

the class TemporalQuantity method multiply.

@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)

Example 44 with UnitConverter

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

the class AbstractQuantity method to.

/**
 * Returns this quantity after conversion to specified unit. The default implementation returns
 * <code>Measure.valueOf(doubleValue(unit), unit)</code> . If this quantity is already stated in the specified unit, then this quantity is returned
 * and no conversion is performed.
 *
 * @param unit
 *          the unit in which the returned measure is stated.
 * @return this quantity or a new quantity equivalent to this quantity stated in the specified unit.
 * @throws ArithmeticException
 *           if the result is inexact and the quotient has a non-terminating decimal expansion.
 */
@Override
public ComparableQuantity<Q> to(Unit<Q> unit) {
    if (unit.equals(this.getUnit())) {
        return this;
    }
    UnitConverter t = getUnit().getConverterTo(unit);
    Number convertedValue = t.convert(getValue());
    return Quantities.getQuantity(convertedValue, unit);
}
Also used : UnitConverter(javax.measure.UnitConverter)

Example 45 with UnitConverter

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

the class PrefixTest method testNestedOperationsNotTheSame.

@Test
public void testNestedOperationsNotTheSame() {
    Unit<Mass> m1 = MICRO(GRAM);
    Unit<Mass> m2 = GRAM.divide(1000).divide(2000);
    UnitConverter c1 = m1.getConverterTo(m2);
    List steps1 = c1.getConversionSteps();
    UnitConverter c2 = m2.getConverterTo(m1);
    List steps2 = c2.getConversionSteps();
    assertNotEquals(c1, c2);
    assertNotEquals(m1, m2);
}
Also used : Mass(javax.measure.quantity.Mass) UnitConverter(javax.measure.UnitConverter) List(java.util.List) Test(org.junit.jupiter.api.Test)

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