Interface UnitFormat
public interface UnitFormat
Unit
to a String
or Appendable
and parses a CharSequence
to a Unit
.
Instances of this class are not required to be thread-safe. It is recommended to use separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.
- Since:
- 1.0
- Version:
- 2.0, July 3, 2020
- Author:
- Jean-Marie Dautelle, Werner Keil
- See Also:
Unit
-
Method Summary
Modifier and Type Method Description String
format(Unit<?> unit)
Formats the specifiedUnit
.Appendable
format(Unit<?> unit, Appendable appendable)
Formats the specifiedUnit
.default boolean
isLocaleSensitive()
void
label(Unit<?> unit, String label)
Attaches a system-wide label to the specified unit.Unit<?>
parse(CharSequence csq)
Parses the text into an instance ofUnit
.Unit<?>
parse(CharSequence csq, ParsePosition pos)
Parses a portion of the specifiedCharSequence
from the specified position to produce aUnit
.
-
Method Details
-
format
Formats the specifiedUnit
.- Parameters:
unit
- theUnit
to format, notnull
appendable
- the appendable destination.- Returns:
- the appendable destination passed in with formatted text appended.
- Throws:
IOException
- if an error occurs while writing to the destination.
-
format
Formats the specifiedUnit
.- Parameters:
unit
- theUnit
to format, notnull
- Returns:
- the string representation using the settings of this
UnitFormat
.
-
label
Attaches a system-wide label to the specified unit.If the specified label is already associated to a unit the previous association can be discarded or ignored. Depending on the
If aUnitFormat
implementation, this call may be ignored if the particular unit already has a label.UnitFormat
implementation is explicitly immutable, similar to e.g. the result ofCollections.unmodifiableList()
, then an UnsupportedOperationException may be thrown upon this call.Since
UnitFormat
implementations often apply the Singleton pattern, system-wide means, the label applies to every instance ofUnitFormatA
implementingUnitFormat
in this case, but not every instance ofUnitFormatB
orUnitFormatC
both also implementingUnitFormat
. If aUnitFormat
#isLocaleSensitive() it is up to the implementation, whether the label is ignored, applied in a local-neutral manner (in addition to its local-sensitive information) or locale-specific.- Parameters:
unit
- the unit being labeled.label
- the new label for this unit.- Throws:
IllegalArgumentException
- if the label is not a valid identifier. This may include characters not supported by a particularUnitFormat
implementation (e.g. only ASCII characters for certain devices)UnsupportedOperationException
- if thelabel
operation is not supported by thisUnitFormat
-
isLocaleSensitive
Returnstrue
if thisUnitFormat
depends on aLocale
to perform its tasks.In environments that do not support a
Locale
, e.g. Java ME, this usually returnsfalse
.- Returns:
- Whether this format depends on a locale.
-
parse
Unit<?> parse(CharSequence csq, ParsePosition pos) throws IllegalArgumentException, MeasurementParseExceptionParses a portion of the specifiedCharSequence
from the specified position to produce aUnit
. If parsing succeeds, then the index of thepos
argument is updated to the index after the last character used.- Parameters:
csq
- theCharSequence
to parse.pos
- a ParsePosition object holding the current parsing index and error parsing index information as described above.- Returns:
- the unit parsed from the specified character sub-sequence.
- Throws:
IllegalArgumentException
- if any problem occurs while parsing the specified character sequence (e.g. illegal syntax).MeasurementParseException
- Since:
- 2.0
-
parse
Parses the text into an instance ofUnit
.The parse must complete normally and parse the entire text. If the parse completes without reading the entire length of the text, an exception is thrown. If any other problem occurs during parsing, an exception is thrown.
- Parameters:
csq
- theCharSequence
to parse.- Returns:
- the unit parsed from the specified character sequence.
- Throws:
MeasurementParseException
- if any problem occurs while parsing the specified character sequence (e.g. illegal syntax).UnsupportedOperationException
- if theUnitFormat
is unable to parse.- Since:
- 2.0
-