Search in sources :

Example 1 with AddConverter

use of tech.units.indriya.function.AddConverter 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 AddConverter

use of tech.units.indriya.function.AddConverter in project indriya by unitsofmeasurement.

the class AddConverterTest method testEqualityOfTwoConverter.

@Test
public void testEqualityOfTwoConverter() {
    AddConverter addConverter = new AddConverter(10);
    assertEquals(addConverter, converter);
    assertNotNull(addConverter);
}
Also used : AddConverter(tech.units.indriya.function.AddConverter) Test(org.junit.jupiter.api.Test)

Aggregations

AddConverter (tech.units.indriya.function.AddConverter)2 Test (org.junit.jupiter.api.Test)1 MultiplyConverter (tech.units.indriya.function.MultiplyConverter)1 RationalConverter (tech.units.indriya.function.RationalConverter)1 MetricPrefix (tech.units.indriya.unit.MetricPrefix)1