Search in sources :

Example 1 with Unit

use of com.squarespace.cldr.units.Unit in project template-compiler by Squarespace.

the class UnitUtils method parseUnitList.

private static Unit[] parseUnitList(String value) {
    String[] parts = value.split(",");
    UnitCategory category = null;
    Unit[] units = new Unit[parts.length];
    for (int i = 0; i < parts.length; i++) {
        Unit unit = Unit.fromIdentifier(parts[i]);
        // Bail out on invalid or incompatible units
        if (unit == null) {
            return null;
        }
        if (category == null) {
            category = unit.category();
        } else if (unit.category() != category) {
            return null;
        }
        units[i] = unit;
    }
    return units;
}
Also used : Unit(com.squarespace.cldr.units.Unit) UnitCategory(com.squarespace.cldr.units.UnitCategory)

Example 2 with Unit

use of com.squarespace.cldr.units.Unit in project template-compiler by Squarespace.

the class UnitFormatter method apply.

@Override
public void apply(Context ctx, Arguments args, Variables variables) throws CodeExecuteException {
    Variable var = variables.first();
    BigDecimal amount = GeneralUtils.nodeToBigDecimal(var.node());
    if (amount == null) {
        var.setMissing();
        return;
    }
    UnitFormatterOptions opts = (UnitFormatterOptions) args.getOpaque();
    CLDR.Locale locale = ctx.cldrLocale();
    Unit inputUnit = opts.inputUnit;
    Unit exactUnit = opts.exactUnit;
    Unit[] exactUnits = opts.exactUnits;
    String compact = opts.compact;
    Unit[] sequence = opts.sequence;
    // We default to output unit.
    if (inputUnit == null && exactUnit == null) {
        UnitValue value = new UnitValue(amount, null);
        StringBuilder buf = new StringBuilder();
        NumberFormatter formatter = CLDR_INSTANCE.getNumberFormatter(locale);
        formatter.formatUnit(value, buf, opts.options);
        var.set(buf);
        return;
    }
    // At least one argument was provided. We will try to infer the others where possible.
    UnitConverter converter = CLDR_INSTANCE.getUnitConverter(locale);
    UnitFactorSet factorSet = null;
    if (compact != null) {
        // First see if compact format matches a direct unit conversion (e.g. temperature, speed)
        Unit unit = selectExactUnit(compact, converter);
        if (unit != null) {
            exactUnit = unit;
        } else if (opts.factorSet != null) {
            // Compact format might correspond to a factor set (e.g. digital bits, bytes).
            factorSet = opts.factorSet;
        } else {
            factorSet = selectFactorSet(compact, converter);
            opts.factorSet = factorSet;
        }
    } else if (exactUnits != null && exactUnits.length > 0) {
        if (opts.factorSet != null) {
            factorSet = opts.factorSet;
        } else {
            UnitCategory category = exactUnits[0].category();
            factorSet = converter.getFactorSet(category, exactUnits);
            opts.factorSet = factorSet;
        }
    } else if (sequence != null && sequence.length > 0) {
        if (opts.factorSet != null) {
            factorSet = opts.factorSet;
        } else {
            UnitCategory category = sequence[0].category();
            factorSet = converter.getFactorSet(category, sequence);
            opts.factorSet = factorSet;
        }
    }
    // Make sure we know what the input units are.
    if (inputUnit == null) {
        inputUnit = inputFromExactUnit(exactUnit, converter);
    }
    // == CONVERSION ==
    UnitValue value = new UnitValue(amount, inputUnit);
    // In sequence mode this will get set below.
    List<UnitValue> values = null;
    if (exactUnit != null) {
        // Convert to exact units using the requested unit.
        value = converter.convert(value, exactUnit);
    } else if (factorSet == null) {
        // Convert directly to "best" unit using the default built-in factor sets.
        value = converter.convert(value);
    } else if (compact != null || exactUnits != null) {
        // Use the factor set to build a compact form.
        value = converter.convert(value, factorSet);
    } else if (sequence != null) {
        // Use the factor set to produce a sequence.
        values = converter.sequence(value, factorSet);
    }
    // == FORMATTING ==
    StringBuilder buf = new StringBuilder();
    NumberFormatter formatter = CLDR_INSTANCE.getNumberFormatter(locale);
    if (values == null) {
        formatter.formatUnit(value, buf, opts.options);
    } else {
        formatter.formatUnits(values, buf, opts.options);
    }
    var.set(buf);
}
Also used : Variable(com.squarespace.template.Variable) UnitFormatterOptions(com.squarespace.template.plugins.platform.i18n.UnitUtils.UnitFormatterOptions) CLDR(com.squarespace.cldr.CLDR) UnitValue(com.squarespace.cldr.units.UnitValue) Unit(com.squarespace.cldr.units.Unit) BigDecimal(java.math.BigDecimal) UnitFactorSet(com.squarespace.cldr.units.UnitFactorSet) UnitConverter(com.squarespace.cldr.units.UnitConverter) UnitCategory(com.squarespace.cldr.units.UnitCategory) NumberFormatter(com.squarespace.cldr.numbers.NumberFormatter)

Aggregations

Unit (com.squarespace.cldr.units.Unit)2 UnitCategory (com.squarespace.cldr.units.UnitCategory)2 CLDR (com.squarespace.cldr.CLDR)1 NumberFormatter (com.squarespace.cldr.numbers.NumberFormatter)1 UnitConverter (com.squarespace.cldr.units.UnitConverter)1 UnitFactorSet (com.squarespace.cldr.units.UnitFactorSet)1 UnitValue (com.squarespace.cldr.units.UnitValue)1 Variable (com.squarespace.template.Variable)1 UnitFormatterOptions (com.squarespace.template.plugins.platform.i18n.UnitUtils.UnitFormatterOptions)1 BigDecimal (java.math.BigDecimal)1