Search in sources :

Example 1 with ScalarUnit

use of org.alien4cloud.tosca.normative.primitives.ScalarUnit in project alien4cloud by alien4cloud.

the class PropertyValueService method getValueInUnit.

public static String getValueInUnit(String propertyValue, String unit, boolean ceil, String toscaType) {
    IPropertyType type = ToscaTypes.fromYamlTypeName(toscaType);
    if (type instanceof ScalarType) {
        try {
            ScalarUnit scalarUnit = ((ScalarType) type).parse(propertyValue);
            double convertedValue = scalarUnit.convert(unit);
            if (ceil) {
                convertedValue = Math.ceil(convertedValue);
            }
            return format(convertedValue);
        } catch (InvalidPropertyValueException e) {
            log.error("e");
            throw new InvalidArgumentException(e.getMessage());
        }
    }
    throw new InvalidArgumentException("Type is not a scalar type");
}
Also used : InvalidArgumentException(alien4cloud.exception.InvalidArgumentException) InvalidPropertyValueException(org.alien4cloud.tosca.exceptions.InvalidPropertyValueException) ScalarType(org.alien4cloud.tosca.normative.types.ScalarType) IPropertyType(org.alien4cloud.tosca.normative.types.IPropertyType) ScalarUnit(org.alien4cloud.tosca.normative.primitives.ScalarUnit)

Aggregations

InvalidArgumentException (alien4cloud.exception.InvalidArgumentException)1 InvalidPropertyValueException (org.alien4cloud.tosca.exceptions.InvalidPropertyValueException)1 ScalarUnit (org.alien4cloud.tosca.normative.primitives.ScalarUnit)1 IPropertyType (org.alien4cloud.tosca.normative.types.IPropertyType)1 ScalarType (org.alien4cloud.tosca.normative.types.ScalarType)1