Top Description Methods
javax.measure.format

public Interface UnitFormat

Imports
java.io.IOException, java.text.ParsePosition, javax.measure.Unit

Formats instances of Unit to a String or Appendable and parses a CharSequence to a Unit.
Synchronization

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.

Authors
Jean-Marie Dautelle, Werner Keil
Since
1.0
Version
2.2, May 20, 2023
See Also
Unit

Method Summary

Modifier and TypeMethod and Description
public Appendable

Returns:

the appendable destination passed in with formatted text appended.
format
(Unit<?>
the Unit to format, not null
unit
,
Appendable
the appendable destination.
appendable
)

Formats the specified Unit.

public String

Returns:

the string representation using the settings of this UnitFormat.
format
(Unit<?>
the Unit to format, not null
unit
)

Formats the specified Unit.

public default boolean

Returns:

Whether this format depends on a locale.
isLocaleSensitive
()

Returns true if this UnitFormat depends on a Locale to perform its tasks.

public void
label(Unit<?>
the unit being labeled.
unit
,
String
the new label for this unit.
label
)

Attaches a system-wide label to the specified Unit.

public Unit<?>

Returns:

the unit parsed from the specified character sub-sequence.
parse
(CharSequence
the CharSequence to parse.
csq
,
ParsePosition
a ParsePosition object holding the current parsing index and error parsing index information as described above.
pos
)

Parses a portion of the specified CharSequence from the specified position to produce a Unit.

public Unit<?>

Returns:

the unit parsed from the specified character sequence.
parse
(CharSequence
the CharSequence to parse.
csq
)

Parses the text into an instance of Unit.

Method Detail

formatback to summary
public Appendable format(Unit<?> unit, Appendable appendable) throws IOException

Formats the specified Unit.

Parameters
unit:Unit<?>

the Unit to format, not null

appendable:Appendable

the appendable destination.

Returns:Appendable

the appendable destination passed in with formatted text appended.

Exceptions
IOException:
if an error occurs while writing to the destination.
formatback to summary
public String format(Unit<?> unit)

Formats the specified Unit.

Parameters
unit:Unit<?>

the Unit to format, not null

Returns:String

the string representation using the settings of this UnitFormat.

isLocaleSensitiveback to summary
public default boolean isLocaleSensitive()

Returns true if this UnitFormat depends on a Locale to perform its tasks.

In environments that do not support a Locale, e.g. Java ME, this usually returns false.

Returns:boolean

Whether this format depends on a locale.

labelback to summary
public void label(Unit<?> unit, String label)

Attaches a system-wide label to the specified Unit.

This method overrides the previous unit's label (e.g. label from unit database or unit system) as units may only have one label. Depending on the UnitFormat implementation, this call may be ignored if the particular unit already has a label.

If a UnitFormat implementation is explicitly immutable, similar to e.g. the result of Collections.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 of UnitFormatA implementing UnitFormat in this case, but not every instance of UnitFormatB or UnitFormatC both also implementing UnitFormat. If a UnitFormat #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:Unit<?>

the unit being labeled.

label:String

the new label for this unit.

Exceptions
IllegalArgumentException:
if the label is not a valid identifier. This may include characters not supported by a particular UnitFormat implementation (e.g. only ASCII characters for certain devices)
UnsupportedOperationException:
if the label operation is not supported by this UnitFormat
parseback to summary
public Unit<?> parse(CharSequence csq, ParsePosition pos) throws MeasurementParseException

Parses a portion of the specified CharSequence from the specified position to produce a Unit. If parsing succeeds, then the index of the pos argument is updated to the index after the last character used.

Parameters
csq:CharSequence

the CharSequence to parse.

pos:ParsePosition

a ParsePosition object holding the current parsing index and error parsing index information as described above.

Returns:Unit<?>

the unit parsed from the specified character sub-sequence.

Exceptions
MeasurementParseException:
if any problem occurs while parsing the specified character sequence (e.g. illegal syntax).
Since
2.0
parseback to summary
public Unit<?> parse(CharSequence csq) throws MeasurementParseException

Parses the text into an instance of Unit.

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:CharSequence

the CharSequence to parse.

Returns:Unit<?>

the unit parsed from the specified character sequence.

Exceptions
MeasurementParseException:
if any problem occurs while parsing the specified character sequence (e.g. illegal syntax).
UnsupportedOperationException:
if the UnitFormat is unable to parse.
Since
2.0