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