Package tech.units.indriya.format
Class LocalUnitFormat
java.lang.Object
tech.units.indriya.format.AbstractUnitFormat
tech.units.indriya.format.LocalUnitFormat
- All Implemented Interfaces:
javax.measure.format.UnitFormat
This class represents the local 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
<table width="90%" * align="center">| ( "^(" (<sign>)? <integer> ( "/" (<sign>)? <integer> )? ")" )
| ( <superscript_digit> )+
| (<integer> "^" <atomic_expr>)
| ( ( "log" ( <integer> )? ) | "ln" ) "(" <add_expr> ")" )
| <unit_identifier>
| ( "(" <add_expr> ")" )
- Since:
- 1.0
- Version:
- 1.0.2, April 30, 2017
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
Operator precedence for the addition and subtraction operationsprivate static final LocalUnitFormat
DefaultQuantityFactory locale instance.private static final int
Operator precedence for the exponentiation and logarithm operationsprivate static final char
Multiplicand characterprivate static final int
Operator precedence for a unit identifier containing no mathematical operations (i.e., consisting exclusively of an identifier and possibly a prefix).private static final int
Operator precedence for the multiplication and division operationsprivate final SymbolMap
The symbol map used by this instance to map betweenUnit
s andString
s, etc... -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionformat
(javax.measure.Unit<?> unit, Appendable appendable) Formats the specified unit.private int
formatConverter
(javax.measure.UnitConverter converter, boolean continued, int unitPrecedence, StringBuilder buffer) Formats the given converter to the given StringBuffer and returns the operator precedence of the converter's mathematical operation.private void
formatExponent
(javax.measure.Unit<?> unit, int pow, int root, boolean continued, Appendable buffer) Format the given unit raised to the given fractional power to the givenStringBuffer
.private int
formatInternal
(javax.measure.Unit<?> unit, Appendable 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.static LocalUnitFormat
Returns the instance for the current default locale (non-ascii characters are allowed)static LocalUnitFormat
getInstance
(Locale locale) Returns an instance for the given locale.static LocalUnitFormat
getInstance
(SymbolMap symbols) Returns an instance for the given symbol map.protected SymbolMap
Get the symbol map used by this instance to map betweenUnit
s andString
s, etc...boolean
javax.measure.Unit<? extends javax.measure.Quantity<?>>
parse
(CharSequence csq) protected javax.measure.Unit<?>
parse
(CharSequence csq, int index) Parses a portion of the specifiedCharSequence
from the specified position to produce a unit.javax.measure.Unit<?>
parse
(CharSequence csq, ParsePosition cursor) Parses a portion of the specifiedCharSequence
from the specified position to produce a unit.Methods inherited from class tech.units.indriya.format.AbstractUnitFormat
format, format, label
-
Field Details
-
DEFAULT_INSTANCE
DefaultQuantityFactory locale instance. If the default locale is changed after the class is initialized, this instance will no longer be used. -
MIDDLE_DOT
private static final char MIDDLE_DOTMultiplicand character- See Also:
-
ADDITION_PRECEDENCE
private static final int ADDITION_PRECEDENCEOperator precedence for the addition and subtraction operations- See Also:
-
PRODUCT_PRECEDENCE
private static final int PRODUCT_PRECEDENCEOperator precedence for the multiplication and division operations- See Also:
-
EXPONENT_PRECEDENCE
private static final int EXPONENT_PRECEDENCEOperator precedence for the exponentiation and logarithm operations- See Also:
-
NOOP_PRECEDENCE
private static final int NOOP_PRECEDENCEOperator precedence for a unit identifier containing no mathematical operations (i.e., consisting exclusively of an identifier and possibly a prefix). Defined to beInteger.MAX_VALUE
so that no operator can have a higher precedence.- See Also:
-
symbolMap
The symbol map used by this instance to map betweenUnit
s andString
s, etc...
-
-
Constructor Details
-
LocalUnitFormat
Base constructor.- Parameters:
symbols
- the symbol mapping.
-
-
Method Details
-
getInstance
Returns the instance for the current default locale (non-ascii characters are allowed) -
getInstance
Returns an instance for the given locale.- Parameters:
locale
-
-
getInstance
Returns an instance for the given symbol map. -
getSymbols
Get the symbol map used by this instance to map betweenUnit
s andString
s, etc...- Specified by:
getSymbols
in classAbstractUnitFormat
- Returns:
- SymbolMap the current symbol map
-
format
Description copied from class:AbstractUnitFormat
Formats the specified unit.- Specified by:
format
in interfacejavax.measure.format.UnitFormat
- Specified by:
format
in classAbstractUnitFormat
- Parameters:
unit
- the unit to format.appendable
- the appendable destination.- Returns:
- The appendable destination passed in as
appendable
, with formatted text appended. - Throws:
IOException
- if an error occurs.
-
isLocaleSensitive
public boolean isLocaleSensitive() -
parse
protected javax.measure.Unit<?> parse(CharSequence csq, int index) throws javax.measure.format.ParserException Description copied from class:AbstractUnitFormat
Parses a portion of the specifiedCharSequence
from the specified position to produce a unit. If there is no unit to parseAbstractUnit.ONE
is returned.- Specified by:
parse
in classAbstractUnitFormat
- Parameters:
csq
- theCharSequence
to parse.index
- the current parsing index.- Returns:
- the unit parsed from the specified character sub-sequence.
- Throws:
javax.measure.format.ParserException
-
parse
public javax.measure.Unit<?> parse(CharSequence csq, ParsePosition cursor) throws javax.measure.format.ParserException Description copied from class:AbstractUnitFormat
Parses a portion of the specifiedCharSequence
from the specified position to produce a unit. If there is no unit to parseAbstractUnit.ONE
is returned.- Specified by:
parse
in classAbstractUnitFormat
- Parameters:
csq
- theCharSequence
to parse.cursor
- the cursor holding the current parsing index.- Returns:
- the unit parsed from the specified character sub-sequence.
- Throws:
javax.measure.format.ParserException
-
parse
public javax.measure.Unit<? extends javax.measure.Quantity<?>> parse(CharSequence csq) throws javax.measure.format.ParserException - Throws:
javax.measure.format.ParserException
-
formatInternal
Format the given unit to the given StringBuilder, then return the operator precedence of the outermost operator in the unit expression that was formatted. SeeConverterFormat
for the constants that define the various precedence values.- Parameters:
unit
- the unit to be formattedbuffer
- theStringBuilder
to be written to- Returns:
- the operator precedence of the outermost operator in the unit expression that was output
- Throws:
IOException
-
formatExponent
private void formatExponent(javax.measure.Unit<?> unit, int pow, int root, boolean continued, Appendable buffer) throws IOException Format the given unit raised to the given fractional power to the givenStringBuffer
.- Parameters:
unit
- Unit the unit to be formattedpow
- int the numerator of the fractional powerroot
- int the denominator of the fractional powercontinued
- booleantrue
if the converter expression should begin with an operator, otherwisefalse
. This will always be true unless the unit being modified is equal to Unit.ONE.buffer
- StringBuffer the buffer to append to. No assumptions should be made about its content.- Throws:
IOException
-
formatConverter
private int formatConverter(javax.measure.UnitConverter converter, boolean continued, int unitPrecedence, StringBuilder buffer) Formats the given converter to the given StringBuffer and returns the operator precedence of the converter's mathematical operation. This is the default implementation, which supports all built-in UnitConverter implementations. Note that it recursively calls itself in the case of aCompound
converter.- Parameters:
converter
- the converter to be formattedcontinued
-true
if the converter expression should begin with an operator, otherwisefalse
.unitPrecedence
- the operator precedence of the operation expressed by the unit being modified by the given converter.buffer
- theStringBuffer
to append to.- Returns:
- the operator precedence of the given UnitConverter
-