- All Implemented Interfaces:
Serializable
,Cloneable
,QuantityFormat
Parses and formats numbers with units of measurement.
- Since:
- 1.1
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class Format
Format.Field
-
Field Summary
Modifier and TypeFieldDescriptionprotected final NumberFormat
The format for parsing and formatting the number part.static final char
The default separator used between numerical value and its unit of measurement.protected final UnitFormat
The format for parsing and formatting the unit of measurement part. -
Constructor Summary
ConstructorDescriptionQuantityFormat
(NumberFormat numberFormat, UnitFormat unitFormat) Creates a new instance using the given number and unit formats.QuantityFormat
(Locale locale) Creates a new instance for the given locale. -
Method Summary
Modifier and TypeMethodDescriptionclone()
Returns a clone of this format.format
(Object quantity, StringBuffer toAppendTo, FieldPosition pos) Formats the specified quantity in the given buffer.Formats the specified quantity.format
(Quantity<?> quantity, Appendable toAppendTo) Formats the specified quantity in the given destination.boolean
Returns whether this format depends on aLocale
to perform its tasks.Quantity
<?> parse
(CharSequence source) Parses the specified text to produce aQuantity
.Quantity
<?> parse
(CharSequence source, ParsePosition pos) Parses a portion of the specifiedCharSequence
from the specified position to produce aQuantity
.parseObject
(String source, ParsePosition pos) Parses text from a string to produce a quantity, or returnsnull
if the parsing failed.Methods inherited from class Format
format, formatToCharacterIterator, parseObject
-
Field Details
-
SEPARATOR
public static final char SEPARATORThe default separator used between numerical value and its unit of measurement. Current value is narrow no-break space (U+202F).- See Also:
-
numberFormat
The format for parsing and formatting the number part. -
unitFormat
The format for parsing and formatting the unit of measurement part.
-
-
Constructor Details
-
QuantityFormat
Creates a new instance for the given locale.- Parameters:
locale
- the locale for the quantity format.
-
QuantityFormat
Creates a new instance using the given number and unit formats.- Parameters:
numberFormat
- the format for parsing and formatting the number part.unitFormat
- the format for parsing and formatting the unit of measurement part.
-
-
Method Details
-
isLocaleSensitive
public boolean isLocaleSensitive()Returns whether this format depends on aLocale
to perform its tasks. This istrue
in thisQuantityFormat
implementation.- Specified by:
isLocaleSensitive
in interfaceQuantityFormat
- Returns:
- whether this format depends on the locale, which is true in this implementation.
- Since:
- 1.4
-
format
Formats the specified quantity. The default implementation delegates toformat(Object, StringBuffer, FieldPosition)
.- Specified by:
format
in interfaceQuantityFormat
- Parameters:
quantity
- the quantity to format.- Returns:
- the string representation of the given quantity.
- Since:
- 1.4
-
format
Formats the specified quantity in the given destination. The default implementation delegates toformat(Object, StringBuffer, FieldPosition)
.- Specified by:
format
in interfaceQuantityFormat
- Parameters:
quantity
- the quantity to format.toAppendTo
- where to format the quantity.- Returns:
- the given
toAppendTo
argument, for method calls chaining. - Throws:
IOException
- if an I/O exception occurred.- Since:
- 1.4
-
format
Formats the specified quantity in the given buffer. The given object shall be aQuantity
instance. -
parse
Parses the specified text to produce aQuantity
.- Specified by:
parse
in interfaceQuantityFormat
- Parameters:
source
- the text to parse.- Returns:
- the quantity parsed from the specified text.
- Throws:
MeasurementParseException
- if the given text cannot be parsed.- Since:
- 1.4
-
parse
Parses a portion of the specifiedCharSequence
from the specified position to produce aQuantity
. If parsing succeeds, then the index of thepos
argument is updated to the index after the last character used.- Specified by:
parse
in interfaceQuantityFormat
- Parameters:
source
- the text, part of which should be parsed.pos
- index and error index information.- Returns:
- the quantity parsed from the specified character sub-sequence.
- Throws:
MeasurementParseException
- if the given text cannot be parsed.- Since:
- 1.4
-
parseObject
Parses text from a string to produce a quantity, or returnsnull
if the parsing failed.- Specified by:
parseObject
in classFormat
- Parameters:
source
- the text, part of which should be parsed.pos
- index and error index information.- Returns:
- a quantity parsed from the string, or
null
in case of error.
-
clone
Returns a clone of this format.
-