use of javax.measure.converter.UnitConverter in project data-prep by Talend.
the class TemperatureImpl method convertTo.
public TemperatureImpl convertTo(Unit<javax.measure.quantity.Temperature> targetUnit) {
UnitConverter converter = tempUnit.getConverterTo(targetUnit);
BigDecimal convertedValue = converter.convert(value, MathContext.DECIMAL128);
return new TemperatureImpl(convertedValue, targetUnit);
}
use of javax.measure.converter.UnitConverter in project molgenis by molgenis.
the class NumericAlgorithmGenerator method generateUnitConversionAlgorithm.
String generateUnitConversionAlgorithm(Attribute targetAttribute, EntityType targetEntityType, Attribute sourceAttribute, EntityType sourceEntityType) {
String algorithm = null;
Unit<? extends Quantity> targetUnit = unitResolver.resolveUnit(targetAttribute, targetEntityType);
Unit<? extends Quantity> sourceUnit = unitResolver.resolveUnit(sourceAttribute, sourceEntityType);
if (sourceUnit != null) {
if (targetUnit != null && !sourceUnit.equals(targetUnit)) {
// if units are convertible, create convert algorithm
UnitConverter unitConverter;
try {
unitConverter = sourceUnit.getConverterTo(targetUnit);
} catch (ConversionException e) {
unitConverter = null;
// algorithm sets source unit and assigns source value to target
algorithm = String.format("$('%s').unit('%s').value();", sourceAttribute.getName(), sourceUnit.toString());
}
if (unitConverter != null) {
// algorithm sets source unit and assigns value converted to target unit to target
algorithm = String.format("$('%s').unit('%s').toUnit('%s').value();", sourceAttribute.getName(), sourceUnit.toString(), targetUnit.toString());
}
} else {
// algorithm sets source unit and assigns source value to target
algorithm = String.format("$('%s').unit('%s').value();", sourceAttribute.getName(), sourceUnit.toString());
}
}
if (algorithm == null) {
// algorithm assigns source value to target
algorithm = String.format("$('%s').value();", sourceAttribute.getName());
}
return algorithm;
}
use of javax.measure.converter.UnitConverter in project ma-core-public by infiniteautomation.
the class BaseDwr method setPointImpl.
protected void setPointImpl(DataPointVO point, String valueStr, SetPointSource source) {
if (point == null)
return;
if (valueStr == null)
Common.runtimeManager.relinquish(point.getId());
else {
// Convert the string value into an object.
DataValue value = DataValue.stringToValue(valueStr, point.getPointLocator().getDataTypeId());
// do reverse conversion of renderer
TextRenderer tr = point.getTextRenderer();
if (point.getPointLocator().getDataTypeId() == DataTypes.NUMERIC && tr instanceof ConvertingRenderer) {
ConvertingRenderer cr = (ConvertingRenderer) tr;
UnitConverter converter = cr.getRenderedUnit().getConverterTo(cr.getUnit());
double convertedValue = converter.convert(value.getDoubleValue());
value = new NumericValue(convertedValue);
}
Common.runtimeManager.setDataPointValue(point.getId(), value, source);
}
}
use of javax.measure.converter.UnitConverter in project tensorics-core by tensorics.
the class JScienceQuantificationStrategy method toStandardUnit.
private <S extends ErronousValue<T> & Quantified> ErronousValue<T> toStandardUnit(S scalar) {
Unit unit = scalar.unit();
throwIfNotJScience(unit);
UnitConverter converter = extract(unit).toStandardUnit();
return convert(converter, scalar);
}
use of javax.measure.converter.UnitConverter in project tensorics-core by tensorics.
the class JScienceQuantificationStrategy method convertValueToUnit.
@Override
public <V extends ErronousValue<T> & Quantified> ErronousValue<T> convertValueToUnit(V value, Unit targetUnit) {
throwIfNotJScience(value.unit(), targetUnit);
UnitConverter converter = extract(value.unit()).getConverterTo(extract(targetUnit));
return convert(converter, value);
}
Aggregations