Search in sources :

Example 46 with Unit

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

the class SimpleQuantityFormat method format.

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

Example 47 with Unit

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

the class SimpleQuantityFormat method parse.

@SuppressWarnings("unchecked")
@Override
public ComparableQuantity<?> parse(CharSequence csq, ParsePosition cursor) throws MeasurementParseException {
    int startDecimal = cursor.getIndex();
    while ((startDecimal < csq.length()) && Character.isWhitespace(csq.charAt(startDecimal))) {
        startDecimal++;
    }
    int endDecimal = startDecimal + 1;
    while ((endDecimal < csq.length()) && !Character.isWhitespace(csq.charAt(endDecimal))) {
        endDecimal++;
    }
    BigDecimal decimal = new BigDecimal(csq.subSequence(startDecimal, endDecimal).toString());
    cursor.setIndex(endDecimal + 1);
    Unit unit = SimpleUnitFormat.getInstance().parse(csq, cursor);
    return Quantities.getQuantity(decimal, unit);
}
Also used : Unit(javax.measure.Unit) AbstractUnit(tech.units.indriya.AbstractUnit) BigDecimal(java.math.BigDecimal)

Example 48 with Unit

use of javax.measure.Unit in project indriya 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(tech.units.indriya.unit.MetricPrefix) Unit(javax.measure.Unit) AbstractUnit(tech.units.indriya.AbstractUnit)

Example 49 with Unit

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

the class UnitsTest method testByClassTime.

@Test
public void testByClassTime() {
    Unit result = Units.getInstance().getUnit(Time.class);
    assertNotNull(result);
    assertEquals("s", result.toString());
}
Also used : Unit(javax.measure.Unit) AbstractUnit(tech.units.indriya.AbstractUnit) TransformedUnit(tech.units.indriya.unit.TransformedUnit) Test(org.junit.jupiter.api.Test)

Example 50 with Unit

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

the class Constant method getOperandsFrom.

@SuppressWarnings({ "unchecked", "rawtypes" })
public void getOperandsFrom(SimpleNode node) {
    Object o = node.jjtGetValue();
    if (o instanceof UnitValue) {
        unitValue = (UnitValue) node.jjtGetValue();
        if (// naked number, so assume already in SI
        unitValue.unit == null) {
            unit = AbstractUnit.ONE;
            value = new Scalar(unitValue.value);
        } else // there was a unit given, so convert
        {
            unit = unitValue.unit.getSystemUnit();
            value = new Scalar(unitValue.unit.getConverterTo((Unit) unit).convert(unitValue.value));
        }
    } else {
        unit = AbstractUnit.ONE;
        value = (Type) o;
    }
}
Also used : Unit(javax.measure.Unit) AbstractUnit(tech.units.indriya.AbstractUnit) Scalar(gov.sandia.n2a.language.type.Scalar)

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