Search in sources :

Example 1 with MetricPrefix

use of tech.units.indriya.unit.MetricPrefix in project indriya by unitsofmeasurement.

the class LocalUnitFormat method formatConverter.

/**
 * Formats the given converter to the given StringBuffer and returns the operator precedence of the converter's mathematical operation. This is the
 * default implementation, which supports all built-in UnitConverter implementations. Note that it recursively calls itself in the case of a
 * {@link javax.measure.converter.UnitConverter.Compound Compound} converter.
 *
 * @param converter
 *          the converter to be formatted
 * @param continued
 *          <code>true</code> if the converter expression should begin with an operator, otherwise <code>false</code>.
 * @param unitPrecedence
 *          the operator precedence of the operation expressed by the unit being modified by the given converter.
 * @param buffer
 *          the <code>StringBuffer</code> to append to.
 * @return the operator precedence of the given UnitConverter
 */
private int formatConverter(UnitConverter converter, boolean continued, int unitPrecedence, StringBuilder buffer) {
    MetricPrefix prefix = symbolMap.getPrefix(converter);
    if ((prefix != null) && (unitPrecedence == NOOP_PRECEDENCE)) {
        buffer.insert(0, symbolMap.getSymbol(prefix));
        return NOOP_PRECEDENCE;
    } else if (converter instanceof AddConverter) {
        if (unitPrecedence < ADDITION_PRECEDENCE) {
            buffer.insert(0, '(');
            buffer.append(')');
        }
        double offset = ((AddConverter) converter).getOffset();
        if (offset < 0) {
            buffer.append("-");
            offset = -offset;
        } else if (continued) {
            buffer.append("+");
        }
        long lOffset = (long) offset;
        if (lOffset == offset) {
            buffer.append(lOffset);
        } else {
            buffer.append(offset);
        }
        return ADDITION_PRECEDENCE;
    } else if (converter instanceof MultiplyConverter) {
        if (unitPrecedence < PRODUCT_PRECEDENCE) {
            buffer.insert(0, '(');
            buffer.append(')');
        }
        if (continued) {
            buffer.append(MIDDLE_DOT);
        }
        double factor = ((MultiplyConverter) converter).getFactor();
        long lFactor = (long) factor;
        if (lFactor == factor) {
            buffer.append(lFactor);
        } else {
            buffer.append(factor);
        }
        return PRODUCT_PRECEDENCE;
    } else if (converter instanceof RationalConverter) {
        if (unitPrecedence < PRODUCT_PRECEDENCE) {
            buffer.insert(0, '(');
            buffer.append(')');
        }
        RationalConverter rationalConverter = (RationalConverter) converter;
        if (!rationalConverter.getDividend().equals(BigInteger.ONE)) {
            if (continued) {
                buffer.append(MIDDLE_DOT);
            }
            buffer.append(rationalConverter.getDividend());
        }
        if (!rationalConverter.getDivisor().equals(BigInteger.ONE)) {
            buffer.append('/');
            buffer.append(rationalConverter.getDivisor());
        }
        return PRODUCT_PRECEDENCE;
    } else {
        // All other converter type (e.g. exponential) we use the
        // string representation.
        buffer.insert(0, converter.toString() + "(");
        buffer.append(")");
        return EXPONENT_PRECEDENCE;
    }
}
Also used : MetricPrefix(tech.units.indriya.unit.MetricPrefix) AddConverter(tech.units.indriya.function.AddConverter) RationalConverter(tech.units.indriya.function.RationalConverter) MultiplyConverter(tech.units.indriya.function.MultiplyConverter)

Example 2 with MetricPrefix

use of tech.units.indriya.unit.MetricPrefix in project indriya by unitsofmeasurement.

the class UnitFormatParser method atomicExpr.

Unit<?> atomicExpr() throws TokenException {
    Unit<?> result = AbstractUnit.ONE;
    // Unit<?> temp = AbstractUnit.ONE;
    Number n = null;
    Token token = null;
    switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
        case INTEGER:
        case FLOATING_POINT:
            n = numberExpr();
            if (n instanceof Integer) {
                {
                    if (true)
                        return result.multiply(n.intValue());
                }
            } else {
                {
                    if (true)
                        return result.multiply(n.doubleValue());
                }
            }
        // break;
        case UNIT_IDENTIFIER:
            token = jj_consume_token(UNIT_IDENTIFIER);
            Unit<?> unit = symbols.getUnit(token.image);
            if (unit == null) {
                MetricPrefix prefix = symbols.getPrefix(token.image);
                if (prefix != null) {
                    String prefixSymbol = symbols.getSymbol(prefix);
                    unit = symbols.getUnit(token.image.substring(prefixSymbol.length()));
                    if (unit != null) {
                        {
                            if (true)
                                return unit.transform(prefix.getConverter());
                        }
                    }
                }
                {
                    if (true)
                        throw new TokenException();
                }
            } else {
                {
                    if (true)
                        return unit;
                }
            }
        // break;
        case OPEN_PAREN:
            jj_consume_token(OPEN_PAREN);
            result = addExpr();
            jj_consume_token(CLOSE_PAREN);
            {
                if (true)
                    return result;
            }
        // break;
        default:
            jj_la1[10] = jj_gen;
            jj_consume_token(-1);
            throw new TokenException();
    }
// throw new Error("Missing return statement in function");
}
Also used : MetricPrefix(tech.units.indriya.unit.MetricPrefix)

Example 3 with MetricPrefix

use of tech.units.indriya.unit.MetricPrefix in project indriya by unitsofmeasurement.

the class LocalUnitFormatParser method AtomicExpr.

public final Unit AtomicExpr() throws TokenException {
    Unit result = AbstractUnit.ONE;
    Number n = null;
    Token token = null;
    switch((nextTokenIndex == -1) ? jj_ntk() : nextTokenIndex) {
        case INTEGER:
        case FLOATING_POINT:
            n = NumberExpr();
            if (n instanceof Integer) {
                {
                    return result.multiply(n.intValue());
                }
            } else {
                {
                    return result.multiply(n.doubleValue());
                }
            }
        case UNIT_IDENTIFIER:
            token = consumeToken(UNIT_IDENTIFIER);
            Unit unit = symbols.getUnit(token.image);
            if (unit == null) {
                MetricPrefix prefix = symbols.getPrefix(token.image);
                if (prefix != null) {
                    String prefixSymbol = symbols.getSymbol(prefix);
                    unit = symbols.getUnit(token.image.substring(prefixSymbol.length()));
                    if (unit != null) {
                        {
                            return unit.transform(prefix.getConverter());
                        }
                    }
                }
                {
                    throw new TokenException();
                }
            } else {
                {
                    return unit;
                }
            }
        case OPEN_PAREN:
            consumeToken(OPEN_PAREN);
            result = AddExpr();
            consumeToken(CLOSE_PAREN);
            {
                return result;
            }
        default:
            laA[10] = genInt;
            consumeToken(-1);
            throw new TokenException();
    }
}
Also used : MetricPrefix(tech.units.indriya.unit.MetricPrefix) Unit(javax.measure.Unit) AbstractUnit(tech.units.indriya.AbstractUnit)

Aggregations

MetricPrefix (tech.units.indriya.unit.MetricPrefix)3 Unit (javax.measure.Unit)1 AbstractUnit (tech.units.indriya.AbstractUnit)1 AddConverter (tech.units.indriya.function.AddConverter)1 MultiplyConverter (tech.units.indriya.function.MultiplyConverter)1 RationalConverter (tech.units.indriya.function.RationalConverter)1