public abstract class SimpleUnitFormat extends AbstractUnitFormat
This class implements the UnitFormat
interface for formatting and parsing units
.
For all SI units, the 20 SI prefixes used to form decimal multiples and sub-multiples of SI units are recognized. Units
are directly
recognized. For example:
AbstractUnit.parse("m°C").equals(MetricPrefix.MILLI(Units.CELSIUS))
AbstractUnit.parse("kW").equals(MetricPrefix.KILO(Units.WATT))
AbstractUnit.parse("ft").equals(Units.METRE.multiply(0.3048))
Modifier and Type | Class and Description |
---|---|
protected static class |
SimpleUnitFormat.ASCIIFormat
This class represents the ASCII format.
|
protected static class |
SimpleUnitFormat.DefaultFormat
This class represents the standard format.
|
private static class |
SimpleUnitFormat.Exponent
This class represents an exponent with both a power (numerator) and a root (denominator).
|
static class |
SimpleUnitFormat.Flavor
Flavor of this format
|
Modifier and Type | Field and Description |
---|---|
private static SimpleUnitFormat.ASCIIFormat |
ASCII
Holds the ASCIIFormat unit format.
|
private static javax.measure.UnitConverter[] |
CONVERTERS |
private static SimpleUnitFormat.DefaultFormat |
DEFAULT
Holds the standard unit format.
|
private static java.lang.String[] |
PREFIXES |
private static javax.measure.Unit<?>[] |
SI_UNITS |
private static java.util.Map<java.lang.String,javax.measure.Unit<?>> |
SYMBOL_TO_UNIT
Holds the unique symbols collection (base units or alternate units).
|
Modifier | Constructor and Description |
---|---|
protected |
SimpleUnitFormat()
Base constructor.
|
Modifier and Type | Method and Description |
---|---|
abstract void |
alias(javax.measure.Unit<?> unit,
java.lang.String alias)
Attaches a system-wide alias to this unit.
|
private static java.lang.String |
asciiPrefix(java.lang.String prefix) |
java.lang.StringBuffer |
format(java.lang.Object unit,
java.lang.StringBuffer toAppendTo,
java.text.FieldPosition pos)
Formats an unit and appends the resulting text to a given string buffer (implements
java.text.Format ). |
abstract java.lang.Appendable |
format(javax.measure.Unit<?> unit,
java.lang.Appendable appendable)
Formats the specified unit.
|
static SimpleUnitFormat |
getInstance()
Returns the unit format for the default locale (format used by
AbstractUnit.parse(CharSequence) and
Unit.toString() ). |
static SimpleUnitFormat |
getInstance(SimpleUnitFormat.Flavor flavor)
Returns the
SimpleUnitFormat in the desired SimpleUnitFormat.Flavor |
protected static boolean |
isAllASCII(java.lang.String input) |
boolean |
isLocaleSensitive() |
abstract boolean |
isValidIdentifier(java.lang.String name)
Indicates if the specified name can be used as unit identifier.
|
abstract void |
label(javax.measure.Unit<?> unit,
java.lang.String label)
Attaches a system-wide label to the specified unit.
|
javax.measure.Unit<?> |
parseObject(java.lang.String source,
java.text.ParsePosition pos)
Parses the text from a string to produce an object (implements
java.text.Format ). |
abstract 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.
|
abstract 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.
|
format, format, getSymbols, parse, parse
private static final SimpleUnitFormat.DefaultFormat DEFAULT
private static final SimpleUnitFormat.ASCIIFormat ASCII
private static final java.util.Map<java.lang.String,javax.measure.Unit<?>> SYMBOL_TO_UNIT
private static final javax.measure.Unit<?>[] SI_UNITS
private static final java.lang.String[] PREFIXES
private static final javax.measure.UnitConverter[] CONVERTERS
public static SimpleUnitFormat getInstance()
AbstractUnit.parse(CharSequence)
and
Unit.toString()
).public static SimpleUnitFormat getInstance(SimpleUnitFormat.Flavor flavor)
SimpleUnitFormat
in the desired SimpleUnitFormat.Flavor
SimpleUnitFormat.Flavor
.public abstract java.lang.Appendable format(javax.measure.Unit<?> unit, java.lang.Appendable appendable) throws java.io.IOException
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 abstract javax.measure.Unit<? extends javax.measure.Quantity> parseProductUnit(java.lang.CharSequence csq, java.text.ParsePosition pos) throws javax.measure.format.ParserException
csq
- the CharSequence
to parse.pos
- an object holding the parsing index and error position.Unit
parsed from the character sequence.java.lang.IllegalArgumentException
- if the character sequence contains an illegal syntax.javax.measure.format.ParserException
public abstract javax.measure.Unit<? extends javax.measure.Quantity> parseSingleUnit(java.lang.CharSequence csq, java.text.ParsePosition pos) throws javax.measure.format.ParserException
csq
- the CharSequence
to parse.pos
- an object holding the parsing index and error position.Unit
parsed from the character sequence.java.lang.IllegalArgumentException
- if the character sequence does not contain a valid unit identifier.javax.measure.format.ParserException
public abstract void label(javax.measure.Unit<?> unit, java.lang.String label)
label
in interface javax.measure.format.UnitFormat
label
in class AbstractUnitFormat
unit
- the unit being labelled.label
- the new label for this unit.java.lang.IllegalArgumentException
- if the label is not a isValidIdentifier(String)
valid identifier.public boolean isLocaleSensitive()
public abstract void alias(javax.measure.Unit<?> unit, java.lang.String alias)
unit
- the unit being aliased.alias
- the alias attached to this unit.java.lang.IllegalArgumentException
- if the label is not a isValidIdentifier(String)
valid identifier.public abstract boolean isValidIdentifier(java.lang.String name)
name
- the identifier to be tested.true
if the name specified can be used as label or alias for this format;false
otherwise.public final java.lang.StringBuffer format(java.lang.Object unit, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)
java.text.Format
).unit
- the unit to format.toAppendTo
- where the text is to be appendedpos
- the field position (not used).toAppendTo
public final javax.measure.Unit<?> parseObject(java.lang.String source, java.text.ParsePosition pos) throws javax.measure.format.ParserException
java.text.Format
).source
- the string source, part of which should be parsed.pos
- the cursor position.null
if the string cannot be parsed.javax.measure.format.ParserException
private static java.lang.String asciiPrefix(java.lang.String prefix)
protected static boolean isAllASCII(java.lang.String input)