Package tech.units.indriya.format
Class SimpleUnitFormat.DefaultFormat
- java.lang.Object
-
- tech.units.indriya.format.AbstractUnitFormat
-
- tech.units.indriya.format.SimpleUnitFormat
-
- tech.units.indriya.format.SimpleUnitFormat.DefaultFormat
-
- All Implemented Interfaces:
javax.measure.format.UnitFormat
- Direct Known Subclasses:
SimpleUnitFormat.ASCIIFormat
- Enclosing class:
- SimpleUnitFormat
protected static class SimpleUnitFormat.DefaultFormat extends SimpleUnitFormat
This class represents the standard format.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class tech.units.indriya.format.SimpleUnitFormat
SimpleUnitFormat.ASCIIFormat, SimpleUnitFormat.DefaultFormat, SimpleUnitFormat.Flavor
-
-
Field Summary
Fields Modifier and Type Field Description private static int
CLOSE_PAREN
private static int
DIVIDE
private static int
EOF
private static int
EXPONENT
private static int
FLOAT
private static int
IDENTIFIER
private static int
INTEGER
private static int
MULTIPLY
protected java.util.HashMap<java.lang.String,javax.measure.Unit<?>>
nameToUnit
Holds the name to unit mapping.private static int
OPEN_PAREN
private static int
PLUS
protected java.util.HashMap<javax.measure.Unit<?>,java.lang.String>
unitToName
Holds the unit to name mapping.
-
Constructor Summary
Constructors Modifier Constructor Description protected
DefaultFormat()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
alias(javax.measure.Unit<?> unit, java.lang.String alias)
Attaches a system-wide alias to this unit.private void
append(java.lang.Appendable appendable, java.lang.CharSequence symbol, int pow, int root)
private void
check(boolean expr, java.lang.String message, java.lang.CharSequence csq, int index)
java.lang.Appendable
format(javax.measure.Unit<?> unit, java.lang.Appendable appendable)
Formats the specified unit.protected SymbolMap
getSymbols()
Returns theSymbolMap
for this unit format.boolean
isLocaleSensitive()
protected static boolean
isUnitIdentifierPart(char ch)
boolean
isValidIdentifier(java.lang.String name)
Indicates if the specified name can be used as unit identifier.void
label(javax.measure.Unit<?> unit, java.lang.String label)
Attaches a system-wide label to the specified unit.protected java.lang.String
nameFor(javax.measure.Unit<?> unit)
private int
nextToken(java.lang.CharSequence csq, java.text.ParsePosition pos)
javax.measure.Unit<?>
parse(java.lang.CharSequence csq)
protected javax.measure.Unit<?>
parse(java.lang.CharSequence csq, int index)
Parses a portion of the specifiedCharSequence
from the specified position to produce a unit.protected javax.measure.Unit<?>
parse(java.lang.CharSequence csq, java.text.ParsePosition cursor)
Parses a portion of the specifiedCharSequence
from the specified position to produce a unit.javax.measure.Unit<? extends javax.measure.Quantity>
parseProductUnit(java.lang.CharSequence csq, java.text.ParsePosition pos)
Parses a sequence of character to produce a unit or a rational product of unit.javax.measure.Unit<? extends javax.measure.Quantity>
parseSingleUnit(java.lang.CharSequence csq, java.text.ParsePosition pos)
Parses a sequence of character to produce a single unit.protected java.lang.String
prefixFor(javax.measure.UnitConverter converter)
private double
readDouble(java.lang.CharSequence csq, java.text.ParsePosition pos)
private SimpleUnitFormat.Exponent
readExponent(java.lang.CharSequence csq, java.text.ParsePosition pos)
private java.lang.String
readIdentifier(java.lang.CharSequence csq, java.text.ParsePosition pos)
private long
readLong(java.lang.CharSequence csq, java.text.ParsePosition pos)
protected javax.measure.Unit<?>
unitFor(java.lang.String name)
-
Methods inherited from class tech.units.indriya.format.SimpleUnitFormat
format, getInstance, getInstance, isAllASCII, parseObject
-
Methods inherited from class tech.units.indriya.format.AbstractUnitFormat
format, format
-
-
-
-
Field Detail
-
EOF
private static final int EOF
- See Also:
- Constant Field Values
-
IDENTIFIER
private static final int IDENTIFIER
- See Also:
- Constant Field Values
-
OPEN_PAREN
private static final int OPEN_PAREN
- See Also:
- Constant Field Values
-
CLOSE_PAREN
private static final int CLOSE_PAREN
- See Also:
- Constant Field Values
-
EXPONENT
private static final int EXPONENT
- See Also:
- Constant Field Values
-
MULTIPLY
private static final int MULTIPLY
- See Also:
- Constant Field Values
-
DIVIDE
private static final int DIVIDE
- See Also:
- Constant Field Values
-
PLUS
private static final int PLUS
- See Also:
- Constant Field Values
-
INTEGER
private static final int INTEGER
- See Also:
- Constant Field Values
-
FLOAT
private static final int FLOAT
- See Also:
- Constant Field Values
-
nameToUnit
protected final java.util.HashMap<java.lang.String,javax.measure.Unit<?>> nameToUnit
Holds the name to unit mapping.
-
unitToName
protected final java.util.HashMap<javax.measure.Unit<?>,java.lang.String> unitToName
Holds the unit to name mapping.
-
-
Method Detail
-
label
public void label(javax.measure.Unit<?> unit, java.lang.String label)
Description copied from class:SimpleUnitFormat
Attaches a system-wide label to the specified unit. For example:SimpleUnitFormat.getInstance().label(DAY.multiply(365), "year"); SimpleUnitFormat.getInstance().label(METER.multiply(0.3048), "ft");
If the specified label is already associated to an unit the previous association is discarded or ignored.- Specified by:
label
in interfacejavax.measure.format.UnitFormat
- Specified by:
label
in classSimpleUnitFormat
- Parameters:
unit
- the unit being labeled.label
- the new label for this unit.
-
alias
public void alias(javax.measure.Unit<?> unit, java.lang.String alias)
Description copied from class:SimpleUnitFormat
Attaches a system-wide alias to this unit. Multiple aliases may be attached to the same unit. Aliases are used during parsing to recognize different variants of the same unit. For example:SimpleUnitFormat.getInstance().alias(METER.multiply(0.3048), "foot"); SimpleUnitFormat.getInstance().alias(METER.multiply(0.3048), "feet"); SimpleUnitFormat.getInstance().alias(METER, "meter"); SimpleUnitFormat.getInstance().alias(METER, "metre");
If the specified label is already associated to an unit the previous association is discarded or ignored.- Specified by:
alias
in classSimpleUnitFormat
- Parameters:
unit
- the unit being aliased.alias
- the alias attached to this unit.
-
isValidIdentifier
public boolean isValidIdentifier(java.lang.String name)
Description copied from class:SimpleUnitFormat
Indicates if the specified name can be used as unit identifier.- Specified by:
isValidIdentifier
in classSimpleUnitFormat
- Parameters:
name
- the identifier to be tested.- Returns:
true
if the name specified can be used as label or alias for this format;false
otherwise.
-
isUnitIdentifierPart
protected static boolean isUnitIdentifierPart(char ch)
-
nameFor
protected java.lang.String nameFor(javax.measure.Unit<?> unit)
-
prefixFor
protected java.lang.String prefixFor(javax.measure.UnitConverter converter)
-
unitFor
protected javax.measure.Unit<?> unitFor(java.lang.String name)
-
parseSingleUnit
public javax.measure.Unit<? extends javax.measure.Quantity> parseSingleUnit(java.lang.CharSequence csq, java.text.ParsePosition pos) throws javax.measure.format.ParserException
Description copied from class:SimpleUnitFormat
Parses a sequence of character to produce a single unit.- Specified by:
parseSingleUnit
in classSimpleUnitFormat
- Parameters:
csq
- theCharSequence
to parse.pos
- an object holding the parsing index and error position.- Returns:
- an
Unit
parsed from the character sequence. - Throws:
javax.measure.format.ParserException
-
parseProductUnit
public javax.measure.Unit<? extends javax.measure.Quantity> parseProductUnit(java.lang.CharSequence csq, java.text.ParsePosition pos) throws javax.measure.format.ParserException
Description copied from class:SimpleUnitFormat
Parses a sequence of character to produce a unit or a rational product of unit.- Specified by:
parseProductUnit
in classSimpleUnitFormat
- Parameters:
csq
- theCharSequence
to parse.pos
- an object holding the parsing index and error position.- Returns:
- an
Unit
parsed from the character sequence. - Throws:
javax.measure.format.ParserException
-
nextToken
private int nextToken(java.lang.CharSequence csq, java.text.ParsePosition pos)
-
check
private void check(boolean expr, java.lang.String message, java.lang.CharSequence csq, int index) throws javax.measure.format.ParserException
- Throws:
javax.measure.format.ParserException
-
readExponent
private SimpleUnitFormat.Exponent readExponent(java.lang.CharSequence csq, java.text.ParsePosition pos)
-
readLong
private long readLong(java.lang.CharSequence csq, java.text.ParsePosition pos)
-
readDouble
private double readDouble(java.lang.CharSequence csq, java.text.ParsePosition pos)
-
readIdentifier
private java.lang.String readIdentifier(java.lang.CharSequence csq, java.text.ParsePosition pos)
-
format
public java.lang.Appendable format(javax.measure.Unit<?> unit, java.lang.Appendable appendable) throws java.io.IOException
Description copied from class:SimpleUnitFormat
Formats the specified unit.- Specified by:
format
in interfacejavax.measure.format.UnitFormat
- Specified by:
format
in classSimpleUnitFormat
- Parameters:
unit
- the unit to format.appendable
- the appendable destination.- Returns:
- The appendable destination passed in as
appendable
, with formatted text appended. - Throws:
java.io.IOException
- if an error occurs.
-
append
private void append(java.lang.Appendable appendable, java.lang.CharSequence symbol, int pow, int root) throws java.io.IOException
- Throws:
java.io.IOException
-
parse
public javax.measure.Unit<?> parse(java.lang.CharSequence csq) throws javax.measure.format.ParserException
- Throws:
javax.measure.format.ParserException
-
getSymbols
protected SymbolMap getSymbols()
Description copied from class:AbstractUnitFormat
Returns theSymbolMap
for this unit format.- Specified by:
getSymbols
in classAbstractUnitFormat
- Returns:
- the symbol map used by this format.
-
parse
protected javax.measure.Unit<?> parse(java.lang.CharSequence csq, int index) throws java.lang.IllegalArgumentException
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:
java.lang.IllegalArgumentException
- if any problem occurs while parsing the specified character sequence (e.g. illegal syntax).
-
parse
protected javax.measure.Unit<?> parse(java.lang.CharSequence csq, java.text.ParsePosition cursor) throws java.lang.IllegalArgumentException
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:
java.lang.IllegalArgumentException
- if any problem occurs while parsing the specified character sequence (e.g. illegal syntax).
-
isLocaleSensitive
public boolean isLocaleSensitive()
-
-