public class LocalUnitFormat extends AbstractUnitFormat
This class represents the local sensitive format.
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> | := | <compound_expr> |
<compound_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> ")" ) |
Modifier and Type | Field and Description |
---|---|
private static int |
ADDITION_PRECEDENCE
Operator precedence for the addition and subtraction operations
|
private static LocalUnitFormat |
DEFAULT_INSTANCE
DefaultQuantityFactory locale instance.
|
private static int |
EXPONENT_PRECEDENCE
Operator precedence for the exponentiation and logarithm operations
|
private static char |
MIDDLE_DOT
Multiplicand character
|
private static int |
NOOP_PRECEDENCE
Operator precedence for a unit identifier containing no mathematical operations (i.e., consisting exclusively of an identifier and possibly a
prefix).
|
private static int |
PRODUCT_PRECEDENCE
Operator precedence for the multiplication and division operations
|
private SymbolMap |
symbolMap
The symbol map used by this instance to map between
Unit s and String s, etc... |
Modifier | Constructor and Description |
---|---|
private |
LocalUnitFormat(SymbolMap symbols)
Base constructor.
|
Modifier and Type | Method and Description |
---|---|
java.lang.Appendable |
format(javax.measure.Unit<?> unit,
java.lang.Appendable appendable)
Formats the specified unit.
|
private int |
formatConverter(javax.measure.UnitConverter converter,
boolean continued,
int unitPrecedence,
java.lang.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,
java.lang.Appendable buffer)
Format the given unit raised to the given fractional power to the given
StringBuffer . |
private int |
formatInternal(javax.measure.Unit<?> unit,
java.lang.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 |
getInstance()
Returns the instance for the current default locale (non-ascii characters are allowed)
|
static LocalUnitFormat |
getInstance(java.util.Locale locale)
Returns an instance for the given locale.
|
static LocalUnitFormat |
getInstance(SymbolMap symbols)
Returns an instance for the given symbol map.
|
protected SymbolMap |
getSymbols()
Get the symbol map used by this instance to map between
Unit s and String s, etc... |
boolean |
isLocaleSensitive() |
javax.measure.Unit<? extends javax.measure.Quantity<?>> |
parse(java.lang.CharSequence csq) |
protected javax.measure.Unit<?> |
parse(java.lang.CharSequence csq,
int index)
Parses a portion of the specified
CharSequence from the specified position to produce a unit. |
javax.measure.Unit<?> |
parse(java.lang.CharSequence csq,
java.text.ParsePosition cursor)
Parses a portion of the specified
CharSequence from the specified position to produce a unit. |
format, format, label
private static final LocalUnitFormat DEFAULT_INSTANCE
private static final char MIDDLE_DOT
private static final int ADDITION_PRECEDENCE
private static final int PRODUCT_PRECEDENCE
private static final int EXPONENT_PRECEDENCE
private static final int NOOP_PRECEDENCE
Integer.MAX_VALUE
so that no operator can have a higher precedence.private final transient SymbolMap symbolMap
Unit
s and String
s, etc...private LocalUnitFormat(SymbolMap symbols)
symbols
- the symbol mapping.public static LocalUnitFormat getInstance()
public static LocalUnitFormat getInstance(java.util.Locale locale)
locale
- public static LocalUnitFormat getInstance(SymbolMap symbols)
protected SymbolMap getSymbols()
Unit
s and String
s, etc...getSymbols
in class AbstractUnitFormat
public java.lang.Appendable format(javax.measure.Unit<?> unit, java.lang.Appendable appendable) throws java.io.IOException
AbstractUnitFormat
format
in interface javax.measure.format.UnitFormat
format
in class AbstractUnitFormat
unit
- the unit to format.appendable
- the appendable destination.appendable
, with formatted text appended.java.io.IOException
- if an error occurs.public boolean isLocaleSensitive()
protected javax.measure.Unit<?> parse(java.lang.CharSequence csq, int index) throws javax.measure.format.ParserException
AbstractUnitFormat
CharSequence
from the specified position to produce a unit. If there is no unit to parse
AbstractUnit.ONE
is returned.parse
in class AbstractUnitFormat
csq
- the CharSequence
to parse.index
- the current parsing index.javax.measure.format.ParserException
public javax.measure.Unit<?> parse(java.lang.CharSequence csq, java.text.ParsePosition cursor) throws javax.measure.format.ParserException
AbstractUnitFormat
CharSequence
from the specified position to produce a unit. If there is no unit to parse
AbstractUnit.ONE
is returned.parse
in class AbstractUnitFormat
csq
- the CharSequence
to parse.cursor
- the cursor holding the current parsing index.javax.measure.format.ParserException
public javax.measure.Unit<? extends javax.measure.Quantity<?>> parse(java.lang.CharSequence csq) throws javax.measure.format.ParserException
javax.measure.format.ParserException
private int formatInternal(javax.measure.Unit<?> unit, java.lang.Appendable buffer) throws java.io.IOException
ConverterFormat
for the constants that define the various precedence values.unit
- the unit to be formattedbuffer
- the StringBuilder
to be written tojava.io.IOException
private void formatExponent(javax.measure.Unit<?> unit, int pow, int root, boolean continued, java.lang.Appendable buffer) throws java.io.IOException
StringBuffer
.unit
- Unit the unit to be formattedpow
- int the numerator of the fractional powerroot
- int the denominator of the fractional powercontinued
- 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
- StringBuffer the buffer to append to. No assumptions should be made about its content.java.io.IOException
private int formatConverter(javax.measure.UnitConverter converter, boolean continued, int unitPrecedence, java.lang.StringBuilder buffer)
Compound
converter.converter
- the converter to be formattedcontinued
- true
if the converter expression should begin with an operator, otherwise false
.unitPrecedence
- the operator precedence of the operation expressed by the unit being modified by the given converter.buffer
- the StringBuffer
to append to.