Search in sources :

Example 51 with UnitConverter

use of javax.measure.UnitConverter in project uom-se by unitsofmeasurement.

the class PrefixTest method testNestedOperationsNotTheSame.

@Test
public void testNestedOperationsNotTheSame() {
    Unit<Mass> m1 = MICRO(GRAM);
    Unit<Mass> m2 = GRAM.divide(1000).divide(2000);
    UnitConverter c1 = m1.getConverterTo(m2);
    List steps1 = c1.getConversionSteps();
    UnitConverter c2 = m2.getConverterTo(m1);
    List steps2 = c2.getConversionSteps();
    assertNotEquals(c1, c2);
    assertNotEquals(m1, m2);
}
Also used : Mass(javax.measure.quantity.Mass) UnitConverter(javax.measure.UnitConverter) List(java.util.List) Test(org.junit.Test)

Example 52 with UnitConverter

use of javax.measure.UnitConverter in project uom-se by unitsofmeasurement.

the class UnitsTest method testConverterToSI.

/**
 * Test method for {@link javax.measure.Unit#toMetric()}.
 */
// @Test
// public void testToMetric() {
// AbstractUnit<? extends QuantityAmount> su = (AbstractUnit<? extends
// QuantityAmount>) one.toMetric();
// assertTrue(su.isUnscaledMetric());
// }
/**
 * Test method for {@link javax.measure.Unit#getConverterTo}.
 */
@Test
public void testConverterToSI() {
    Double factor = 10.0;
    UnitConverter converter = one.getConverterTo(one);
    Double result = converter.convert(factor.doubleValue());
    assertEquals(result, factor);
    logger.log(Level.FINER, result.toString());
}
Also used : UnitConverter(javax.measure.UnitConverter) Test(org.junit.Test)

Example 53 with UnitConverter

use of javax.measure.UnitConverter in project n2a by frothga.

the class ExportJob method morphologyUnits.

public String morphologyUnits(String value) {
    value = value.trim();
    int unitIndex = UnitValue.findUnits(value);
    // no number
    if (unitIndex == 0)
        return "0" + value;
    // no unit
    if (unitIndex >= value.length())
        return value;
    String unitString = value.substring(unitIndex).trim();
    value = value.substring(0, unitIndex);
    Unit<?> unit = UCUM.parse(unitString);
    double v = 0;
    try {
        v = Double.valueOf(value);
        UnitConverter converter = unit.getConverterToAny(um);
        v = converter.convert(v);
    } catch (Exception error) {
    }
    unitsUsed.put(um, "um");
    // no unit string at end of number, because morphology units are always in um
    return print(v);
}
Also used : UnitConverter(javax.measure.UnitConverter) UnconvertibleException(javax.measure.UnconvertibleException) IncommensurableException(javax.measure.IncommensurableException)

Example 54 with UnitConverter

use of javax.measure.UnitConverter in project n2a by frothga.

the class ExportJob method appendUnits.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void appendUnits(boolean assumeNML) {
    // Purge pure dimensions (added independent of specific unit) if they are standard and NML dimensions are available.
    if (assumeNML) {
        List<String> keys = new ArrayList<String>(dimensionsUsed.keySet());
        for (String key : keys) {
            Dimension d = dimensionsUsed.get(key);
            if (dimensionsNML.containsKey(d))
                dimensionsUsed.remove(key);
        }
    }
    for (Entry<Unit<?>, String> e : unitsUsed.entrySet()) {
        Unit<?> key = e.getKey();
        if (assumeNML && unitsNML.containsKey(key))
            continue;
        String symbol = e.getValue();
        Dimension dimension = key.getDimension();
        String dimensionName = dimensionsNML.get(dimension);
        if (// Not a standard NML dimension
        dimensionName == null) {
            dimensionName = dimension.toString();
            dimensionsUsed.put(dimensionName, dimension);
        } else if (// Is a standard NML dimension, so only add if don't have NML dimensions available.
        !assumeNML) {
            dimensionsUsed.put(dimensionName, dimension);
        }
        Element unit = addElement("Unit", elements);
        unit.setAttribute("symbol", symbol);
        unit.setAttribute("dimension", dimensionName);
        // Determine offset
        Unit systemUnit = key.getSystemUnit();
        UnitConverter converter = key.getConverterTo(systemUnit);
        double offset = converter.convert(Integer.valueOf(0)).doubleValue();
        if (offset != 0)
            unit.setAttribute("offset", String.valueOf(offset));
        // Determine power*scale
        double scale = converter.convert(Integer.valueOf(1)).doubleValue() - offset;
        int power = (int) Math.round(Math.log(scale) / Math.log(10));
        if (Math.abs(scale - Math.pow(10, power)) < epsilon) {
            unit.setAttribute("power", String.valueOf(power));
        } else {
            unit.setAttribute("scale", String.valueOf(scale));
        }
    }
    for (Entry<String, Dimension> d : dimensionsUsed.entrySet()) {
        String name = d.getKey();
        Dimension value = d.getValue();
        Element dimension = addElement("Dimension", elements);
        dimension.setAttribute("name", name);
        Map<? extends Dimension, Integer> bases = value.getBaseDimensions();
        if (bases == null) {
            Map<Dimension, Integer> temp = new HashMap<Dimension, Integer>();
            temp.put(value, 1);
            bases = temp;
        }
        for (Entry<? extends Dimension, Integer> e : bases.entrySet()) {
            String base = e.getKey().toString().substring(1, 2).toLowerCase();
            if (base.equals("θ"))
                base = "k";
            dimension.setAttribute(base, e.getValue().toString());
        }
    }
}
Also used : HashMap(java.util.HashMap) AccessElement(gov.sandia.n2a.language.AccessElement) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Dimension(javax.measure.Dimension) Unit(javax.measure.Unit) UnitConverter(javax.measure.UnitConverter)

Aggregations

UnitConverter (javax.measure.UnitConverter)54 IncommensurableException (javax.measure.IncommensurableException)18 UnconvertibleException (javax.measure.UnconvertibleException)13 Test (org.junit.jupiter.api.Test)11 Test (org.junit.Test)9 RationalConverter (tech.units.indriya.function.RationalConverter)7 Unit (javax.measure.Unit)5 List (java.util.List)4 Mass (javax.measure.quantity.Mass)4 Date (java.util.Date)3 DateFormat (java.text.DateFormat)2 DecimalFormat (java.text.DecimalFormat)2 Format (java.text.Format)2 NumberFormat (java.text.NumberFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2 HashMap (java.util.HashMap)2 Dimension (javax.measure.Dimension)2 CompoundFormat (org.apache.sis.io.CompoundFormat)2 Angle (org.apache.sis.measure.Angle)2 AngleFormat (org.apache.sis.measure.AngleFormat)2