Search in sources :

Example 41 with Unit

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

the class DefaultQuantityFormat method format.

@Override
public Appendable format(Quantity measure, Appendable dest) throws IOException {
    Unit unit = measure.getUnit();
    dest.append(measure.getValue().toString());
    if (measure.getUnit().equals(AbstractUnit.ONE))
        return dest;
    dest.append(' ');
    return SimpleUnitFormat.getInstance().format(unit, dest);
}
Also used : Unit(javax.measure.Unit) AbstractUnit(tec.uom.se.AbstractUnit)

Example 42 with Unit

use of javax.measure.Unit in project indriya by unitsofmeasurement.

the class UnitFormatTest method testFormatLocal.

@Test
public void testFormatLocal() {
    final UnitFormat format = LocalUnitFormat.getInstance();
    final Appendable a = new StringBuilder();
    try {
        format.format(METRE, a);
    } catch (IOException e) {
        fail(e.getMessage());
    }
    assertEquals(METRE, sut.getUnit());
    assertEquals("m", a.toString());
    final Appendable a2 = new StringBuilder();
    @SuppressWarnings("unchecked") Unit<Speed> v = (Unit<Speed>) METRE.divide(SECOND);
    try {
        format.format(v, a2);
    } catch (IOException e) {
        fail(e.getMessage());
    }
    assertEquals("m/s", a2.toString());
}
Also used : Speed(javax.measure.quantity.Speed) UnitFormat(javax.measure.format.UnitFormat) LocalUnitFormat(tech.units.indriya.format.LocalUnitFormat) SimpleUnitFormat(tech.units.indriya.format.SimpleUnitFormat) IOException(java.io.IOException) Unit(javax.measure.Unit) Test(org.junit.jupiter.api.Test)

Example 43 with Unit

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

the class LocalUnitFormatParser method AddExpr.

public final Unit AddExpr() throws TokenException {
    Unit result = AbstractUnit.ONE;
    Number n1 = null;
    Token sign1 = null;
    Number n2 = null;
    Token sign2 = null;
    if (jj_2_1(2147483647)) {
        n1 = NumberExpr();
        sign1 = Sign();
    } else {
    }
    result = MulExpr();
    switch((nextTokenIndex == -1) ? jj_ntk() : nextTokenIndex) {
        case PLUS:
        case MINUS:
            sign2 = Sign();
            n2 = NumberExpr();
            break;
        default:
            laA[1] = genInt;
    }
    if (n1 != null) {
        if (sign1.image.equals("-")) {
            result = result.multiply(-1);
        }
        result = result.shift(n1.doubleValue());
    }
    if (n2 != null) {
        double offset = n2.doubleValue();
        if (sign2.image.equals("-")) {
            offset = -offset;
        }
        result = result.shift(offset);
    }
    {
        return result;
    }
}
Also used : Unit(javax.measure.Unit) AbstractUnit(tec.uom.se.AbstractUnit)

Example 44 with Unit

use of javax.measure.Unit in project uom-se 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(tec.uom.se.unit.MetricPrefix) Unit(javax.measure.Unit) AbstractUnit(tec.uom.se.AbstractUnit)

Example 45 with Unit

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

the class UnitFormatTest method testFormatLocal.

@Test
public void testFormatLocal() {
    final UnitFormat format = LocalUnitFormat.getInstance();
    final Appendable a = new StringBuilder();
    try {
        format.format(METRE, a);
    } catch (IOException e) {
        fail(e.getMessage());
    }
    assertEquals(METRE, sut.getUnit());
    assertEquals("m", a.toString());
    final Appendable a2 = new StringBuilder();
    @SuppressWarnings("unchecked") Unit<Speed> v = (Unit<Speed>) METRE.divide(SECOND);
    try {
        format.format(v, a2);
    } catch (IOException e) {
        fail(e.getMessage());
    }
    assertEquals("m/s", a2.toString());
}
Also used : Speed(javax.measure.quantity.Speed) UnitFormat(javax.measure.format.UnitFormat) IOException(java.io.IOException) Unit(javax.measure.Unit) Test(org.junit.Test)

Aggregations

Unit (javax.measure.Unit)52 AbstractUnit (tech.units.indriya.AbstractUnit)12 AbstractUnit (tec.uom.se.AbstractUnit)11 UnitConverter (javax.measure.UnitConverter)5 Test (org.junit.Test)5 CoordinateSystem (com.revolsys.geometry.cs.CoordinateSystem)4 ProjectedCoordinateSystem (com.revolsys.geometry.cs.ProjectedCoordinateSystem)4 BigDecimal (java.math.BigDecimal)4 Length (javax.measure.quantity.Length)4 CoordinateSystemAxis (org.opengis.referencing.cs.CoordinateSystemAxis)4 GeographicCoordinateSystem (com.revolsys.geometry.cs.GeographicCoordinateSystem)3 Format (java.text.Format)3 NumberFormat (java.text.NumberFormat)3 IncommensurableException (javax.measure.IncommensurableException)3 Angle (javax.measure.quantity.Angle)3 TestUnit (javax.measure.test.TestUnit)3 Test (org.junit.jupiter.api.Test)3 IdentifiedObject (org.opengis.referencing.IdentifiedObject)3 CoordinateSystem (org.opengis.referencing.cs.CoordinateSystem)3 ChainedCoordinatesOperation (com.revolsys.geometry.cs.projection.ChainedCoordinatesOperation)2