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);
}
}
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);
}
}
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);
}
}
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);
}
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);
}
Aggregations