Search in sources :

Example 1 with UnitConverter

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);
}
Also used : UnitConverter(javax.measure.converter.UnitConverter) BigDecimal(java.math.BigDecimal)

Example 2 with UnitConverter

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;
}
Also used : ConversionException(javax.measure.converter.ConversionException) UnitConverter(javax.measure.converter.UnitConverter)

Example 3 with UnitConverter

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);
    }
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) UnitConverter(javax.measure.converter.UnitConverter) ConvertingRenderer(com.serotonin.m2m2.view.text.ConvertingRenderer) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue) TextRenderer(com.serotonin.m2m2.view.text.TextRenderer)

Example 4 with UnitConverter

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);
}
Also used : UnitConverter(javax.measure.converter.UnitConverter) Unit(org.tensorics.core.units.Unit) JScienceUnit(org.tensorics.core.units.JScienceUnit)

Example 5 with UnitConverter

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);
}
Also used : UnitConverter(javax.measure.converter.UnitConverter)

Aggregations

UnitConverter (javax.measure.converter.UnitConverter)6 InvalidArgumentException (com.serotonin.InvalidArgumentException)1 PointValueFacade (com.serotonin.m2m2.rt.dataImage.PointValueFacade)1 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)1 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)1 NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)1 DiscreteTimeSeries (com.serotonin.m2m2.util.chart.DiscreteTimeSeries)1 NumericTimeSeries (com.serotonin.m2m2.util.chart.NumericTimeSeries)1 PointTimeSeriesCollection (com.serotonin.m2m2.util.chart.PointTimeSeriesCollection)1 ConvertingRenderer (com.serotonin.m2m2.view.text.ConvertingRenderer)1 TextRenderer (com.serotonin.m2m2.view.text.TextRenderer)1 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)1 AnalogLowLimitDetectorVO (com.serotonin.m2m2.vo.event.detector.AnalogLowLimitDetectorVO)1 Color (java.awt.Color)1 Paint (java.awt.Paint)1 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 TimeZone (java.util.TimeZone)1