public class UnitFormat extends Format implements UnitFormat, Localized
java.text
and the API from javax.measure.format
.
In addition to the symbols of the Système international (SI), this class is also capable to handle
some symbols found in Well Known Text (WKT) definitions or in XML files.
parse(CharSequence)
method is of the
"EPSG:####"
or "urn:ogc:def:uom:EPSG::####"
form (ignoring case and whitespaces),
then "####"
is parsed as an integer and forwarded to the Units.valueOfEPSG(int)
method.
"degrees_east"
, "degrees_north"
or "Degrees North"
.
This class ignores those suffixes and unconditionally returns Units.DEGREE
for all axis directions.
In particular, the units for "degrees_west"
and "degrees_east"
do not have
opposite sign. It is caller responsibility to handle the direction of axes associated to netCDF units.
UnitFormat
is generally not thread-safe.
However if there is no call to any setter method or to label(Unit, String)
after construction,
then the parse(CharSequence)
and format(Unit)
methods can be invoked concurrently in
different threads.Units.valueOf(String)
,
Serialized FormDefined in the sis-utility
module
Modifier and Type | Class and Description |
---|---|
static class |
UnitFormat.Style
Identify whether unit formatting uses ASCII symbols, Unicode symbols or full localized names.
|
Format.Field
Constructor and Description |
---|
UnitFormat(Locale locale)
Creates a new format for the given locale.
|
Modifier and Type | Method and Description |
---|---|
UnitFormat |
clone()
Returns a clone of this unit format.
|
StringBuffer |
format(Object unit,
StringBuffer toAppendTo,
FieldPosition pos)
Formats the specified unit in the given buffer.
|
String |
format(Unit<?> unit)
Formats the given unit.
|
Appendable |
format(Unit<?> unit,
Appendable toAppendTo)
Formats the specified unit.
|
Locale |
getLocale()
Returns the locale used by this
UnitFormat . |
UnitFormat.Style |
getStyle()
Returns whether unit formatting uses ASCII symbols, Unicode symbols or full localized names.
|
boolean |
isLocaleSensitive()
Returns whether this
UnitFormat depends on the Locale given at construction time
for performing its tasks. |
void |
label(Unit<?> unit,
String label)
Attaches a label to the specified unit.
|
Unit<?> |
parse(CharSequence symbols)
Parses the given text as an instance of
Unit . |
Unit<?> |
parse(CharSequence symbols,
ParsePosition position)
Parses a portion of the given text as an instance of
Unit . |
Object |
parseObject(String source)
Parses text from a string to produce a unit.
|
Object |
parseObject(String source,
ParsePosition pos)
Parses text from a string to produce a unit, or returns
null if the parsing failed. |
void |
setLocale(Locale locale)
Sets the locale that this
UnitFormat will use for long names. |
void |
setStyle(UnitFormat.Style style)
Sets whether unit formatting should use ASCII symbols, Unicode symbols or full localized names.
|
format, formatToCharacterIterator
public UnitFormat(Locale locale)
locale
- the locale to use for parsing and formatting units.public Locale getLocale()
UnitFormat
.getLocale
in interface Localized
UnitFormat
.public void setLocale(Locale locale)
UnitFormat
will use for long names.
For example a call to setLocale(Locale.US)
instructs this formatter to use the “meter” spelling instead of “metre”.locale
- the new locale for this UnitFormat
.UnitServices.getUnitFormat(String)
public boolean isLocaleSensitive()
UnitFormat
depends on the Locale
given at construction time
for performing its tasks. This method returns true
if formatting long names (e.g. “metre”
or “meter”} and false
if formatting only the unit symbol (e.g. “m”).isLocaleSensitive
in interface UnitFormat
true
if formatting depends on the locale.public UnitFormat.Style getStyle()
UnitFormat
instance.public void setStyle(UnitFormat.Style style)
style
- the desired style of units.public void label(Unit<?> unit, String label)
unit name
,
depending on the format style.
If the specified label is already associated to another unit, then the previous association is discarded.
" # ( ) * + - . / : = ? [ ] { } ^ ⋅ ∕
label
in interface UnitFormat
unit
- the unit being labeled.label
- the new label for the given unit.IllegalArgumentException
- if the given label is not a valid unit name.public Appendable format(Unit<?> unit, Appendable toAppendTo) throws IOException
UnitFormat.Style.NAME
and the Unit.getName()
method
returns a non-null value, then that value is appended. Unit
instances implemented by
Apache SIS are handled in a special way for localizing the name according the
locale specified to this format.Unit.getSymbol()
method returns a non-null value,
then that value is appended.Unit.getBaseUnits()
.format
in interface UnitFormat
unit
- the unit to format.toAppendTo
- where to format the unit.toAppendTo
argument, for method calls chaining.IOException
- if an error occurred while writing to the destination.public StringBuffer format(Object unit, StringBuffer toAppendTo, FieldPosition pos)
format(Unit, Appendable)
.public String format(Unit<?> unit)
format(Unit, Appendable)
.format
in interface UnitFormat
unit
- the unit to format.public Unit<?> parse(CharSequence symbols) throws ParserException
Unit
.
If the parse completes without reading the entire length of the text, an exception is thrown.
The parsing is lenient: symbols can be products or quotients of units like “m∕s”,
words like “meters per second”, or authority codes like "urn:ogc:def:uom:EPSG::1026"
.
The product operator can be either '.'
(ASCII) or '⋅'
(Unicode) character.
Exponent after symbol can be decimal digits as in “m2” or a superscript as in “m²”.
The default implementation delegates to
parse(symbols, new ParsePosition(0))
and verifies that all non-white characters have been parsed.
parse
in interface UnitFormat
symbols
- the unit symbols or URI to parse.ParserException
- if a problem occurred while parsing the given symbols.Units.valueOf(String)
public Unit<?> parse(CharSequence symbols, ParsePosition position) throws ParserException
Unit
.
Parsing begins at the index given by ParsePosition.getIndex()
.
After parsing, the above-cited index is updated to the first unparsed character.
The parsing is lenient: symbols can be products or quotients of units like “m∕s”,
words like “meters per second”, or authority codes like "urn:ogc:def:uom:EPSG::1026"
.
The product operator can be either '.'
(ASCII) or '⋅'
(Unicode) character.
Exponent after symbol can be decimal digits as in “m2” or a superscript as in “m²”.
Note that contrarily to parseObject(String, ParsePosition)
, this method never return null
.
If an error occurs at parsing time, an unchecked ParserException
is thrown.
symbols
- the unit symbols to parse.position
- on input, index of the first character to parse.
On output, index after the last parsed character.ParserException
- if a problem occurred while parsing the given symbols.public Object parseObject(String source) throws ParseException
parse(CharSequence)
and wraps the ParserException
into a ParseException
for compatibility with java.text
API.parseObject
in class Format
source
- the text, part of which should be parsed.ParseException
- if the given string can not be fully parsed.public Object parseObject(String source, ParsePosition pos)
null
if the parsing failed.
The default implementation delegates to parse(CharSequence, ParsePosition)
and catches
the ParserException
.parseObject
in class Format
source
- the text, part of which should be parsed.pos
- index and error index information as described above.null
in case of error.public UnitFormat clone()
Copyright © 2010–2017 The Apache Software Foundation. All rights reserved.