Search in sources :

Example 1 with AbstractUnit

use of tec.uom.se.AbstractUnit in project uom-se by unitsofmeasurement.

the class EBNFHelper method newUnitPrecedenceInternal.

private static int newUnitPrecedenceInternal(Unit<?> unit, Appendable buffer, SymbolMap symbolMap) throws IOException {
    UnitConverter converter = null;
    boolean printSeparator = false;
    StringBuilder temp = new StringBuilder();
    int unitPrecedence = NOOP_PRECEDENCE;
    Unit<?> parentUnit = unit.getSystemUnit();
    converter = ((AbstractUnit<?>) unit).getSystemConverter();
    if (KILOGRAM.equals(parentUnit)) {
        if (unit instanceof TransformedUnit<?>) {
            // incosistency
            if (unit.equals(GRAM)) {
                return noopPrecedenceInternal(buffer, symbolMap.getSymbol(GRAM));
            } else {
                // parentUnit = GRAM;
                // converter = unit.getConverterTo((Unit) KILOGRAM);
                converter = ((TransformedUnit) unit).getConverter();
            }
        // parentUnit = GRAM;
        } else {
            converter = unit.getConverterTo((Unit) GRAM);
        }
    } else if (CUBIC_METRE.equals(parentUnit)) {
        if (converter != null) {
            parentUnit = LITRE;
        }
    }
    // https://github.com/unitsofmeasurement/si-units/issues/4
    if (unit instanceof TransformedUnit) {
        TransformedUnit transUnit = (TransformedUnit) unit;
        if (parentUnit == null)
            parentUnit = transUnit.getParentUnit();
        // String x = parentUnit.toString();
        converter = transUnit.getConverter();
    }
    unitPrecedence = formatInternal(parentUnit, temp, symbolMap);
    printSeparator = !parentUnit.equals(AbstractUnit.ONE);
    int result = ConverterFormatter.formatConverter(converter, printSeparator, unitPrecedence, temp, symbolMap);
    buffer.append(temp);
    return result;
}
Also used : TransformedUnit(tec.uom.se.unit.TransformedUnit) UnitConverter(javax.measure.UnitConverter) Unit(javax.measure.Unit) TransformedUnit(tec.uom.se.unit.TransformedUnit) BaseUnit(tec.uom.se.unit.BaseUnit) AbstractUnit(tec.uom.se.AbstractUnit) AnnotatedUnit(tec.uom.se.unit.AnnotatedUnit)

Example 2 with AbstractUnit

use of tec.uom.se.AbstractUnit in project uom-se by unitsofmeasurement.

the class ProductUnit method getSystemConverter.

@Override
public UnitConverter getSystemConverter() {
    UnitConverter converter = AbstractConverter.IDENTITY;
    for (Element e : elements) {
        if (e.unit instanceof AbstractUnit) {
            UnitConverter cvtr = ((AbstractUnit) e.unit).getSystemConverter();
            if (!(cvtr.isLinear()))
                throw new UnsupportedOperationException(e.unit + " is non-linear, cannot convert");
            if (e.root != 1)
                throw new UnsupportedOperationException(e.unit + " holds a base unit with fractional exponent");
            int pow = e.pow;
            if (pow < 0) {
                // Negative power.
                pow = -pow;
                cvtr = cvtr.inverse();
            }
            for (int j = 0; j < pow; j++) {
                converter = converter.concatenate(cvtr);
            }
        }
    }
    return converter;
}
Also used : AbstractUnit(tec.uom.se.AbstractUnit) UnitConverter(javax.measure.UnitConverter)

Aggregations

UnitConverter (javax.measure.UnitConverter)2 AbstractUnit (tec.uom.se.AbstractUnit)2 Unit (javax.measure.Unit)1 AnnotatedUnit (tec.uom.se.unit.AnnotatedUnit)1 BaseUnit (tec.uom.se.unit.BaseUnit)1 TransformedUnit (tec.uom.se.unit.TransformedUnit)1