Top Description Fields Constructors Methods
tech.units.indriya.format

public Class LocalUnitFormat

extends AbstractUnitFormat
Class Inheritance
Static Imports
tech.units.indriya.format.ConverterFormatter.formatConverterLocal, tech.units.indriya.format.FormatConstants.*, tech.units.indriya.unit.Units.CUBIC_METRE, .Units.GRAM, .Units.KILOGRAM, .Units.LITRE

This class represents the locale sensitive format.

Here is the grammar for CommonUnits in Extended Backus-Naur Form (EBNF)

Note that the grammar has been left-factored to be suitable for use by a top-down parser generator such as JavaCC

Lexical Entities:
<sign> := "+" | "-"
<digit> := "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
<superscript_digit> := "⁰" | "¹" | "²" | "³" | "⁴" | "⁵" | "⁶" | "⁷" | "⁸" | "⁹"
<integer> := (<digit>)+
<number> := (<sign>)? (<digit>)* (".")? (<digit>)+ (("e" | "E") (<sign>)? (<digit>)+)?
<exponent> := ( "^" ( <sign> )? <integer> )
| ( "^(" (<sign>)? <integer> ( "/" (<sign>)? <integer> )? ")" )
| ( <superscript_digit> )+
<initial_char> := ? Any Unicode character excluding the following: ASCII control & whitespace (\u0000 - \u0020), decimal digits '0'-'9', '(' (\u0028), ')' (\u0029), '*' (\u002A), '+' (\u002B), '-' (\u002D), '.' (\u002E), '/' (\u005C), ':' (\u003A), '^' (\u005E), '²' (\u00B2), '³' (\u00B3), '·' (\u00B7), '¹' (\u00B9), '⁰' (\u2070), '⁴' (\u2074), '⁵' (\u2075), '⁶' (\u2076), '⁷' (\u2077), '⁸' (\u2078), '⁹' (\u2079) ?
<unit_identifier> := <initial_char> ( <initial_char> | <digit> )*
Non-Terminals:
<unit_expr> := <mix_expr>
<mix_expr> := <add_expr> ( ":" <add_expr> )*
<add_expr> := ( <number> <sign> )? <mul_expr> ( <sign> <number> )?
<mul_expr> := <exponent_expr> ( ( ( "*" | "·" ) <exponent_expr> ) | ( "/" <exponent_expr> ) )*
<exponent_expr> := ( <atomic_expr> ( <exponent> )? )
| (<integer> "^" <atomic_expr>)
| ( ( "log" ( <integer> )? ) | "ln" ) "(" <add_expr> ")" )
<atomic_expr> := <number>
| <unit_identifier>
| ( "(" <add_expr> ")" )
Authors
Eric Russell, Werner Keil
Since
1.0
Version
1.6, October 4, 2024

Field Summary

Modifier and TypeField and Description
private static final LocalUnitFormat
DEFAULT_INSTANCE

DefaultQuantityFactory locale instance.

private final transient SymbolMap
symbolMap

The symbol map used by this instance to map between Units and Strings, etc...

Constructor Summary

AccessConstructor and Description
private
LocalUnitFormat(SymbolMap
the symbol mapping.
symbols
)

Base constructor.

Method Summary

Modifier and TypeMethod and Description
public Appendable
format(Unit<?>
the unit to format.
unit
,
Appendable
the appendable destination.
appendable
)

Implements abstract tech.units.indriya.format.AbstractUnitFormat.format.

Implements javax.measure.format.UnitFormat.format.

Formats the specified unit.

private void
formatExponent(Unit<?>
Unit the unit to be formatted
unit
,
int
int the numerator of the fractional power
pow
,
int
int the denominator of the fractional power
root
,
boolean
boolean true if the converter expression should begin with an operator, otherwise false. This will always be true unless the unit being modified is equal to Unit.ONE.
continued
,
Appendable
StringBuffer the buffer to append to. No assumptions should be made about its content.
buffer
)

Format the given unit raised to the given fractional power to the given StringBuffer.

private int

Returns:

the operator precedence of the outermost operator in the unit expression that was output
formatInternal
(Unit<?>
the unit to be formatted
unit
,
Appendable
the StringBuilder to be written to
buffer
)

Format the given unit to the given StringBuilder, then return the operator precedence of the outermost operator in the unit expression that was formatted.

public static LocalUnitFormat
getInstance()

Returns the instance for the current default locale (non-ascii characters are allowed)

public static LocalUnitFormat
getInstance(Locale
the locale to use
locale
)

Returns an instance for the given locale.

public static LocalUnitFormat
getInstance(SymbolMap symbols)

Returns an instance for the given symbol map.

protected SymbolMap

Returns:

SymbolMap the current symbol map
getSymbols
()

Get the symbol map used by this instance to map between Units and Strings, etc...

public boolean
isLocaleSensitive()

Overrides default javax.measure.format.UnitFormat.isLocaleSensitive.

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

protected Unit<?>
parse(CharSequence
the CharSequence to parse.
csq
,
int
the current parsing index.
index
)

Implements abstract tech.units.indriya.format.AbstractUnitFormat.parse.

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

public Unit<?>
parse(CharSequence
the CharSequence to parse.
csq
,
ParsePosition
the cursor holding the current parsing index.
cursor
)

Implements abstract tech.units.indriya.format.AbstractUnitFormat.parse.

Implements javax.measure.format.UnitFormat.parse.

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

public Unit<? extends Quantity<?>>
parse(CharSequence
the CharSequence to parse.
csq
)

Implements javax.measure.format.UnitFormat.parse.

Parses the text into an instance of Unit.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from tech.units.indriya.format.AbstractUnitFormat:
formatlabel

Field Detail

DEFAULT_INSTANCEback to summary
private static final LocalUnitFormat DEFAULT_INSTANCE

DefaultQuantityFactory locale instance. If the default locale is changed after the class is initialized, this instance will no longer be used.

symbolMapback to summary
private final transient SymbolMap symbolMap

The symbol map used by this instance to map between Units and Strings, etc...

Constructor Detail

LocalUnitFormatback to summary
private LocalUnitFormat(SymbolMap symbols)

Base constructor.

Parameters
symbols:SymbolMap

the symbol mapping.

Method Detail

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

Implements abstract tech.units.indriya.format.AbstractUnitFormat.format.

Implements javax.measure.format.UnitFormat.format.

Doc from tech.units.indriya.format.AbstractUnitFormat.format.

Formats the specified unit.

Parameters
unit:Unit<?>

the unit to format.

appendable:Appendable

the appendable destination.

Returns:Appendable

The appendable destination passed in as appendable, with formatted text appended.

Annotations
@Override
Exceptions
IOException:
if an error occurs.
formatExponentback to summary
private void formatExponent(Unit<?> unit, int pow, int root, boolean continued, Appendable buffer) throws IOException

Format the given unit raised to the given fractional power to the given StringBuffer.

Parameters
unit:Unit<?>

Unit the unit to be formatted

pow:int

int the numerator of the fractional power

root:int

int the denominator of the fractional power

continued:boolean

boolean true if the converter expression should begin with an operator, otherwise false. This will always be true unless the unit being modified is equal to Unit.ONE.

buffer:Appendable

StringBuffer the buffer to append to. No assumptions should be made about its content.

formatInternalback to summary
private int formatInternal(Unit<?> unit, Appendable buffer) throws IOException

Format the given unit to the given StringBuilder, then return the operator precedence of the outermost operator in the unit expression that was formatted. See ConverterFormat for the constants that define the various precedence values.

Parameters
unit:Unit<?>

the unit to be formatted

buffer:Appendable

the StringBuilder to be written to

Returns:int

the operator precedence of the outermost operator in the unit expression that was output

Annotations
@SuppressWarnings:rawtypes, unchecked
getInstanceback to summary
public static LocalUnitFormat getInstance()

Returns the instance for the current default locale (non-ascii characters are allowed)

getInstanceback to summary
public static LocalUnitFormat getInstance(Locale locale)

Returns an instance for the given locale.

Parameters
locale:Locale

the locale to use

getInstanceback to summary
public static LocalUnitFormat getInstance(SymbolMap symbols)

Returns an instance for the given symbol map.

getSymbolsback to summary
protected SymbolMap getSymbols()

Get the symbol map used by this instance to map between Units and Strings, etc...

Returns:SymbolMap

SymbolMap the current symbol map

isLocaleSensitiveback to summary
public boolean isLocaleSensitive()

Overrides default javax.measure.format.UnitFormat.isLocaleSensitive.

Doc from javax.measure.format.UnitFormat.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.

parseback to summary
protected Unit<?> parse(CharSequence csq, int index) throws MeasurementParseException

Implements abstract tech.units.indriya.format.AbstractUnitFormat.parse.

Doc from tech.units.indriya.format.AbstractUnitFormat.parse.

Parses a portion of the specified CharSequence from the specified position to produce a unit. If there is no unit to parse AbstractUnit#ONE is returned.

Parameters
csq:CharSequence

the CharSequence to parse.

index:int

the current parsing index.

Returns:Unit<?>

the unit parsed from the specified character sub-sequence.

parseback to summary
public Unit<?> parse(CharSequence csq, ParsePosition cursor) throws MeasurementParseException

Implements abstract tech.units.indriya.format.AbstractUnitFormat.parse.

Implements javax.measure.format.UnitFormat.parse.

Doc from tech.units.indriya.format.AbstractUnitFormat.parse.

Parses a portion of the specified CharSequence from the specified position to produce a unit. If there is no unit to parse AbstractUnit#ONE is returned.

Parameters
csq:CharSequence

the CharSequence to parse.

cursor:ParsePosition

the cursor holding the current parsing index.

Returns:Unit<?>

the unit parsed from the specified character sub-sequence.

Exceptions
MeasurementParseException:

Doc from javax.measure.format.UnitFormat.parse.

if any problem occurs while parsing the specified character sequence (e.g. illegal syntax).

parseback to summary
public Unit<? extends Quantity<?>> parse(CharSequence csq) throws MeasurementParseException

Implements javax.measure.format.UnitFormat.parse.

Doc from javax.measure.format.UnitFormat.parse.

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<? extends Quantity<?>>

the unit parsed from the specified character sequence.

Annotations
@Override
Exceptions
MeasurementParseException:
if any problem occurs while parsing the specified character sequence (e.g. illegal syntax).
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override