Search in sources :

Example 1 with UnitFormat

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;
}
Also used : Locale(java.util.Locale) UnitFormat(javax.measure.format.UnitFormat)

Example 2 with UnitFormat

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);
}
Also used : UnitFormat(javax.measure.format.UnitFormat) Test(org.junit.Test)

Example 3 with UnitFormat

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());
    }
}
Also used : ParserException(javax.measure.format.ParserException) UnitFormat(javax.measure.format.UnitFormat) Test(org.junit.Test)

Example 4 with UnitFormat

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");
    });
}
Also used : UnitFormat(javax.measure.format.UnitFormat) LocalUnitFormat(tech.units.indriya.format.LocalUnitFormat) Test(org.junit.jupiter.api.Test)

Example 5 with UnitFormat

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());
}
Also used : UnitFormat(javax.measure.format.UnitFormat) LocalUnitFormat(tech.units.indriya.format.LocalUnitFormat) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Aggregations

UnitFormat (javax.measure.format.UnitFormat)17 Test (org.junit.Test)8 Test (org.junit.jupiter.api.Test)8 LocalUnitFormat (tech.units.indriya.format.LocalUnitFormat)7 SimpleUnitFormat (tech.units.indriya.format.SimpleUnitFormat)3 IOException (java.io.IOException)2 Unit (javax.measure.Unit)2 ParserException (javax.measure.format.ParserException)2 Speed (javax.measure.quantity.Speed)2 Locale (java.util.Locale)1 ServiceProvider (javax.measure.spi.ServiceProvider)1 Ignore (org.junit.Ignore)1 Disabled (org.junit.jupiter.api.Disabled)1