Search in sources :

Example 1 with MeasurementUnit

use of com.storedobject.core.MeasurementUnit in project SODevelopment by syampillai.

the class AbstractQuantityField method convertX.

private T convertX(T value) {
    value = v(value);
    if (allowedUnits != null && !allowedUnits.contains(value.getUnit())) {
        MeasurementUnit unit = value.getUnit();
        MeasurementUnit mu = allowedUnits.stream().filter(u -> u.getType() == unit.getType()).findFirst().orElse(null);
        if (mu == null) {
            if (value.isZero()) {
                mu = allowedUnits.stream().findFirst().orElseThrow();
            } else {
                throw new SORuntimeException();
            }
        }
        // noinspection unchecked
        value = (T) value.convert(mu);
    }
    return value;
}
Also used : MeasurementUnit(com.storedobject.core.MeasurementUnit) SORuntimeException(com.storedobject.common.SORuntimeException)

Example 2 with MeasurementUnit

use of com.storedobject.core.MeasurementUnit in project SODevelopment by syampillai.

the class AbstractQuantityField method unitChanged.

private void unitChanged(MeasurementUnit unit) {
    if (unit == null || isReadOnly()) {
        return;
    }
    T q = v();
    MeasurementUnit u = q.getUnit();
    if (u == unit) {
        return;
    }
    T q1;
    try {
        // noinspection unchecked
        q1 = (T) q.convert(unit);
        if (q1 != null) {
            changed = true;
            setValue(q1);
            return;
        }
    } catch (Throwable ignored) {
    }
    // noinspection unchecked
    setValue((T) Quantity.create(q.getValue(), unit));
}
Also used : MeasurementUnit(com.storedobject.core.MeasurementUnit)

Aggregations

MeasurementUnit (com.storedobject.core.MeasurementUnit)2 SORuntimeException (com.storedobject.common.SORuntimeException)1