use of javax.measure.format.UnitFormat in project sis by apache.
the class UnitServices method getUnitFormat.
/**
* Returns the unit format for the given name. The argument can be the name of
* any value in the {@link org.apache.sis.measure.UnitFormat.Style} enumeration.
* The argument can be any name in the following table:
*
* <table class="sis">
* <caption>Available unit format name</caption>
* <tr><th>Name</th> <th>Examples</th></tr>
* <tr><td>SYMBOL</td> <td>km, m³, m∕s, N⋅m, K, °C, hPa, rad, µrad</td></tr>
* <tr><td>UCUM</td> <td>km, m3, m/s, N.m</td></tr>
* <tr><td>NAME</td> <td>kilometre, cubic metre, metres per second</td></tr>
* </table>
*
* The {@code "NAME"} format is locale-sensitive. The format locale can be modified by a call
* to {@link org.apache.sis.measure.UnitFormat#setLocale(Locale)} on the returned object.
*
* @param name the name of the desired format.
* @return the corresponding unit format, or {@code null} if none.
*/
@Override
public UnitFormat getUnitFormat(String name) {
final Locale locale = Locale.getDefault(Locale.Category.FORMAT);
name = name.toUpperCase(locale).trim();
final org.apache.sis.measure.UnitFormat.Style style;
try {
style = org.apache.sis.measure.UnitFormat.Style.valueOf(name);
} catch (IllegalArgumentException e) {
// JSR-363 specification mandate that we return null.
Logging.recoverableException(Logging.getLogger(Loggers.MEASURE), UnitServices.class, "getUnitFormat", e);
return null;
}
org.apache.sis.measure.UnitFormat f = new org.apache.sis.measure.UnitFormat(locale);
f.setStyle(style);
return f;
}
use of javax.measure.format.UnitFormat in project uom-se by unitsofmeasurement.
the class LocalFormatTest method testFormatMm.
@Test
public void testFormatMm() {
final UnitFormat format = LocalUnitFormat.getInstance();
String s = format.format(MILLI(METRE));
assertEquals("mm", s);
}
use of javax.measure.format.UnitFormat in project uom-se by unitsofmeasurement.
the class UnitFormatTest method testParseLocal.
@Test(expected = UnsupportedOperationException.class)
public void testParseLocal() {
final UnitFormat format = LocalUnitFormat.getInstance();
try {
Unit<?> u = format.parse("min");
assertEquals("min", u.getSymbol());
} catch (ParserException e) {
fail(e.getMessage());
}
}
use of javax.measure.format.UnitFormat in project indriya by unitsofmeasurement.
the class LocalFormatTest method testParseIrregularStringLocal.
@Test
public void testParseIrregularStringLocal() {
assertThrows(UnsupportedOperationException.class, () -> {
final UnitFormat format = LocalUnitFormat.getInstance();
@SuppressWarnings("unused") Unit<?> u = format.parse("bl//^--1a");
});
}
use of javax.measure.format.UnitFormat in project indriya by unitsofmeasurement.
the class LocalFormatTest method testPrefixKm.
@Test
@Disabled
public // TODO LocalUnitFormat won't parse Compound Units, EBNF does, also see https://github.com/unitsofmeasurement/uom-se/issues/145
void testPrefixKm() {
final UnitFormat format = LocalUnitFormat.getInstance();
Unit<?> u = format.parse("km");
assertEquals(KILO(METRE), u);
assertEquals("km", u.toString());
}
Aggregations