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