Package javax.measure.unit
Class UnitFormat
java.lang.Object
java.text.Format
javax.measure.unit.UnitFormat
- All Implemented Interfaces:
Serializable
,Cloneable
- Direct Known Subclasses:
UnitFormat.DefaultFormat
This class provides the interface for formatting and parsing units
.
For all SI
units, the 20 SI prefixes used to form decimal
multiples and sub-multiples of SI units are recognized.
NonSI
units are directly recognized. For example:[code]
Unit.valueOf("m°C").equals(SI.MILLI(SI.CELSIUS))
Unit.valueOf("kW").equals(SI.KILO(SI.WATT))
Unit.valueOf("ft").equals(SI.METER.multiply(0.3048))[/code]
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected static class
This class represents the ASCIIFormat format.protected static class
This class represents the standard format.Nested classes/interfaces inherited from class java.text.Format
Format.Field
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionabstract void
Attaches a system-wide alias to this unit.final StringBuffer
format
(Object unit, StringBuffer toAppendTo, FieldPosition pos) Formats an unit and appends the resulting text to a given string buffer (implementsjava.text.Format
).abstract Appendable
format
(Unit<?> unit, Appendable appendable) Formats the specified unit.static UnitFormat
Returns the unit format for the default locale (format used byUnit.valueOf(CharSequence)
andUnit.toString()
).static UnitFormat
getInstance
(Locale inLocale) Returns the unit format for the specified locale.static UnitFormat
Returns the UCUM international unit format; this format uses characters range0000-007F
exclusively and is not locale-sensitive.abstract boolean
isValidIdentifier
(String name) Indicates if the specified name can be used as unit identifier.abstract void
Attaches a system-wide label to the specified unit.final Unit<?>
parseObject
(String source, ParsePosition pos) Parses the text from a string to produce an object (implementsjava.text.Format
).parseProductUnit
(CharSequence csq, ParsePosition pos) Parses a sequence of character to produce a unit or a rational product of unit.parseSingleUnit
(CharSequence csq, ParsePosition pos) Parses a sequence of character to produce a single unit.Methods inherited from class java.text.Format
clone, format, formatToCharacterIterator, parseObject
-
Constructor Details
-
UnitFormat
protected UnitFormat()Base constructor.
-
-
Method Details
-
getInstance
Returns the unit format for the default locale (format used byUnit.valueOf(CharSequence)
andUnit.toString()
).- Returns:
- the default unit format (locale sensitive).
-
getInstance
Returns the unit format for the specified locale.- Returns:
- the unit format for the specified locale.
-
getUCUMInstance
Returns the UCUM international unit format; this format uses characters range0000-007F
exclusively and is not locale-sensitive. For example:kg.m/s2
- Returns:
- the UCUM international format.
-
format
Formats the specified unit.- Parameters:
unit
- the unit to format.appendable
- the appendable destination.- Throws:
IOException
- if an error occurs.
-
parseProductUnit
public abstract Unit<? extends Quantity> parseProductUnit(CharSequence csq, ParsePosition pos) throws ParseException Parses a sequence of character to produce a unit or a rational product of unit.- Parameters:
csq
- theCharSequence
to parse.pos
- an object holding the parsing index and error position.- Returns:
- an
Unit
parsed from the character sequence. - Throws:
IllegalArgumentException
- if the character sequence contains an illegal syntax.ParseException
-
parseSingleUnit
public abstract Unit<? extends Quantity> parseSingleUnit(CharSequence csq, ParsePosition pos) throws ParseException Parses a sequence of character to produce a single unit.- Parameters:
csq
- theCharSequence
to parse.pos
- an object holding the parsing index and error position.- Returns:
- an
Unit
parsed from the character sequence. - Throws:
IllegalArgumentException
- if the character sequence does not contain a valid unit identifier.ParseException
-
label
Attaches a system-wide label to the specified unit. For example: [code] UnitFormat.getInstance().label(DAY.multiply(365), "year"); UnitFormat.getInstance().label(METER.multiply(0.3048), "ft"); [/code] If the specified label is already associated to an unit the previous association is discarded or ignored.- Parameters:
unit
- the unit being labelled.label
- the new label for this unit.- Throws:
IllegalArgumentException
- if the label is not aisValidIdentifier(String)
valid identifier.
-
alias
Attaches a system-wide alias to this unit. Multiple aliases may be attached to the same unit. Aliases are used during parsing to recognize different variants of the same unit. For example: [code] UnitFormat.getLocaleInstance().alias(METER.multiply(0.3048), "foot"); UnitFormat.getLocaleInstance().alias(METER.multiply(0.3048), "feet"); UnitFormat.getLocaleInstance().alias(METER, "meter"); UnitFormat.getLocaleInstance().alias(METER, "metre"); [/code] If the specified label is already associated to an unit the previous association is discarded or ignored.- Parameters:
unit
- the unit being aliased.alias
- the alias attached to this unit.- Throws:
IllegalArgumentException
- if the label is not aisValidIdentifier(String)
valid identifier.
-
isValidIdentifier
Indicates if the specified name can be used as unit identifier.- Parameters:
name
- the identifier to be tested.- Returns:
true
if the name specified can be used as label or alias for this format;false
otherwise.
-
format
Formats an unit and appends the resulting text to a given string buffer (implementsjava.text.Format
). -
parseObject
Parses the text from a string to produce an object (implementsjava.text.Format
).- Specified by:
parseObject
in classFormat
- Parameters:
source
- the string source, part of which should be parsed.pos
- the cursor position.- Returns:
- the corresponding unit or
null
if the string cannot be parsed.
-