Search in sources :

Example 1 with RationalConverter

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

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

the class PrefixTest method testKibi.

@Test
public void testKibi() {
    final UnitConverter expected = new RationalConverter(128, 125);
    final UnitConverter actual = KIBI(METRE).getConverterTo(KILO(METRE));
    assertEquals("Ki", KIBI.getSymbol());
    assertEquals(expected, actual);
}
Also used : RationalConverter(tech.units.indriya.function.RationalConverter) UnitConverter(javax.measure.UnitConverter) Test(org.junit.jupiter.api.Test)

Example 3 with RationalConverter

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

the class PrefixTest method testZebi.

@Test
public void testZebi() {
    final UnitConverter expected = new RationalConverter(1, 6835937500000000000L);
    final UnitConverter actual = ZEBI(GRAM).getConverterTo(ZETTA(GRAM));
    assertEquals(expected, actual);
}
Also used : RationalConverter(tech.units.indriya.function.RationalConverter) UnitConverter(javax.measure.UnitConverter) Test(org.junit.jupiter.api.Test)

Example 4 with RationalConverter

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

the class PrefixTest method testYobi.

@Test
public void testYobi() {
    final UnitConverter expected = new RationalConverter(BigInteger.ONE, BigDecimal.valueOf(7812500000000000000000D).toBigInteger());
    final UnitConverter actual = YOBI(GRAM).getConverterTo(YOTTA(GRAM));
    assertEquals(expected, actual);
}
Also used : RationalConverter(tech.units.indriya.function.RationalConverter) UnitConverter(javax.measure.UnitConverter) Test(org.junit.jupiter.api.Test)

Example 5 with RationalConverter

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

the class EBNFFormatTest method testTransformed.

@Test
public void testTransformed() {
    final String ANGSTROEM_SYM = "\u212B";
    final Unit<Length> ANGSTROEM = new TransformedUnit<Length>(ANGSTROEM_SYM, METRE, METRE, new RationalConverter(BigInteger.ONE, BigInteger.TEN.pow(10)));
    final String s = format.format(ANGSTROEM);
    assertEquals(ANGSTROEM_SYM, s);
}
Also used : Length(javax.measure.quantity.Length) RationalConverter(tech.units.indriya.function.RationalConverter) TransformedUnit(tech.units.indriya.unit.TransformedUnit) Test(org.junit.jupiter.api.Test)

Aggregations

RationalConverter (tech.units.indriya.function.RationalConverter)10 Test (org.junit.jupiter.api.Test)9 UnitConverter (javax.measure.UnitConverter)7 Length (javax.measure.quantity.Length)2 TransformedUnit (tech.units.indriya.unit.TransformedUnit)2 AddConverter (tech.units.indriya.function.AddConverter)1 MultiplyConverter (tech.units.indriya.function.MultiplyConverter)1 MetricPrefix (tech.units.indriya.unit.MetricPrefix)1