Package tech.units.indriya.format
Class SimpleUnitFormat
java.lang.Object
tech.units.indriya.format.AbstractUnitFormat
tech.units.indriya.format.SimpleUnitFormat
- All Implemented Interfaces:
javax.measure.format.UnitFormat
- Direct Known Subclasses:
SimpleUnitFormat.DefaultFormat
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))
- Since:
- 1.0
- Version:
- 1.5, April 15, 2019
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static final class
This class represents the ASCII format.protected static class
This class represents the standard format.private static class
This class represents an exponent with both a power (numerator) and a root (denominator).static enum
Flavor of this format -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final SimpleUnitFormat.ASCIIFormat
Holds the ASCIIFormat unit format.private static final SimpleUnitFormat.DefaultFormat
Holds the standard unit format.private static final String
private static final javax.measure.UnitConverter[]
private static final String[]
private static final Prefix[]
private static final javax.measure.Unit<?>[]
Holds the unique symbols collection (base units or alternate units). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract void
Attaches a system-wide alias to this unit.private static String
asciiPrefix
(String prefix) private static String
final StringBuffer
format
(Object unit, StringBuffer toAppendTo, FieldPosition pos) Formats an unit and appends the resulting text to a given string buffer (implementsjava.text.Format
).abstract Appendable
format
(javax.measure.Unit<?> unit, Appendable appendable) Formats the specified unit.static SimpleUnitFormat
Returns the unit format for the default locale (format used byAbstractUnit.parse(CharSequence)
andUnit.toString()
).static SimpleUnitFormat
getInstance
(SimpleUnitFormat.Flavor flavor) Returns theSimpleUnitFormat
in the desiredSimpleUnitFormat.Flavor
protected static boolean
isAllASCII
(String input) to check if a string only contains US-ASCII charactersabstract boolean
isValidIdentifier
(String name) Indicates if the specified name can be used as unit identifier.abstract void
Attaches a system-wide label to the specified unit.final javax.measure.Unit
<?> parseObject
(String source, ParsePosition pos) Parses the text from a string to produce an object (implementsjava.text.Format
).abstract javax.measure.Unit
<? extends javax.measure.Quantity> parseProductUnit
(CharSequence csq, 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
(CharSequence csq, ParsePosition pos) Parses a sequence of character to produce a single unit.Methods inherited from class tech.units.indriya.format.AbstractUnitFormat
format, format, getSymbols, parse, parse
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface javax.measure.format.UnitFormat
isLocaleSensitive, parse
-
Field Details
-
SI_UNITS
private static final javax.measure.Unit<?>[] SI_UNITS -
PREFIXES
-
PREFIX_SYMBOLS
-
PREFIX_CONVERTERS
private static final javax.measure.UnitConverter[] PREFIX_CONVERTERS -
DEFAULT
Holds the standard unit format. -
ASCII
Holds the ASCIIFormat unit format. -
SYMBOL_TO_UNIT
Holds the unique symbols collection (base units or alternate units). -
MU
- See Also:
-
-
Constructor Details
-
SimpleUnitFormat
protected SimpleUnitFormat()Base constructor.
-
-
Method Details
-
getInstance
Returns the unit format for the default locale (format used byAbstractUnit.parse(CharSequence)
andUnit.toString()
).- Returns:
- the default unit format (locale sensitive).
-
getInstance
Returns theSimpleUnitFormat
in the desiredSimpleUnitFormat.Flavor
- Returns:
- the instance for the given
SimpleUnitFormat.Flavor
.
-
format
public abstract Appendable format(javax.measure.Unit<?> unit, Appendable appendable) throws IOException 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.
-
parseProductUnit
public abstract javax.measure.Unit<? extends javax.measure.Quantity> parseProductUnit(CharSequence csq, ParsePosition pos) throws javax.measure.format.ParserException Parses a sequence of character to produce a unit or a rational product of unit.- Parameters:
csq
- theCharSequence
to parse.pos
- an object holding the parsing index and error position.- Returns:
- an
Unit
parsed from the character sequence. - Throws:
IllegalArgumentException
- if the character sequence contains an illegal syntax.javax.measure.format.ParserException
-
parseSingleUnit
public abstract javax.measure.Unit<? extends javax.measure.Quantity> parseSingleUnit(CharSequence csq, ParsePosition pos) throws javax.measure.format.ParserException Parses a sequence of character to produce a single unit.- Parameters:
csq
- theCharSequence
to parse.pos
- an object holding the parsing index and error position.- Returns:
- an
Unit
parsed from the character sequence. - Throws:
IllegalArgumentException
- if the character sequence does not contain a valid unit identifier.javax.measure.format.ParserException
-
label
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
- Overrides:
label
in classAbstractUnitFormat
- Parameters:
unit
- the unit being labeled.label
- the new label for this unit.- Throws:
IllegalArgumentException
- if the label is not aisValidIdentifier(String)
valid identifier.
-
alias
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.- Parameters:
unit
- the unit being aliased.alias
- the alias attached to this unit.- Throws:
IllegalArgumentException
- if the label is not aisValidIdentifier(String)
valid identifier.
-
isValidIdentifier
Indicates if the specified name can be used as unit identifier.- 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.
-
format
Formats an unit and appends the resulting text to a given string buffer (implementsjava.text.Format
).- Parameters:
unit
- the unit to format.toAppendTo
- where the text is to be appendedpos
- the field position (not used).- Returns:
toAppendTo
-
parseObject
public final javax.measure.Unit<?> parseObject(String source, ParsePosition pos) throws javax.measure.format.ParserException Parses the text from a string to produce an object (implementsjava.text.Format
).- Parameters:
source
- the string source, part of which should be parsed.pos
- the cursor position.- Returns:
- the corresponding unit or
null
if the string cannot be parsed. - Throws:
javax.measure.format.ParserException
-
asciiPrefix
-
asciiSymbol
-
isAllASCII
to check if a string only contains US-ASCII characters
-