Search in sources :

Example 1 with IntegerRangeAttribute

use of org.openforis.idm.model.IntegerRangeAttribute in project collect by openforis.

the class AttributeValueUtils method extractNormalizedValue.

private static Object extractNormalizedValue(NumericRangeAttribute<?, ?> attr) {
    NumericRange<?> value = attr.getValue();
    RangeAttributeDefinition defn = attr.getDefinition();
    List<Unit> units = defn.getUnits();
    if (units != null && units.size() > 1) {
        Unit unit = attr.getUnit();
        Unit defaultUnit = defn.getDefaultUnit();
        if (unit != null && defaultUnit != null && unit != defaultUnit) {
            Number from = value.getFrom();
            Number to = value.getTo();
            NumericRange<?> normalizedValue;
            if (attr instanceof IntegerRangeAttribute) {
                int normalizedFrom = getNormalizedValue(from, unit, defaultUnit).intValue();
                int normalizedTo = getNormalizedValue(to, unit, defaultUnit).intValue();
                normalizedValue = new IntegerRange(normalizedFrom, normalizedTo, defaultUnit);
            } else {
                double normalizedFrom = getNormalizedValue(from, unit, defaultUnit).doubleValue();
                double normalizedTo = getNormalizedValue(to, unit, defaultUnit).doubleValue();
                normalizedValue = new RealRange(normalizedFrom, normalizedTo, defaultUnit);
            }
            return normalizedValue;
        } else {
            return value;
        }
    } else {
        return value;
    }
}
Also used : IntegerRange(org.openforis.idm.model.IntegerRange) RealRange(org.openforis.idm.model.RealRange) Unit(org.openforis.idm.metamodel.Unit) IntegerRangeAttribute(org.openforis.idm.model.IntegerRangeAttribute) RangeAttributeDefinition(org.openforis.idm.metamodel.RangeAttributeDefinition)

Aggregations

RangeAttributeDefinition (org.openforis.idm.metamodel.RangeAttributeDefinition)1 Unit (org.openforis.idm.metamodel.Unit)1 IntegerRange (org.openforis.idm.model.IntegerRange)1 IntegerRangeAttribute (org.openforis.idm.model.IntegerRangeAttribute)1 RealRange (org.openforis.idm.model.RealRange)1