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);
}
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);
}
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();
}
}
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());
}
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;
}
}
Aggregations