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
public abstract class SimpleUnitFormat extends AbstractUnitFormat
This class implements the
UnitFormatinterface for formatting and parsingunits.For all SI units, the 20 SI prefixes used to form decimal multiples and sub-multiples of SI units are recognized.
Unitsare 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 Classes Modifier and Type Class Description protected static classSimpleUnitFormat.ASCIIFormatThis class represents the ASCII format.protected static classSimpleUnitFormat.DefaultFormatThis class represents the standard format.private static classSimpleUnitFormat.ExponentThis class represents an exponent with both a power (numerator) and a root (denominator).static classSimpleUnitFormat.FlavorFlavor of this format
-
Field Summary
Fields Modifier and Type Field Description private static SimpleUnitFormat.ASCIIFormatASCIIHolds the ASCIIFormat unit format.private static SimpleUnitFormat.DefaultFormatDEFAULTHolds the standard unit format.private static java.lang.StringMUprivate static javax.measure.UnitConverter[]PREFIX_CONVERTERSprivate static java.lang.String[]PREFIX_SYMBOLSprivate static Prefix[]PREFIXESprivate static javax.measure.Unit<?>[]SI_UNITSprivate static java.util.Map<java.lang.String,javax.measure.Unit<?>>SYMBOL_TO_UNITHolds the unique symbols collection (base units or alternate units).
-
Constructor Summary
Constructors Modifier Constructor Description protectedSimpleUnitFormat()Base constructor.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract voidalias(javax.measure.Unit<?> unit, java.lang.String alias)Attaches a system-wide alias to this unit.private static java.lang.StringasciiPrefix(java.lang.String prefix)private static java.lang.StringasciiSymbol(java.lang.String s)java.lang.StringBufferformat(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 (implementsjava.text.Format).abstract java.lang.Appendableformat(javax.measure.Unit<?> unit, java.lang.Appendable appendable)Formats the specified unit.static SimpleUnitFormatgetInstance()Returns the unit format for the default locale (format used byAbstractUnit.parse(CharSequence)andUnit.toString()).static SimpleUnitFormatgetInstance(SimpleUnitFormat.Flavor flavor)Returns theSimpleUnitFormatin the desiredSimpleUnitFormat.Flavorprotected static booleanisAllASCII(java.lang.String input)to check if a string only contains US-ASCII charactersabstract booleanisValidIdentifier(java.lang.String name)Indicates if the specified name can be used as unit identifier.abstract voidlabel(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 (implementsjava.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.-
Methods inherited from class tech.units.indriya.format.AbstractUnitFormat
format, format, getSymbols, parse, parse
-
-
-
-
Field Detail
-
SI_UNITS
private static final javax.measure.Unit<?>[] SI_UNITS
-
PREFIXES
private static final Prefix[] PREFIXES
-
PREFIX_SYMBOLS
private static final java.lang.String[] PREFIX_SYMBOLS
-
PREFIX_CONVERTERS
private static final javax.measure.UnitConverter[] PREFIX_CONVERTERS
-
DEFAULT
private static final SimpleUnitFormat.DefaultFormat DEFAULT
Holds the standard unit format.
-
ASCII
private static final SimpleUnitFormat.ASCIIFormat ASCII
Holds the ASCIIFormat unit format.
-
SYMBOL_TO_UNIT
private static final java.util.Map<java.lang.String,javax.measure.Unit<?>> SYMBOL_TO_UNIT
Holds the unique symbols collection (base units or alternate units).
-
MU
private static final java.lang.String MU
- See Also:
- Constant Field Values
-
-
Method Detail
-
getInstance
public static SimpleUnitFormat getInstance()
Returns the unit format for the default locale (format used byAbstractUnit.parse(CharSequence)andUnit.toString()).- Returns:
- the default unit format (locale sensitive).
-
getInstance
public static SimpleUnitFormat getInstance(SimpleUnitFormat.Flavor flavor)
Returns theSimpleUnitFormatin the desiredSimpleUnitFormat.Flavor- Returns:
- the instance for the given
SimpleUnitFormat.Flavor.
-
format
public abstract java.lang.Appendable format(javax.measure.Unit<?> unit, java.lang.Appendable appendable) throws java.io.IOExceptionFormats the specified unit.- Specified by:
formatin interfacejavax.measure.format.UnitFormat- Specified by:
formatin classAbstractUnitFormat- 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.
-
parseProductUnit
public abstract javax.measure.Unit<? extends javax.measure.Quantity> parseProductUnit(java.lang.CharSequence csq, java.text.ParsePosition pos) throws javax.measure.format.ParserExceptionParses a sequence of character to produce a unit or a rational product of unit.- Parameters:
csq- theCharSequenceto parse.pos- an object holding the parsing index and error position.- Returns:
- an
Unitparsed from the character sequence. - Throws:
java.lang.IllegalArgumentException- if the character sequence contains an illegal syntax.javax.measure.format.ParserException
-
parseSingleUnit
public abstract javax.measure.Unit<? extends javax.measure.Quantity> parseSingleUnit(java.lang.CharSequence csq, java.text.ParsePosition pos) throws javax.measure.format.ParserExceptionParses a sequence of character to produce a single unit.- Parameters:
csq- theCharSequenceto parse.pos- an object holding the parsing index and error position.- Returns:
- an
Unitparsed from the character sequence. - Throws:
java.lang.IllegalArgumentException- if the character sequence does not contain a valid unit identifier.javax.measure.format.ParserException
-
label
public abstract void label(javax.measure.Unit<?> unit, java.lang.String 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:
labelin interfacejavax.measure.format.UnitFormat- Overrides:
labelin classAbstractUnitFormat- Parameters:
unit- the unit being labeled.label- the new label for this unit.- Throws:
java.lang.IllegalArgumentException- if the label is not aisValidIdentifier(String)valid identifier.
-
alias
public abstract void alias(javax.measure.Unit<?> unit, java.lang.String 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:
java.lang.IllegalArgumentException- if the label is not aisValidIdentifier(String)valid identifier.
-
isValidIdentifier
public abstract boolean isValidIdentifier(java.lang.String name)
Indicates if the specified name can be used as unit identifier.- Parameters:
name- the identifier to be tested.- Returns:
trueif the name specified can be used as label or alias for this format;falseotherwise.
-
format
public final 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 (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(java.lang.String source, java.text.ParsePosition pos) throws javax.measure.format.ParserExceptionParses 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
nullif the string cannot be parsed. - Throws:
javax.measure.format.ParserException
-
asciiPrefix
private static java.lang.String asciiPrefix(java.lang.String prefix)
-
asciiSymbol
private static java.lang.String asciiSymbol(java.lang.String s)
-
isAllASCII
protected static boolean isAllASCII(java.lang.String input)
to check if a string only contains US-ASCII characters
-
-