javax.measure.unit
Class UnitFormat
- java.lang.Object
-
- java.text.Format
-
- javax.measure.unit.UnitFormat
-
- All Implemented Interfaces:
- java.io.Serializable, java.lang.Cloneable
public abstract class UnitFormat extends java.text.FormatThis class provides the interface for formatting and parsing
units.For all
SIunits, the 20 SI prefixes used to form decimal multiples and sub-multiples of SI units are recognized.NonSIunits are directly recognized. For example:Unit.valueOf("m°C").equals(SI.MILLI(SI.CELSIUS)) Unit.valueOf("kW").equals(SI.KILO(SI.WATT)) Unit.valueOf("ft").equals(SI.METER.multiply(0.3048))- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method and Description abstract voidalias(Unit<?> unit, java.lang.String alias)Attaches a system-wide alias to this unit.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(Unit<?> unit, java.lang.Appendable appendable)Formats the specified unit.static UnitFormatgetInstance()Returns the unit format for the default locale (format used byUnit.valueOf(CharSequence)andUnit.toString()).static UnitFormatgetInstance(java.util.Locale inLocale)Returns the unit format for the specified locale.static UnitFormatgetUCUMInstance()Returns the UCUM international unit format; this format uses characters range0000-007Fexclusively and is not locale-sensitive.abstract booleanisValidIdentifier(java.lang.String name)Indicates if the specified name can be used as unit identifier.abstract voidlabel(Unit<?> unit, java.lang.String label)Attaches a system-wide label to the specified unit.Unit<?>parseObject(java.lang.String source, java.text.ParsePosition pos)Parses the text from a string to produce an object (implementsjava.text.Format).abstract Unit<? extends 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 Unit<? extends Quantity>parseSingleUnit(java.lang.CharSequence csq, java.text.ParsePosition pos)Parses a sequence of character to produce a single unit.
-
-
-
Method Detail
-
getInstance
public static UnitFormat getInstance()
Returns the unit format for the default locale (format used byUnit.valueOf(CharSequence)andUnit.toString()).- Returns:
- the default unit format (locale sensitive).
-
getInstance
public static UnitFormat getInstance(java.util.Locale inLocale)
Returns the unit format for the specified locale.- Returns:
- the unit format for the specified locale.
-
getUCUMInstance
public static UnitFormat getUCUMInstance()
Returns the UCUM international unit format; this format uses characters range0000-007Fexclusively and is not locale-sensitive. For example:kg.m/s2- Returns:
- the UCUM international format.
-
format
public abstract java.lang.Appendable format(Unit<?> unit, java.lang.Appendable appendable) throws java.io.IOException
Formats the specified unit.- Parameters:
unit- the unit to format.appendable- the appendable destination.- Throws:
java.io.IOException- if an error occurs.
-
parseProductUnit
public abstract Unit<? extends Quantity> parseProductUnit(java.lang.CharSequence csq, java.text.ParsePosition pos) throws java.text.ParseException
Parses 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.java.text.ParseException
-
parseSingleUnit
public abstract Unit<? extends Quantity> parseSingleUnit(java.lang.CharSequence csq, java.text.ParsePosition pos) throws java.text.ParseException
Parses 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.java.text.ParseException
-
label
public abstract void label(Unit<?> unit, java.lang.String label)
Attaches a system-wide label to the specified unit. For example:If the specified label is already associated to an unit the previous association is discarded or ignored.UnitFormat.getInstance().label(DAY.multiply(365), "year"); UnitFormat.getInstance().label(METER.multiply(0.3048), "ft");- Parameters:
unit- the unit being labelled.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(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:If the specified label is already associated to an unit the previous association is discarded or ignored.UnitFormat.getLocaleInstance().alias(METER.multiply(0.3048), "foot"); UnitFormat.getLocaleInstance().alias(METER.multiply(0.3048), "feet"); UnitFormat.getLocaleInstance().alias(METER, "meter"); UnitFormat.getLocaleInstance().alias(METER, "metre");- 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).- Specified by:
formatin classjava.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 Unit<?> parseObject(java.lang.String source, java.text.ParsePosition pos)
Parses the text from a string to produce an object (implementsjava.text.Format).- Specified by:
parseObjectin classjava.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.
-
-
DMelt 3.0 © DataMelt by jWork.ORG