Search in sources :

Example 1 with MultiplyConverter

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

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

the class MultiplyConverterTest method testEqualityOfTwoLogConverter.

@Test
public void testEqualityOfTwoLogConverter() {
    assertFalse(converter.equals(null));
    assertEquals(new MultiplyConverter(2), converter);
}
Also used : MultiplyConverter(tech.units.indriya.function.MultiplyConverter) Test(org.junit.jupiter.api.Test)

Example 3 with MultiplyConverter

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

the class MultiplyConverterTest method inverseTest.

@Test
public void inverseTest() {
    assertNotNull(converter.inverse());
    assertEquals(new MultiplyConverter(0.5), converter.inverse());
}
Also used : MultiplyConverter(tech.units.indriya.function.MultiplyConverter) Test(org.junit.jupiter.api.Test)

Example 4 with MultiplyConverter

use of tech.units.indriya.function.MultiplyConverter in project n2a by frothga.

the class ImportJob method unit.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void unit(Node node) {
    String symbol = getAttribute(node, "symbol");
    String dimension = getAttribute(node, "dimension");
    int power = getAttribute(node, "power", 0);
    double scale = getAttribute(node, "scale", 1.0);
    double offset = getAttribute(node, "offset", 0.0);
    Unit unit = dimensions.get(dimension);
    if (unit == null)
        unit = nmlDimensions.get(dimension);
    // fall back, but in general something is broken about the file
    if (unit == null)
        unit = AbstractUnit.ONE;
    if (power > 0)
        unit = unit.transform(MultiplyConverter.ofRational(BigInteger.TEN.pow(power), BigInteger.ONE));
    else if (power < 0)
        unit = unit.transform(MultiplyConverter.ofRational(BigInteger.ONE, BigInteger.TEN.pow(-power)));
    else {
        if (scale == 1.0) {
            unit = unit.shift(offset);
        } else {
            // UCUM only allows rational numbers, so convert scale
            MultiplyConverter ratio = null;
            if (scale < 1.0) {
                // Attempt to find a simple ratio of 1/integer
                double inverse = 1.0 / scale;
                long integer = Math.round(inverse);
                if (Math.abs(inverse - integer) < epsilon)
                    ratio = MultiplyConverter.ofRational(1, integer);
            }
            if (ratio == null) {
                String s = getAttribute(node, "scale").toLowerCase();
                String[] pieces = s.split("e");
                int shift = 0;
                if (pieces.length > 1)
                    shift = Integer.valueOf(pieces[1]);
                pieces = pieces[0].split(".");
                if (pieces.length > 1) {
                    shift -= pieces[1].length();
                    s = pieces[0] + pieces[1];
                }
                BigInteger numerator = new BigInteger(s);
                BigInteger denominator = new BigDecimal(10).pow(shift).toBigInteger();
                ratio = MultiplyConverter.ofRational(numerator, denominator);
            }
            unit = unit.transform(ratio).shift(offset);
        }
    }
    // Since LEMS and NeuroML tend to follow certain naming practices, we may be able to retrieve
    // a more parsimonious unit based on direct name translation.
    String tempName = symbol;
    tempName = tempName.replace("_per_", "/");
    tempName = tempName.replace("per_", "/");
    tempName = tempName.replace("_", ".");
    tempName = tempName.replace("ohm", "Ohm");
    tempName = tempName.replace("hour", "h");
    Unit temp = null;
    try {
        temp = UCUM.parse(tempName);
    } catch (MeasurementParseException | TokenException e) {
    }
    if (// found a unit with matching dimension ...
    temp != null && temp.isCompatible(unit)) {
        Number tempScale = temp.getConverterTo(temp.getSystemUnit()).convert(Integer.valueOf(1));
        Number unitScale = temp.getConverterTo(unit.getSystemUnit()).convert(Integer.valueOf(1));
        if (// ... and matching scale ...
        tempScale.equals(unitScale)) {
            int unitLength = UCUM.format(unit).length();
            int tempLength = UCUM.format(temp).length();
            if (// ... and at least as parsimonious
            tempLength <= unitLength) {
                unit = temp;
                // Update dimension if this is directly equivalent, but strictly more parsimonious
                if (power == 0 && scale == 1 && offset == 0 && tempLength < unitLength) {
                    dimensions.put(dimension, temp);
                }
            }
        }
    }
    ExpressionParser.namedUnits.put(symbol, unit);
}
Also used : TokenException(systems.uom.ucum.internal.format.TokenException) BigInteger(java.math.BigInteger) Unit(javax.measure.Unit) TransformedUnit(tech.units.indriya.unit.TransformedUnit) AbstractUnit(tech.units.indriya.AbstractUnit) BigDecimal(java.math.BigDecimal) MultiplyConverter(tech.units.indriya.function.MultiplyConverter) MeasurementParseException(javax.measure.format.MeasurementParseException)

Aggregations

MultiplyConverter (tech.units.indriya.function.MultiplyConverter)4 Test (org.junit.jupiter.api.Test)2 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 Unit (javax.measure.Unit)1 MeasurementParseException (javax.measure.format.MeasurementParseException)1 TokenException (systems.uom.ucum.internal.format.TokenException)1 AbstractUnit (tech.units.indriya.AbstractUnit)1 AddConverter (tech.units.indriya.function.AddConverter)1 RationalConverter (tech.units.indriya.function.RationalConverter)1 MetricPrefix (tech.units.indriya.unit.MetricPrefix)1 TransformedUnit (tech.units.indriya.unit.TransformedUnit)1